Inheritance
Inheritance
Inheritance
Chapter Topics
Chapter 6 discusses the following main topics:
⚫ Inheritance Basics
⚫ Using Super
⚫ Method Overriding
⚫ Dynamic Method Dispatch
⚫ Abstract Methods
⚫ Final
Class Hierarchy
Insect
Contains those attributes
and methods that are
shared by all insects.
BumbleBee Grasshopper
11-7
Inheritance
•Hybrid Inheritance
Example:
class A class B extends A
{ {
int i; int j;
void showi() void showj()
{
{
System.out.println(“j: " + j);
System.out.println("i: " + i); }
} void sum()
} {
System.out.println("i+j: " + (i+j));
}
}
class SimpleInheritance
{
public static void main(String args[])
{
A a = new A();
B b = new B();
a.i = 10;
System.out.println("Contents of a: ");
a.showi();
b.i = 7;
b.j = 8;
System.out.println("Contents of b: ");
b.showi();
b.showj();
System.out.println("Sum of I and j in b:");
b.sum();
}
}
Super
• ‘super’ is a keyword used to refer to instance
variables of super class from sub class.
✔ super.a=10;
• It is used to call a constructor of super class from
constructor of sub class which should be first
statement.
✔ super(arg-list);
• It is used to call a super class method from sub
class method to avoid redundancy of code
✔ super.addNumbers(a, b);
Super
Super and Hiding
class A {
int i;
}
class B extends A {
int i;
B(int a, int b) {
super.i = a; i = b;
}
void show() {
System.out.println("i in superclass: " + super.i);
System.out.println("i in subclass: " + i);
}
}
Example: Super and Hiding
•Although the i variable in B hides the i variable in A,
super allows access to the hidden variable of the
super-class:
class UseSuper {
public static void main(String args[]) {
B subOb = new B(1, 2);
subOb.show();
}
}
Method Overriding
•
Usage of Java Method Overriding
⚫Method overriding is used to provide the specific
implementation of a method which is already provided by
its superclass.
⚫Method overriding is used for runtime polymorphism
Rules for Java Method Overriding
⚫The method must have the same name as in the parent
class
⚫The method must have the same parameter as in the
parent class.
⚫There must be an IS-A relationship (inheritance).
Class Demo
Class A
{
Example Class B
{ {
int i, j; int k; public static void main(String ar
A(int a1, int a2) B(int x, int y, int z) {
{ { B ob = new B(2,5,9);
i =a1; super(x,y); ob.display();
j=a2; k =z; }
} } }
void display() void display()
{ {
System.out.println(i+ j); // calling overrriden
} method
} Super.display();
System.out.println(k);
}
}
Dynamic Method Dispatch or Runtime Polymorphism
}
Abstract class and Methods
• An abstract class is a class that is declared with abstract keyword.
• An abstract method is a method that is declared without an
implementation.
• An abstract class may or may not have all abstract methods. Some of
them can be concrete methods
• A method defined abstract must always be redefined in the
subclass,thus making overriding compulsory OR either make subclass
itself abstract.
• Any class that contains one or more abstract methods must also be
declared with abstract keyword.
• There can be no object of an abstract class.That is, an abstract class
can not be directly instantiated with the new operator.
• An abstract class can have parametrized constructors and default
constructor is always present in an abstract class.
Example
abstract class Shape class TestAbstraction1{
{ public static void main(String args[])
abstract void draw(); {
} Shape s=new Circle1();
s.draw();
//In real scenario, implementation is provide }
d by others i.e. unknown by end user }