INHERITANCE
1
INHERITANCE BASICS
1. A class can inherit the properties of another class and add its
own modification.
2. The class whose properties are extended is known as super or
base or parent class.
3. The class which extends the properties of super class is known
as sub or derived or child class
4. To declare inherited classes ‘extends’ keyword is used
class B extends A { ….. } A
A is super class
B is sub class B
Types of Inheritance
Single Inheritance Hierarchical Inheritance
A X
B A B C
MultiLevel Inheritance
C
Defining a Subclass
Syntax :
class <subclass name> extends <superclass name>
{
variable declarations;
method declarations;
}
• Extends keyword signifies that properties of the super class are
extended to sub class
• Sub class will not inherit private members of super class
Single Inheritance
class SingleInheritance {
class A {
int i, j; public static void main(String args[]) {
void show() { A a1 = new A();
System.out.println("i and j: " + i + " " + j); B b1 = new B();
} // using suerclass object
} a1.i = 10;
class B extends A {
a1.j = 20;
int k,sum;
a1.show();
void display() {
//using subclass object
System.out.println("k: " + k);
} b1.i = 7;
void sum() { b1.j = 8;
sum=i+j+k; b1.k = 9;
System.out.println("i+j+k: " + sum); b1.show();
} b1.display();
} b1.sum()
5
MultiLevel Inheritance
class MultilevelInheritance {
class A {
public static void main(String args[]) {
int i;
A a1 = new A();
void show() {
B b1 = new B();
System.out.println("i : " + i );
C c1 = new C();
}
//class A
}
a1.i = 10;
class B extends A {
//class B
int j;
b1.i = 100;
void display() {
b1.j = 20;
System.out.println(“j: " + j);
b1.show(); A
}
b1.dispaly();
}
//class C
class C extends B {
c1.i = 7; B
int k;
c1.j = 8;
void displayK() {
c1.k = 9;
System.out.println("k: " + k); C
c1.show();
}
c1.display();
}
c1.displayk();}}
6
private members are not inherited
class A {
int i;
private int j;
void show() {
System.out.println("i and j: " + i + " " + j);
}
}
class B extends A {
int k,sum;
void display() {
System.out.println("k: " + k);
}
void sum() {
sum=i+j+k; //error ,j is private
System.out.println("i+j+k: " + sum);
}
} 7
protected members are inherited in subclass
class A {
int i;
protected int j;
void show() {
System.out.println("i and j: " + i + " " + j);
}
}
class B extends A {
int k,sum;
void display() {
System.out.println("k: " + k);
}
void sum() {
sum=i+j+k;
System.out.println("i+j+k: " + sum);
}
} 8
Hierarchical Inheritance
class HeirachicalInheritance {
class A {
public static void main(String args[]) {
int i;
A a1 = new A();
void show() {
B b1 = new B();
System.out.println("i : " + i );
C c1 = new C();
}
//class A
}
a1.i = 10;
class B extends A {
//class B
int j;
b1.i = 100;
void display() {
b1.j = 20;
System.out.println(“j: " + j);
b1.show();
}
b1.dispaly();
} X
//class C
class C extends A {
c1.i = 7;
int k;
c1.k = 9;
void displayK() { A B C
c1.show();
System.out.println("k: " + k);
c1.displayk();
}
}}
}
9
A Superclass Variable Can Reference a Subclass Object
class InheritanceDemo {
class A {
int i, j; public static void main(String args[]) {
void show() { A a1 = new A();
System.out.println("i and j: " + i + " " + j); B b1 = new B();
} b1.i = 7;
} b1.j = 8;
class B extends A {
b1.k = 9;
int k,sum;
b1.show();
void display() {
b1.display();
System.out.println("k: " + k);
}
} a1=b1;
a1.show();
a1.display(); //error
}}
10
Using super
super keyword is used :
to call the superclass' constructor.
to access a member of the superclass that has
been hidden by a member of a subclass.
11
first use of super
number of parameters in subclass constructor
is equal to sum of number of data members in
super class and its own, if all are distinct.
super must be a very first statement in
subclass constructor
12
Using super to Call Superclass Constructors
class A { class C extends B {
int i; int k;
A(int p) C(int x,int y,int z)
{ {
i=p; super(x,y);
} k=z;
void show() { }
System.out.println("i : " + i ); void displayK() {
}} System.out.println("k: " + k);
}}
class B extends A { class SuperDemo {
int j; public static void main(String args[]) {
B(int a,int b)
{ C c1 = new C(100,200,300);
super(a); System.out.println("USING C'S CONSTRUCTOR");
j=b; c1.show();
} c1.display();
void display() { c1.displayK();
System.out.println("j: " + j); }}
}} 13
A Second Use for super
super keyword is used when subclass and super
class have members with the same name.
In this case subclass hides the member of super class
General form:
super.member
Here, member can be either a method or an instance
variable.
14
class A {
int i;
}
OUTPUT
class B extends A {
int i; // this i hides the i in A
i in superclass: 1
B(int a, int b) {
super.i = a; // i in A
i in subclass: 2
i = b; // i in B
}
void show() {
System.out.println("i in superclass: " + super.i);
System.out.println("i in subclass: " + i);
}
}
class UseSuper {
public static void main(String args[]) {
B subOb = new B(1, 2);
subOb.show();
}
}
15
When Constructors Are Called
In a class hierarchy, constructors are called in order
of derivation, from super class to sub class
super( ) must be the first statement executed in a
subclass constructor, this order is the same whether
or not super( ) is used.
If super( ) is not used, then the default constructor of
each superclass will be executed
16
class A {
A() {
System.out.println("Inside A's constructor.");
} OUTPUT
}
class B extends A { Inside A's constructor
B() {
super(); Inside B's constructor
System.out.println("Inside B's constructor.");
}} Inside C's constructor
class C extends B {
C() {
super();
System.out.println("Inside C's constructor.");
}}
class CallingCons {
public static void main(String args[]) {
C c = new C();
}
}
17
class A {
A() {
System.out.println("Inside A's constructor.");
} OUTPUT
}
class B extends A { Inside A's constructor
B() {
System.out.println("Inside B's constructor."); Inside B's constructor
}
} Inside C's constructor
class C extends B {
C() {
System.out.println("Inside C's constructor.");
}
}
class CallingCons {
public static void main(String args[]) {
C c = new C();
}
}
18
Method Overriding
A method in a subclass has the same name and type signature
as a method in its superclass, then the method in the subclass
is said to override the method in the superclass.
It is a feature that supports polymorphism.
When an overridden method is called through the subclass
object, it will always refer to the version of the method
defined by the subclass.
The superclass version of the method is hidden.
19
Program for method overriding
class A { void show() {
int i, j; System.out.println("k: " + k);
A(int a, int b) { }
i = a;
}
j = b;
class Override {
}
void show() { public static void main(String args[]) {
System.out.println("i and j: " + i + " " + j);
} B subOb = new B(1, 2, 3);
}
class B extends A { subOb.show(); // this calls show() in B
int k; }
B(int a, int b, int c) {
}
super(a, b);
k = c;
}
20
Program for method overriding
class A { void show() {
int i, j; super.show(); //calls show() in A
A(int a, int b) { System.out.println("k: " + k);
i = a;
}
j = b;
}
}
void show() { class Override {
System.out.println("i and j: " + i + " " + j); public static void main(String args[]) {
}
} B subOb = new B(1, 2, 3);
class B extends A {
int k; subOb.show(); // this calls show() in B
B(int a, int b, int c) {
}
super(a, b);
}
k = c;
}
21
class A { class C extends B {
int i, j; float x,y;
A(int a, int b) { C(int a, int b, int c,int d,int e,float f,float g) {
i = a;
super(a, b,c,d,e);
j = b;
x=f;
}
void add() { y=g;
System.out.println("i + j: " + (i + j)); }
} void add() {
} super.add();
class B extends A { System.out.println(“x+y: " + (x+y));
int p,q,r; }
B(int a, int b, int c,int d,int e) {
}
super(a, b);
class Override {
p = c;
q=d; public static void main(String args[]) {
r=e;
} C c1=new C(1,2,3,4,5,1.2,1.3)
void add() {
super.add(); c1.add();
System.out.println(“p + q+r: " + (p + q+r)); }
}
} 22
}
Run time polymorphism
Dynamic method dispatch is the mechanism by which a call to an
overridden function is resolved at run time, rather than at compile time.
When an overridden method is called through a super class reference,
Java determines which version of that method to execute based upon
the type of the object being referred to at the time the call occurs
If we assign object of subclass to super class reference then subclass
method is called and if object of super class is assigned to super class
reference then super class method is called
23
Dynamic method dispatch
class A {
void show() { class Dispatch {
System.out.println("Inside A's method"); public static void main(String args[]) {
} A a = new A(); // object of type A
} B b = new B(); // object of type B
class B extends A { C c = new C(); // object of type C
void show() { A r; // obtain a reference of type A
System.out.println("Inside B's method"); r = a; // r refers to an A object
} r. show(); // calls A's version of show()
} r = b; // r refers to a B object
class C extends A { r. show(); // calls B's version of show()
void show() { r = c; // r refers to a C object
System.out.println("Inside C's method"); r. show(); // calls C's version of show()
} }
}
OUTPUT
Inside A's method
Inside B's method
Inside C's method
24
Abstract class
The class which contains one or more abstract
method is called as abstract class.
Abstract method contains no implementation, i.e. no
body.
abstract keyword is used to declare abstract class.
Syntax
abstract classname
{
………….
abstract methodname();
………
}
Abstract classes define partial behaviour.
Abstract class
Abstract classes cannot be instantiated with the
new operator i.e. we can not create object of
abstract class, but they can have reference
variable.
We cannot declare abstract constructors, or
abstract static methods
Any subclass of an abstract class must either
implement all of the abstract methods in the
super class, or be itself declared abstract.
26
Why create abstract methods?
To force same name and signature pattern in all the
subclasses
Subclasses should not use their own naming patterns
They should have the flexibility to code these
methods with their own specific requirements.
Example of Abstract class
class AbstractDemo {
abstract class A {
public static void main(String args[]) {
void show() {
System.out.println("Inside A's method"); B b1 = new B();
} b1.display();
abstract void display(); b1.show();
}
class B extends A { A a1 = new A(); //error
void display() { A a2;
a2=b1;
System.out.println(“B defines display");
a2.display();
}
a2.show();
}
final keyword
Declaring constants
final int MAX=100;
Preventing method overriding
final void show (final int x)
Preventing inheritance
final class Demo {}
class A {
final float PI=3.14f;
final void show() {
………….
}
}
final class B extends A {
void show() { //error
…………………..
}
}
class C extends B{ //error
{
…………………
}
30
Multiple Inheritance
A B class A
{
int i=1;
C
}
class B
class C has two super {
classes as A & B int i=2;
}
class C extends A,B
{
//two copies of i are available
}
31
Java does not allow such multiple inheritance
directly. So one class can not extend two
super classes
The reason is to just keep the language
simple.
Java allows multiple inheritance through the
use of interface concept.
32
Interfaces
Interfaces are syntactically similar to classes, but they
lack instance variables, and their methods are declared
without any body.
Interfaces defines only abstract methods and final
variables.
Once it is defined, any number of classes can
implement an interface
It is responsibility of class which implements the
interface to define the abstract methods
Can’t create object for interface, but we can have
reference for it.
33
Defining an Interface
interface keyword is used to define interface.
interface Interfacename {
return-type method-name1(parameter-list);
return-type method-name2(parameter-list);
type final-varname1 = value;
type final-varname2 = value;
…………………………………..
}
Methods end with a semicolon after the parameter
list i.e. they are abstract methods
Variables are implicitly final and static.
34
Implementing Interfaces
To implement an interface, the implements keyword is used
General form
class classname implements interfacename1, interfacename2….
{
// class-body
}
or
class classname extends classname implements interfacename1,
interfacename2….
{
// class-body
}
35
Example of Interface
class InterfaceDemo {
interface A {
public static void main(String args[]) {
void show(),
int a=100; B b1 = new B();
} b1.display();
class B implements A { b1.show();
public void show() {
a=20;//error A a1 = new A(); //error
System.out.println(“B implements A a2;
method"); a2=b1;
a2.show();
}
}
void display()
}
{
System.out.println(“B’s own method");
}
}
Write program for following
inheritance hierarchy
STUDENT
int roll
void getroll()
interface
TEST SPORTS
int m1,m2 float smarks=5.8f
void getm() void putsp()
RESULT
float total
void display()
37
class Result extends Test implements Sports
import java.util.*;
{
class Student{
float total;
int roll;
public void puts()
Scanner sc =new Scanner(System.in);
{
void getroll()
System.out.println("sports marks="+m);
{
}
System.out.println("Enter roll no");
void display()
roll=sc.nextInt();
{
}
total=m1+m2+m;
}
System.out.println("Roll No="+roll);
class Test extends Student
System.out.println("Marks1="+m1);
{
System.out.println("Marks2="+m2);
int m1,m2;
System.out.println("sports marks="+m);
Scanner sc =new Scanner(System.in);
System.out.println("Total="+total);
void getm()
}}
{
class MainClass
System.out.println("Enter m,arks of 2 subjects");
{
m1=sc.nextInt();
public static void main(String ar[])
m2=sc.nextInt();
{
}
Result r1=new Result();
}
r1.getroll();
interface Sports
r1.getm();
{
r1.puts();
float m=5.8f;
r1.display();
void puts();
}} 38
}
Interfaces Can Be Extended
One interface can inherit another by use of the
keyword extends.
The syntax is the same as for inheriting
classes.
When a class implements an interface that
inherits another interface, it must provide
implementations for all methods defined
within the interface inheritance chain.
39
interface A {
void show();
}
interface B extends A {
void display();
}
// This class must implement all of A and B methods
class MyClass implements B {
public void show() {
System.out.println("Implement show().");
}
public void display() {
System.out.println("Implement display().");
}}
class IFExtend {
public static void main(String arg[]) {
MyClass ob = new MyClass();
ob.show();
ob.display();
40
}}