0% found this document useful (0 votes)
25 views70 pages

Inheritance

Inheritance in Java allows one class to acquire properties and behaviors of another class. It establishes a parent-child relationship between classes, where the child class inherits from the parent class. This allows code reusability, where behaviors defined in the parent class can be used by all child classes. Child classes can override or extend the functionality inherited from the parent class. The "extends" keyword is used to establish inheritance between classes, while the "super" keyword allows child classes to call parent class constructors and access parent members.

Uploaded by

Sanyam Saini
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
25 views70 pages

Inheritance

Inheritance in Java allows one class to acquire properties and behaviors of another class. It establishes a parent-child relationship between classes, where the child class inherits from the parent class. This allows code reusability, where behaviors defined in the parent class can be used by all child classes. Child classes can override or extend the functionality inherited from the parent class. The "extends" keyword is used to establish inheritance between classes, while the "super" keyword allows child classes to call parent class constructors and access parent members.

Uploaded by

Sanyam Saini
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 70

INHERITANC

E
IN JAVA
What Is Inheritance?

• Inheritance in java is a
mechanism in which one object
acquires all the properties and
behaviors of parent object.
• Inheritance represents IS_A
relationship also known as parent
– child relationship.
Inheritance in Java can be best
understood in terms of Parent
and Child relationship, also
known as Super class(Parent)
and Sub class(child) in Java
language. Inheritance defines a
relationship between a Super
class and its Sub class. extends
and implements keywords are
used to describe inheritance in
Java
Why Inheritance? Reusability
Benefits of Inheritance in OOP : Reusability

– Once a behavior (method) is defined in a super class,
that behavior is automatically inherited by all subclasses
Thus, you write a method only once and it can be used by
all subclasses.

– Once a set of properties (fields) are defined in a super
class, the same set of properties are inherited by all
subclasses

A class and its children share common set of properties
– A subclass only needs to implement the differences
between itself and the parent.
How to derive a sub-class?

6
extends keyword
To derive a child class, we use the extends keyword. Suppose we
have a parent class called Person.
public class Person
{
protected String name;
protected String address;

// Default constructor
public Person(){
System.out.println(“Inside Person:Constructor”);
name = ""; address = "";
}
. . . .
}

7
extends keyword
• Now, we want to create another class named Student
• Since a student is also a person, we decide to just extend
the class Person, so that we can inherit all the properties and
methods of the existing class Person.
• To do this, we write,
public class Student extends Person
{
public Student()
{
System.out.println(“Inside
Student:Constructor”);
}
. . . .
}
9
What You Can Do in a Sub-class
• A subclass inherits all of the “public” and “protected”
members (fields or methods) of its parent, no matter what
package the subclass is in.
• If the subclass is in the same package as its parent, it also
inherits the package-private (i.e. default) members (fields or
methods) of the parent.
What Can not be Inherited
• Private members (fields or methods) of the parent,or super
class and cannot be directly accessed also.
• Constructor of the super class.

10
What You Can Do in a Sub-class Regarding Fields
• The inherited fields can be used directly, just like any other
fields.
• You can declare new fields in the subclass that are not in
the super class.
• You can declare a field in the subclass with the same name
as the one in the super class, thus hiding it (not
recommended).
• A subclass does not inherit the private members of its parent
class. However, if the super class has public or protected
methods for accessing its private fields, these can also be
used by the subclass.
11
What You Can Do in a Sub-class Regarding
Methods
• The inherited methods can be used directly as they are.
• You can write a new instance method in the subclass that has the
same signature as the one in the super class, thus overriding it.
• You can write a new static method in the subclass that has the
same signature as the one in the super class, thus hiding it.
• You can declare new methods in the subclass that are not in
the super class.

