Inheritance in Java
Inheritance in Java
Inheritance in Java
Mechanism of deriving new class from old class.
The old class is known as-
Single inheritance -
When a class extends another one class only
then we call it a single inheritance.
The below flow diagram shows that class B
extends only one class which is A.
Here A is a parent class of B and B would
A
be a child class of A.
B
Single Inheritance example program in Java
Class A
{
public void methodA()
{
System.out.println("Base class method");
}
}
Class B extends A
{
public void methodB()
{
System.out.println("Child class method");
}
public static void main(String args[])
{
B obj = new B();
obj.methodA(); //calling super class method
obj.methodB(); //calling local method
}
}
Single Inheritance example program in Java
class Base {
Base() {
System.out.println("Base Class Constructor Called ");
}
}
class Derived extends Base {
Derived() {
System.out.println("Derived Class Constructor Called ");
}
}
public class Main {
public static void main(String[] args) {
Derived d = new Derived();
}
}
Sub Class Constructor
A subclass constructor is used to construct the
instance variables of both the subclass and the
superclass.
The subclass constructor uses the keyword super to
invoke the constructor method of superclass.
The super Keyword is used with following
Conditions
super may only be used within a subclass constructor.
The call to super must appear as first statement.
Parameters in the super call must match with
declaration in superclass
Single Inheritance example program in Java
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();
}
}
“Multiple Inheritance”
“Multiple Inheritance” refers to the concept of one
class extending (Or inherits) more than one base
class.
The problem with “multiple inheritance” is that the
derived class will have to manage the dependency
on two base classes.
A B
C
Multilevel Inheritance
Multilevel inheritance refers to a mechanism in
OO technology where one can inherit from a
derived class, thereby making this derived class
the base class for the new class.
As you can see in below flow diagram C is subclass
or child class of B and B is a child class of A.
A
B
C
Class X
{
public void methodX()
{
System.out.println("Class X method");
}
}
Class Y extends X
{
public void methodY()
{
System.out.println("class Y method");
}
}
Class Z extends Y
{
public void methodZ()
{
System.out.println("class Z method");
}
public static void main(String args[])
{
Z obj = new Z();
obj.methodX(); //calling grand parent class method
obj.methodY(); //calling parent class method
obj.methodZ(); //calling local method
}
}
class Car{
public Car()
{
System.out.println("Class Car");
}
public void vehicleType()
{
System.out.println("Vehicle Type: Car");
}
}
class Maruti extends Car{
public Maruti()
{
System.out.println("Class Maruti");
}
public void brand()
{
System.out.println("Brand: Maruti");
}
public void speed()
{
System.out.println("Max: 90Kmph");
}
}
public class Maruti800 extends Maruti{
public Maruti800()
{
System.out.println("Maruti Model: 800");
}
public void speed()
{
System.out.println("Max: 80Kmph");
}
public static void main(String args[])
{
Maruti800 obj=new Maruti800();
obj.vehicleType();
obj.brand();
obj.speed();
}
}
class Design {
Design(){
System.out.println("Design()...");
} public class TestCallOrder {
} public static void
class Coding extends Design { main(String[] args) {
Coding(){ //Create object of bottom
System.out.println("coding()..."); most class object
} System.out.println("Construct
} or call order...");
class Testing extends Coding { new Testing();
Testing()
{ }
System.out.println("Testing()..."); }
}
}
class X class Z extends Y
{ {
int i; int k;
X(int i) Z(int i, int j, int k)
{ {
this.i = i; super(i, j);
System.out.println("Created X"); this.k = k;
} System.out.println("Created Z");
} }
}
class Y extends X
{ class MultiLevelConstructors
int j; {
Y(int i, int j) public static void main(String arg[])
{ {
super(i); Z z = new Z(12, 22, 32);
this.j = j; System.out.println("---------------");
System.out.println("Created Y");
} }
} }
Hierarchical Inheritance
In such kind of inheritance one class is inherited
by many sub classes.
In below example class B,C and D inherits the
same class A.
A is parent class (or base class) of B,C & D.
B C D
Class A
{
public void methodA()
{
System.out.println("method of Class A");
}
}
Class B extends A
{
public void methodB()
{
System.out.println("method of Class B");
}
}
Class C extends A
{
public void methodC()
{
System.out.println("method of Class C");
}
}
Class D extends A
{
public void methodD()
{
System.out.println("method of Class D");
}
}
Class MyClass
{
public static void main(String args[])
{
B obj1 = new B();
C obj2 = new C();
D obj3 = new D();
obj1.methodA();
obj2.methodA();
obj3.methodA();
}} `
Overriding Methods
If subclass (child class) has the same method as declared
in the parent class, it is known as method overriding.
In other words, If subclass provides the specific
implementation of the method that has been provided by
one of its parent class, it is known as Method Overriding.
Advantage of Java Method Overriding
provide specific implementation of a method that is already provided by
its super class.
class Test{
public static void main(String args[]){
SBI s=new SBI();
ICICI i=new ICICI();
System.out.println("SBI Rate of Interest: "+s.getRateOfInterest());
System.out.println("ICICI Rate of Interest: "+i.getRateOfInterest());
}
Method Overloading Method Overriding
1) Method overloading is used Method overriding is used
to increase the readability of to provide the specific
the program. implementation of the
method that is already
provided by its super class.