2. Contents
Introduction of inheritance
Classes classification
Super class
Intermediate class
Child class
Types of inheritance
Single inheritance
Multilevel inheritance
Hierarchical inheritance
Interface
Abstract class
Method overriding
3. Inheritance
• One of the most effective features of Object-
oriented programming.
• It is the capability of one class to inherit
properties from another class.
• Establish a link/connectivity between 2 or
more classes.
• Permits sharing and accessing properties
from one to another class.
• To establish this relation java uses “extends”
keyword.
• Represents “IS-A” relationship.
5. Category of classes on the basic of
inheritance
Super class
(base/parent/driver/inheritance class).
Intermediate class
(mediating/dual class).
Child class
(sub/associate/derived/inherited class).
6. 1. Super class
Top located class
Service provider
(its properties accessed by all its lower level
class).
2. Intermediate class
Middle located class
Having dual policy
(obtain properties of upper class and transmit
properties to lower level class)
3. Child class
Bottom located class
Much benefitted class
Much loaded class
(properties of child class as well as class and
parent class can be accessed by only the object of child class)
class A Parent
class
{
............
............
}
class B
Intermediate
{ class
...........
...........
}
class C
{
...........
...........
}
Child
class
7. Types of inheritance
Single inheritance
Multilevel inheritance
Hierarchical inheritance
8. Single inheritance
A structure having one and only one parent as well as child
class.
Child class is authorized to access the property of parent
class.
Syntax:-
class A
{
..............
..............
}
class B extends A
{
.............
.............
}
Parent
class
Child
class
Sharing
of
property
9. Multilevel inheritance
When a class extends a class, which extends another class then
this is called as multilevel inheritance.
Child class as well as intermediate class may access the properties
of upper level classes. Syntax:-
class A
{
..............
..............
}
class B extends A
{
.............
.............
}
class C extends B
{
..............
..............
}
Parent class
Intermediate
class
Child class
11. Hierarchical inheritance
When more than one classes are derived from a single base class,
such type of inheritance is called as hierarchical inheritance.
Child classes must be connected with
only parent class.
Syntax:-
class A
{
..............
..............
}
class B extends A
{
.............
.............
}
class C extends A
{
..............
..............
}
Parent
class
child class
child class
13. Interface
Java supports a special feature called
interface.
This feature helps to connect a class with
more than one classes
For this type of connectivity
java uses “implements” keyword.
Object of interface cannot be created.
Syntax:-
interface A {
..............
..............
}
interface B
{
.............
.............
}
class C implements A
{
..............
..............
}
class D implements A
{
..........
..........
}
interface
Child class
Intermediate
class
interface
Intermediate
class
14. Abstract class
A class that is declared with “abstract” keyword, is
known as abstract class in java.
It can have abstract and non abstract methods(method
with body).
No object of abstract class can be created.
Abstract class can contain constructor and instance
variable.
It contain a class with 0 to 100% abstraction.
Constructor chaining can be done in abstract class.
Syntax:
abstract class<class-name>{ }
15. //an abstract class without any abstract method
Abstract class Base
{
void fun()
{
system.out.println(“base fun() called”);
}
}
class Derived extends Base
{
}
class Main
{
public static void main(String args [])
{
Derived d= new Derived();
d.fun();
}
}
16. Difference between abstract
class &interface
Abstract class Interface
•It provides 0 to 100% abstraction
of methods.
It provides 100% abstraction of
methods.
•A class can inherit only one
abstract class at a time ( no
multiple inheritance).
A class can inherit many interface
at a time .
•Abstract class contain constructor
and instance variable.
Interface do not contain constructor
and instance variable.
•Constructor chaining will be done
in abstract class .
No constructor chaining will be
possible.
17. Method Overriding
If the subclass(child class) has the same
method as declared in the parent class, it
is called method overriding
Rules for method overriding in java
Method must have same name as in the
parent
class.
Method must have same parameter as in the
parent class.
Must be IS-A relationship(inheritance).
19. class Human
{
//overridden method
public void eat()
{
System.out.println(“human is eating”);
}
}
class Boy extends Human
{
//overriding method
public void eat()
{
System.out.println(“Boy is eating”);
}
public static void main(String args[])
{
Boy obj=new Boy();
//this will call the child class version of eat()
obj.eat();
}
}