12
BASIC EXAMPLE OF
INHERITANCE IN JAVA
//Creating a SubClass class Main
{
class A public static
{ void main(String
int a=10, b=20;
args[])
void showab()
{ {
System.out.println(“a: “+a+ A obj1= new A();
“b: ”+b); obj1.showab();
} B obj2= new B();
obj2.sum();
} }
//Creating a SubClass by extending A }
class
class B extends A
{
void sum()
{
System.out.println(“Sum of a and
b is: ”+(a+b));
}
}
Output
Inheritance
• As you can see, the subclass B
include all the members of its
super class, A.
• This is why class B can access
“a” and “b” and call showab().
• Also, include sum(), “a” and
“b” can be referred directly, as
if they were part of B class.
• Even though A is super class
for B, it is also a completely
independent, stand alone
class.
TYPES OF
INHERITANCE
TYPES OF
INHERITANCE
Based on number of ways inheriting the feature of
base class into derived class we have some types of
inheritance; they are:
• Single inheritance
• Multilevel inheritance
• Hierarchical inheritance
TYPES OF INHERITANCE
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 be a child class of A
MULTILEVEL INHERITANCE
Here class C inherits class B
and class B inherits class A
which means B is a parent
class of C and A is a parent
class of B. So in this case class And Parent class of C
C is implicitly inheriting the
properties and method of class
A along with B that’s what is
called multilevel inheritance.
Hierarchical Inheritance
In this inheritance multiple classes inherits from a single class i.e
there is one super class and multiple sub classes. As we can see
from the below diagram when a same class is having more than
one sub class (or) more than one sub class has the same parent is
called as Hierarchical Inheritance.
Object Class

12
Object Class

Object class is mother of all classes
– In Java language, all classes are sub-classed (extended) from the
Object super class
– Object class is the only class that does not have a parent class
● Defines and implements behavior common to all classes
including the ones that you write
– getClass()
– equals()
– toString()
– ...

36
Class Hierarchy
● A sample class hierarchy

37
Constructor Calling
Chain

16
How Constructor method of a Super class gets called
• A subclass constructor invokes the constructor of the
super class implicitly
– When a Student object, a subclass (child class), is instantiated, the
default constructor of its super class (parent class), Person class, is
invoked implicitly before sub-class's constructor method is invoked
• A subclass constructor can invoke the constructor of the
super explicitly by using the “super” keyword
– The constructor of the Student class can explicitly invoke the
constructor of the Person class using “super” keyword
– Used when passing parameters to the constructor of the super class

17
Example: Constructor Calling Chain
● To illustrate this, consider the following code,
public static void main( String[] args )
{ Student anna = new Student();
}

In the code, we create an object of class Student. The
output of the program is,
Inside Person:Constructor
Inside Student:Constructor

40
Example: Constructor Calling Chain
The program flow is shown below.

41
“super” keyword

20
The “super” keyword
• A subclass can also explicitly
call a constructor of its
immediate super class.

• This is done by using the super


constructor call.
• A super constructor call in the
constructor of a subclass will
result in the execution of
relevant constructor from the
super class, based on the
arguments passed.
43
The “super” keyword
• Syntax for calling base class default
constructor
super()
It is used for calling base or parent class
default constructor from derived class
constructor.

• Syntax for calling base class


Parameterize Constructor
super(arg1, arg2,…, argN)
It is used for calling base or parent or
super class parameterize constructor from
derived class constructor.

44
The “super” keyword
Few things to remember when using the super constructor call:
– The super() call must occur as the first statement in a constructor
– The super() call can only be used in a constructor (not in ordinary methods)

45
The “super” keyword
Another use of super is to refer to
members of the super class (just
like the this reference ).
For example,
public Student()
{ super.name = “somename”;
super.address = “some
address”;
}

46
Overriding Methods

25
Overriding methods

If a derived class needs to have a different implementation
of a certain instance method from that of the super class,
override that instance method in the sub class
– Note that the scheme of overriding applies only to instance
methods
– For static methods, it is called hiding methods
● The overriding method has the same name, number and type
of parameters, and return type as the method it overrides
The overriding method can also return a subtype of the type

returned by the overridden method. This is called a covariant
return type
50
Example: Overriding Methods
Suppose we have the following
implementation for the getName
method in the Person super class,
public class Person {
:
:
public String getName()
{ System.out.println("
Parent: getName");
return name;
}
}

52
Example: Overriding Methods
To override the getName method of the super class Person in
the subclass Student, reimplement the method with the same
signature
public class Student extends Person{
:
public String getName()
{ System.out.println("Student: getName"); return
name;
}
:
}
Now, when we invoke the getName method of an object of the
subclass Student, the getName method of the Student would be
called, and the output would be,
Student: getName
53
Modifiers in the Overriding Methods
• The access specifier for an
overriding method can allow
more, but not less, access than
the overridden method
– For example, a protected instance
method in the super class can be
made public, but not private, in the
subclass.
• You will get a compile-time
error if you attempt to change
an instance method in the
super class to a class method
in the subclass, and vice versa
58
Runtime Polymorphism
with Method Overriding

30
Run – Time Polymorphism OR
Dynamic Method Dispatch
– The ability of a reference variable to change behavior
according to what object instance it is holding.
– It is a technique which enables us to assign the child
class object to a base class reference.
– This allows multiple objects of different subclasses to be
treated as objects of a single super class, while automatically
selecting the proper methods to apply to a particular object
based on the subclass it belongs to.

62
63
48
• Static methods are automatically final
66
Other examples of final classes are
your wrapper classes and String class
– You cannot create a subclass of
String class 67
49
THANK YOU
Any Suggestion Or Question Is Welcome

You might also like