Inheritance, Interface and Polymorphism
Inheritance, Interface and Polymorphism
INTERFACE AND
POLYMORPHISM
Chapter-12
Lesson Outcome
✔ Introduction
✔ Need of Inheritance
✔ Different Forms of Inheritance
✔ Derived/Sub and Base/Super Classes
✔ Inheritance and Constructors
✔ Abstract Classes
✔ Interfaces
✔ Polymorphism in Java
Introduction
• Inheritance 🡪 Capability of one class to derive properties from another class.
Automobiles
Vehicles
Car
Derived class inheriting the properties including the methods of the old class known as base
class
Advantage: Code reusability
Once a base class is written and debugged it can be used in various situations without having
to redefine it or rewrite it. Already tested and saved previous code can be reused
Without redefining the old class we can add new properties to derived class and inherited class
member function's can be redefined
Need of Inheritance
• Capability to express the inheritance relationship which ensures closeness with the real –
world models
• Reusability[Allows addition of additional features to an existing class without modifying
it.,
• Eg: Student🡪 GraduateStudent(Derived Class) with new features added that differentiate
the between students and graduate students
• Transitive nature of inheritance
• Person
Student
Graduate Student
Different forms of Inheritance
• New class that is been derived is called derived class/sub-class/target class🡪
inherits from the base class
• Existing class🡪 base class or super class/parent class
• Single Inheritance
• When a subclass inherits only one base class it is known a s single inheritance
X Base Class
Sub Class
Y
Different forms of Inheritance
• Multiple Inheritance🡪When a subclass inherits from multiple base classes it is known as
multiple inheritance
Base classes
X Y Inheritance Graph/Derivation
Derivation from multiple base classes
Z Sub Class
Multiple Inheritance
Different forms of Inheritance
• Hierarchical Inheritance
• When many subclass inherit from a single base class it is known as hierarchical
inheritance
Different forms of Inheritance
• Multilevel Inheritance
• The transitive nature of inheritance is reflected by this form of inheritance
• When a subclass inherits from a class that itself inherits from another class, it is
known as multilevel inheritance
Inheritance hierarchy/Derivation
Relationship between base and derived class
Different forms of Inheritance
• Hybrid inheritance
• When a subclass inherits from multiple base class and all of its base classes inherit
from a single base class
Derived/Sub and Base/Super Classes
• Derived class/Sub class has to identify from which it is derived(base/super class)
• Java way of identifying the base class is to include the base class name in the
derived class definition
• A subclass is defined by using the keyword extends along with the super/base
class name in addition to its own details
• Syntax of defining a derived/sub class
• class<sub class-name> extends < super-class name> {
• :// Members of sub class
•}
Concept of Inheritance
// Java program to illustrate
// Java program to illustrate
// protected modifier
// protected modifier
package p1; package p2;
import p1.*; // importing all classes in package p1
// Class A
// Class B is subclass of A
public class A class B extends A
{ {
public static void main(String args[])
protected void display()
{
{
B obj = new B();
System.out.println(“Old is Gold"); obj.display();
} }
Access Control of Inherited members
• Although a subclass or derived class inherits all the members of its super class yet it can access only those
variables/methods for it has access permissions
• Access of inherited members depends upon their access modifiers
• i.e whether they are private or public or protected or something else
• Access rules of inherited members:
• Private: members are accessible only inside their own class where they have declared
• Public: members are accessible in all the classes whether a subclass or class in the same package or class in
another class along with their own class
• Protected: member s are accessible inside their own class as well as in all subclasses of their class regardless
of whether subclasses exist in the same package or any other package
• default: members with no access specifier
• members are accessible only inside classes that are in same package as that of their own class. These
members are not accessible outside the package; not even though subclass that are in some other package
• Private protected: members are accessible only from subclasses(whether in same aockage or any other
package)
Accessibility of Access Specifiers
Access Control of Inherited members-Example
Access Control of Inherited members-Example
Subtleties of Inheritance in Java
• Every class declared in Java is direct or indirect subclass of a class Object defined
in java.lang package.,
• you create a class with no extends keyword still it will be a subclass., and its
superclass is class Object provided by Java in java.lang package
• Java allows only single inheritance which means that a class has atmost one
immediate superclass
• Every class in Java has one and only one immediate/direct superclass, but might
have several subclasses
• This inheritance is transitive, therefore every class inherits state and behaviour
from all classes which are higher in the class hierarchy
Note:
• ``
Overriding Methods and Hiding Member
Variables
• Sometimes a subclass can use the same names for variables and methods as in the
superclass
• This leads to following behavior
• Methods are overridden
• Variables are hidden
• If a method or variable is used without explicitly specifying its parent class’s name ,only
the subclass member would be used or invoked.
Overriding Methods and Hiding Member
Variables
Referring to Overridden method
• We can still refer to the methods of superclass by using the keyword super
• E.g.,super.overriddenMethod()🡪 refers to the method as it is defined in the superclass
• Class Two extends One{
• :
• public void otherMethod(){
• System.out.println(“I am going to use variable value and invoke method display().”);
• value=25;//this will store 25 in value of class Two
• super.display();//now display of class One shall get invoked
• } I am going to use variable value and invoke method display().”);
• } I am member of class One.
Referring to Hidden Variable
• Superclass’s hidden variables are also can be referred by using keyword super., eg.,
super.hiddenVariable refers to variable hidden Variable of the superclass
• Eg., value=25 can be referred as super.value=25
Use of final
• The final variables are constants the cannot be changed
• The final methods cannot be overridden by subclasses
• A final class cannot be extended
Overriding(Points to Remember)