Unit 4-Inheritance and Interface
Unit 4-Inheritance and Interface
Introduction
The mechanism of deriving a new class
from an old one is called Inheritance.
In the inheritance the class(old class)
which is give data members and methods
is known as base or super or parent class.
The class(new class) which is taking the
data members and methods is known as
sub or derived or child class.
The concept of inheritance is also known
as re-usability or extendable classes or sub
classing or derivation.
Example
Single inheritance
In single inheritance there exists
single base class and single derived
class.
Syntax:
class subclassname extends
superclassname
{
classmembers;
}
E class Employee
x {
a float salary=40000;
m }
class Vehicle
{
int speed=50;
}
class Bike4 extends Vehicle
{
int speed=100;
void display()
{ System.out.println(super.speed);
} //will print speed of Vehicle now
public static void main(String args[])
{
Bike4 b=new Bike4();
b.display();
}
}
Example where super is used to invoke parent class constructor.
class Vehicle
{
Vehicle()
{ System.out.println("Vehicle is created"); }
}
class Bike extends Vehicle
{
Bike()
{
super(); //will invoke parent class constructor
System.out.println("Bike is created");
}
public static void main(String args[])
{
Bike b=new Bike();
}
}
Example where super can be used to invoke parent class
method
class Person
{
void message()
{ System.out.println("welcome"); }
}
class Student extends Person
{
void message()
{ System.out.println("welcome to java");
}
void display()
{ message(); //will invoke current class message()
method
super.message(); //will invoke parent class message()
method
}
public static void main(String args[])
{
Student s=new Student();
s.display();
}
Example
In a garden, trees are maintained. A tree
has following set of attributes:-Tree code,
heights, base and amount spent on the
tree so far.
Define Tree class, its constructor, update()
that updates tree information and
display()
Define derive class Mango tree that has
additional yield attribute. Define Garden
class and display information of a tree
and a Mango Tree.(use super keyword)
solution
import java.util.Scanner;
class Tree
{
Scanner sc=new Scanner(System.in);
int Tree_code,height,base;
float amount_spent;
Tree(int tc,int h,int b,float am)
{
Tree_code=tc;
height=h;
base=b;
amount_spent=am;
}
contd…
C
void update()
o
{
n
System.out.println("update tree code, height, base and amount
t spent:");
d Tree_code=sc.nextInt();
… height=sc.nextInt();
base=sc.nextInt();
amount_spent=sc.nextFloat();
}
void display()
{
System.out.println("Tree code :"+Tree_code);
System.out.println("Tree height :"+height);
System.out.println("Tree base :"+base);
System.out.println("Amount spent on tree :"+amount_spent);
}
}
contd…
Contd…
class Mango extends Tree
{
int yield;
Mango(int tc,int h,int b,float am,int y)
{
super(tc,h,b,am);
yield=y;
}
void update()
{
super.update();
System.out.println("update yeild:");
yield=sc.nextInt();
}
contd…
contd… void display()
{
System.out.println("Tree code :"+Tree_code);
System.out.println("Tree height :"+height);
System.out.println("Tree base :"+base);
System.out.println("Amount spent on
tree :"+amount_spent);
System.out.println("Yield :"+yield);
}
}
class Garden
{
public static void main(String args[])
{
Mango m=new Mango(11,200,50,4000,50000);
m.display();
m.update();
m.display();
}
}
Question
Abstract Classes
A class that is declared as abstract is known as
abstract class.
It needs to be extended and its method implemented.
It cannot be instantiated.
When a class contains one or more abstract methods,
it should also declared abstract.
e.g. abstract class shape
{…..}
Example abstract class Bike
{
abstract void run();
}
class Honda extends Bike
{
void run()
{
System.out.println("running safely..")
;
}
Private
Protected
Levels of protection
provided by visibility
modifiers
Public Access
Friendly Access
Protected Access
Private Access
Private Protected Access
Public Access
A class, method, constructor, interface etc
declared public can be accessed from any
other class. Therefore fields, methods,
blocks declared inside a public class can
be accessed from any class belonging to
the Java Universe.
Example :
interface ItemConstants
{
int code=1001;
}
interface Item extends ItemConstants
{
void display();
}
Implementing Interfaces
Syntax :
class classname implements
interfacename
{ body of classname }
n {
public void eat();
public void travel();
}
class Mammal implements Animal
{
public void eat()
{ System.out.println("Mammal eats");
}
public void travel()
{ System.out.println("Mammal travels");
}
public static void main(String args[])
{
Mammal m = new Mammal();
m.eat();
m.travel();
}
Multiple inheritance with
interfaces
interface Exam
{ void percent_cal(); }
class Student
{
String name="Ramesh";
int roll_no=01;
int mark1=93;
int mark2=84;
}
class Result extends Student implements Exam
{
public void percent_cal()
{
int total=(mark1+mark2);
float percent=total*100/200;
System.out.println ("Percentage:
"+percent+"%");
}
Contd…
void display()
{
System.out.println ("Name of Student: "+name);
System.out.println ("Roll No. of Student:
"+roll_no);
System.out.println ("Marks of Subject 1:
"+mark1);
System.out.println ("Marks of Subject 2:
"+mark2);
}
}
class MultipleInheritance
{
public static void main(String args[])
{
Result R=new Result();
R.display();
R.percent_cal();
}
}
Question
interface A
{
public void methodA();
}
interface B extends A
{
public void methodB();
}
interface C extends A
{
public void methodC();
}
contd…
Contd…class D implements B, C
{
public void methodA()
{ System.out.println("MethodA");
}
public void methodB()
{ System.out.println("MethodB");
}
public void methodC()
{ System.out.println("MethodC");
}
public static void main(String args[])
{
D obj1= new D();
obj1.methodA();
obj1.methodB();
obj1.methodC();
}
}
Hybrid inheritance using interface and classes
interface A
{
public void methodA();
}
class B implements A
{
public void methodB()
{
System.out.println("MethodB");
}
public void methodA()
{
System.out.println("MethodA");
}
}
contd…
Contd…
interface C extends A
{
public void methodC();
}
class D extends B implements C
{
public void methodC()
{ System.out.println("MethodC"); }
public static void main(String args[])
{
D obj1= new D();
obj1.methodA();
obj1.methodB();
obj1.methodC();
}
Question
d class Area_Demo
{
…
public static void main(String arg[])
{
Rect r=new Rect();
Tri t=new Tri();
Circle c=new Circle();
System.out.println(“The Area of rectangle
is:”+r.compute(10,20));
System.out.println(“The Area of Triangle
is :”+t.compute(10,12));
System.out.println(“The Area of Circle
is :”+c.compute(10,12));
}
Question