Inheritance
Inheritance
Inheritance is a mechanism wherein a new
class is derived from an existing class. In Java,
classes may inherit or acquire the properties and
methods of other classes. A class derived from
another class is called a subclass, whereas the class
from which a subclass is derived is called a
superclass. A subclass can have only one superclass,
whereas a superclass may have one or more
subclasses.
Syntax
class Subclass-name extends Superclass-name
{
//methods and fields
}
The extends keyword indicates that you are making
a new class that derives from an existing class.
Example
class Employee
{
float salary=40000;
}
class Programmer extends Employee
{
int bonus=10000;
public static void main(String args[])
{
Programmer p=new Programmer();
System.out.println("Programmer salary is:"+p.salary);
System.out.println("Bonus of Programmer is:"+p.bonus);
}
}
Output
Programmer salary is:40000.0
Bonus of programmer is:10000
Programmer is the subclass and Employee is the superclass.
In the above example, Programmer object can access the field
of own class as well as of Employee class i.e. code reusability.
Types of inheritance in java :
Single Inheritance
Multiple Inheritance
Multilevel Inheritance
Hierarchical Inheritance
Hybrid Inheritance
There are 5 types of inheritance
In java programming, multiple and hybrid inheritance is
supported through interface only.
Single Inheritance
Single inheritance is damn easy to understand. 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 classof
B and B would be a child class of A.
Class A
Class B
Syntax
class A
{
}
class B extends A
{
}
Class B extends A
{
}
Example
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
}
}
Multi level 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.
Class A
Class B
Class C
Syntax
class A
{
}
class B extends A
{
}
class C extends B
{
}
Example
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
} }
Hierarchical Inheritance
In such kind of inheritance one class is inherited by
many sub classes. In below example class B and
C inherits the same class A. A is parent class (or base
class) of B,C & D.
Class A
Class CClass B
Syntax
class base_class_name
{
}
class derived_class_name1 extends base_class_name
{
}
class derived_class_name2 extends base_class_name
{
}
Example
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 void methodB()
{
System.out.println("method of Class B");
}
public static void main(String args[])
{
B obj1 = new B(); C obj2 = new C();
D obj3 = new D(); obj1.methodA();
obj2.methodA(); obj3.methodA();
} }
Multiple Inheritance
“Multiple Inheritance” refers to the concept of one class
extending (Or inherits) more than one base class. The
inheritance we learnt earlier had the concept of one base class or
parent. The problem with “multiple inheritance” is that the
derived class will have to manage the dependency on two base
classes.
Class C
Class BClass A
Hybrid Inheritance
In simple terms you can say that Hybrid inheritance is a
combination of Singleand Multiple inheritance. A typical flow diagram
would look like below. A hybrid inheritance can be achieved in the java in a
same way as multiple inheritance can be!! Using interfaces. yes you heard it
right. By usinginterfaces you can have multiple as well as hybrid
inheritance in Java.
Class A
Class B Class C
Class D
Inheritance

Inheritance

  • 1.
  • 2.
    Inheritance Inheritance is amechanism wherein a new class is derived from an existing class. In Java, classes may inherit or acquire the properties and methods of other classes. A class derived from another class is called a subclass, whereas the class from which a subclass is derived is called a superclass. A subclass can have only one superclass, whereas a superclass may have one or more subclasses.
  • 3.
    Syntax class Subclass-name extendsSuperclass-name { //methods and fields } The extends keyword indicates that you are making a new class that derives from an existing class.
  • 4.
    Example class Employee { float salary=40000; } classProgrammer extends Employee { int bonus=10000; public static void main(String args[]) { Programmer p=new Programmer(); System.out.println("Programmer salary is:"+p.salary); System.out.println("Bonus of Programmer is:"+p.bonus); } }
  • 5.
    Output Programmer salary is:40000.0 Bonusof programmer is:10000 Programmer is the subclass and Employee is the superclass. In the above example, Programmer object can access the field of own class as well as of Employee class i.e. code reusability.
  • 6.
    Types of inheritancein java : Single Inheritance Multiple Inheritance Multilevel Inheritance Hierarchical Inheritance Hybrid Inheritance There are 5 types of inheritance In java programming, multiple and hybrid inheritance is supported through interface only.
  • 7.
    Single Inheritance Single inheritanceis damn easy to understand. 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 classof B and B would be a child class of A. Class A Class B
  • 8.
    Syntax class A { } class Bextends A { } Class B extends A { }
  • 9.
    Example Class A { publicvoid 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 } }
  • 10.
    Multi level Inheritance Multilevelinheritance 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. Class A Class B Class C
  • 11.
    Syntax class A { } class Bextends A { } class C extends B { }
  • 12.
    Example Class X { public voidmethodX() { System.out.println("Class X method"); } } Class Y extends X { public void methodY() { System.out.println("class Y method"); } }
  • 13.
    Class Z extendsY { 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 } }
  • 14.
    Hierarchical Inheritance In suchkind of inheritance one class is inherited by many sub classes. In below example class B and C inherits the same class A. A is parent class (or base class) of B,C & D. Class A Class CClass B
  • 15.
    Syntax class base_class_name { } class derived_class_name1extends base_class_name { } class derived_class_name2 extends base_class_name { }
  • 16.
    Example Class A { public voidmethodA() { 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"); } }
  • 17.
    Class D extendsA { public void methodD() { System.out.println("method of Class D"); } } Class MyClass { public void methodB() { System.out.println("method of Class B"); } public static void main(String args[]) { B obj1 = new B(); C obj2 = new C(); D obj3 = new D(); obj1.methodA(); obj2.methodA(); obj3.methodA(); } }
  • 18.
    Multiple Inheritance “Multiple Inheritance”refers to the concept of one class extending (Or inherits) more than one base class. The inheritance we learnt earlier had the concept of one base class or parent. The problem with “multiple inheritance” is that the derived class will have to manage the dependency on two base classes. Class C Class BClass A
  • 19.
    Hybrid Inheritance In simpleterms you can say that Hybrid inheritance is a combination of Singleand Multiple inheritance. A typical flow diagram would look like below. A hybrid inheritance can be achieved in the java in a same way as multiple inheritance can be!! Using interfaces. yes you heard it right. By usinginterfaces you can have multiple as well as hybrid inheritance in Java. Class A Class B Class C Class D