Unit-4
NKOCET, Solapur Prof. Shinde Reshma U.
Contents of unit4:
Points to cover:
Inheritance
• Super class and subclass
• Protected members
• Relation between sunclass and superclass
• Constructor in subclass
• Object class
Polymorphism
• Abstract class and method
• Final method ands classes
• Polymorphism examples and inheritancs
Inheritance:
Introduction:
• Inheritance represent reusability feature of OOPs which help us to reuse
properties of existing class into new class without redefining.
• It is a mechanism in which one object acquires all the properties and
behaviour of parent object with adding its own properties.
• Reusability because in inheritance we create new class upon the existing
class
• New class is called as subclass or child class or derived class and existing
class is called as super class or parent class or base class
• Here in diagram we can see that class B has
been derived /inherited from class A. So class A
is base class and Class B is derived class
Inheritance contin…
How to inherit class in java?
• Consider parent class named as base-class and we want to inherit child
class named as derived-class, then we have to use following syntax to
achieve inheritance
• Syntax:
• As we have inherited derived-class from base-class, Object of derived-
class can access properties of both classes
• Properties of class can be either variables or method of the class
Inheritance contin…
• In java inheritance is an is-a relationship. That is we use inhrtitance only
if there exists an is-a relationship between teo classes.
• For example:
– Car is vehical
– Dog is an animal
– Mango is a fruit
– Student is a member of college
• So in above examples car, Dog, Mango, Student are derived classes
whereas vehical, animal, fruit, college are baase classes
Inheritance contin…
Why Do We Need Java Inheritance?
1. Code Reusability: The code written in the Superclass is common to all
subclasses. Child classes can directly use the parent class code.
2. Method Overriding: Method Overriding is achievable only through
Inheritance. It is one of the ways by which Java achieves Run Time
Polymorphism.
3. Abstraction: The concept of abstract where we do not have to provide
all details is achieved through inheritance. Abstraction only shows the
functionality to the user.
NKOCET, Solapur Prof. Shinde Reshma U.
Inheritance contin…
• Program for Inheritance:
Inheritance contin…
• Basic Program for Inheritance:
Inheritance contin…
Types of inheritance:
There are three types of inheritance in java:
• Single inheritance
• Multilevel inheritance
• Hierarchical inheritance
• Multiple inheritance // is not supported by java
• Hybrid inheritance //not supported by java
Inheritance contin…
1. Single Inheritance:
• In single inheritance, subclasses inherit the features of one superclass.
• In this type there will be only one child class and only one parent class
• In the image below, class A serves as a base class for the derived class
B.
• Syntax:
Inheritance contin…
2. Multilevel inheritance:
• In Multilevel Inheritance a subclass extends from a superclass and then
the same subclass acts as a superclass for another class
• In the below image, class A serves as a base class for the derived class B,
and then B serves as a base class for the derived class C.
• Syntax:
Inheritance contin…
3. Hierarchical inheritance:
• In Hierarchical Inheritance, one class serves as a superclass (base class)
for more than one subclass.
• In the below image, class A serves as a base class for the derived classes
B and C
• Syntax:
Inheritance contin…
4. Multiple inheritance:
• In Multiple inheritances, one class can have more than one superclass
and inherit features from all parent classes.
• “Note that Java does not support multiple inheritances with classes. “
• To reduce the complexity and simplify the language, multiple
inheritance is not supported in java.
• Consider the C class inherits A and B classes. If A and B classes have the
same method and you call it from child class object, there will be
ambiguity to call the method of A or B class.
• Still In Java, we can achieve multiple inheritances only through
Interfaces.
Inheritance contin…
5. Hybrid inheritance:
• It is a mix of two or more of the above types of inheritance. Since Java
doesn’t support multiple inheritances with classes, hybrid inheritance
involving multiple inheritance is also not possible with classes.
• In Java, we can achieve hybrid inheritance only through Interfaces if we
want to involve multiple inheritance to implement Hybrid inheritance.
Inheritance contin…
5. Hybrid inheritance conti…
• However, it is important to note that Hybrid inheritance does not
necessarily require the use of Multiple Inheritance exclusively.
• It can be achieved through a combination of Multilevel Inheritance and
Hierarchical Inheritance with classes, Hierarchical and Single Inheritance
with classes as mentioned below.
Protected members in inheritance:
• In Java, there are four types of access modifiers which are public,
private, default, and protected.
• Lets understand if base class is having protected property, then how
many ways we can implement inheritance so that derived class can
access that specificproperty.
various scenarios of accessing protected members which are listed below
as follows:
• Accessing in the same class (Not the case of inheritance) (Accessible)
• Accessing in other classes of the same package (Accessible)
• Accessing protected members of a class in its subclass in the same
package (Accessible)
• Accessing in sub-class in a different package (Accessible)
• Accessing another class in a different package (Not the case of
inheritance) (Not Acessible)
Protected members in inheritance:
1. Accessing in the same class
– We can access protected members of a class anywhere in the same
class.
– Here protected member ‘a’ have been used by Display() and main()
in the same class ‘A1’
Protected members in inheritance contin..
2. Accessing in other classes of the same java file(package)
– We can access protected data of a baseclass in subclass present in
the same java file
– Here subclass A have used member ‘var1’ of superclass ‘A’ . Both
subclass and superclass are in the same file
Protected members in inheritance contin..
3. Accessing protected members of a class in its subclass in the same
java package
– This is the case of inheritance.
– We can access protected members of a superclass in its subclass if
both are different files but present in the same package.
– In following case both A.java and B.java are in same folder
Protected members in inheritance contin..
4. Accessing in sub-class in a different package
– We can access protected members of a superclass in its subclass
present in a different package.
– Here A.java and B.java are present in different folders pack1 and
pack2 respectively.
Protected members in inheritance contin..
5. Accessing in another class in a different package
– We cannot access the protected members of a class in a class (non-
subclass) that is present in a different package.
– When we compile B.java
File it will give error as
Constructors in inheritance:
• We know that every class has its own constructors which get executed
as soon as its object get created.
• But in inheritance concept where we do not prefer to create object of
base class, how base class constructor gets executed??
• Java has provision to call baseclass constructor through derived class
constructor using super() keyword as shown below.
• In each type of inheritance base class constructor is executed before the
derived class constructor
• When we create object of subclass, subclass constructor first calls
baseclass constructor with super() then its own constructor body get
executed.
Constructors in inheritance:
• See the following program where class A is base class and class B is derived
from A.
• In main() function we have created object of class B. Hence that object is
responsible for calling B() constructor. Here B() constructor is responsible for
calling A() constructor.
• “Hence object in main() first calls to derived class constructor, then
derived class constructor calls base class constructor. Base class
constructor get executes and then derived class constructor get executes”
In inheritance:
• Order of Calling constructor:
Derived Base
• Order of execution of constructor:
Base Derived
Constructors in inheritance contin…
Constructor in Single inher. constructor in multilevel inher.
Output:
Parameterized Constructors in inheritance:
How to pass arguments to base class constructor which has parameters?
• Steps to follow to pass argument to base class constructor:
1. In this case we pass the argument to derived class constructor while
creating derived class object.
2. Then derived class constructor take that value and passes to super()
keyword.
3. Super() keyword send that argument to base class constructor
Parameterized Constructors in inheritance:
• If base class constructor is parameterized, then we pass its value using
super keyword as follows: