Topics
• Inheritance in Java
1 Object-Oriented Programming Using Java
Inheritance Basics
• Reusability is achieved by Inheritance
• Java classes can be Reused by Extending a class. Extending an
existing class is nothing but reusing properties of the existing
classes.
• The class whose properties are extended is known as super or base
or parent class.
• The class which extends the properties of super class is known as
sub or derived or child class
• A class can either extends another class or can implement an
interface
2 Object-Oriented Programming Using Java
Java’s Support For Inheritance
• By Extending a Class
Syntax
class class-name extends super-class-name
{
……..
}
• Example
class A
{
………
}// End of class A A <<super-class>>
class B extends A
{
……..
}// End of class B
B <<sub-class>>
3 Object-Oriented Programming Using Java
Java’s Support For Inheritance
• By Implementing an Interface
Syntax
class class-name implements interface-name
{
……..
}
• Example
interface A
{
………
}// End of interface A A <<interface>>
class B implements A
{
……..
}// End of class B
B <<sub-class>>
4 Object-Oriented Programming Using Java
Forms of Inheritance
Single Inheritance Hierarchical Inheritance
A A X X
B B A B C A B C
Multiple Class Inheritance Multiple Interface Implementation
NOT SUPPORTED BY JAVA SUPPORTED BY JAVA
Multi-Level Inheritance Multiple Inheritance
A A
A B A B
B B
C C C C
5 Object-Oriented Programming Using Java
Multiple Inheritance in Java
• Java Does Not Support Multiple Inheritance by Extending
Multiple Classes
• The Following Code is Wrong
class A { } // End of class A
class B { } // End of class B
class C extends A, B
Wrong Java Does not
{ Allow Multiple Class
}// End of class C Extensions
6 Object-Oriented Programming Using Java
Multiple Inheritance in Java ….
• Java Supports Multiple Inheritance by Implementing Multiple
Interfaces
• The Following Code is Correct
interface A { } // End of interface A
interface B { } // End of interface B
class C implements A, B Java Allows Multiple
{ Interface
}// End of class C Implementations
7 Object-Oriented Programming Using Java
Thank You
8 Object-Oriented Programming Using Java