Object Oriented Programming Using JAVA
INHERITANCE
A PowerPoint Presentation
By
T. PARTHASARADHI
OOPS Concepts using JAVA
OOPs is an Object Oriented programming Paradigms.
It is the concept of Programming Language like C#,
Java using real world entity.
They are:
• Objects • Class
• Abstraction • Encapsulation
• Inheritance • Polymorphism
• Dynamic Binding • Message Communication
What is an Inheritance
Deriving the properties of a new class using an
existing class is known as Inheritance.
• Subclass:- The class which acquires the properties
of an existing class is known as a subclass or a
derived class or a child class.
• Superclass:- The class whose properties are
acquired by a new class is known as a superclass,
base class or a parent class.
What is an Inheritance
Defining a subclass:- The keyword extends is used
to define a subclass.
Class SubclassName extends SuperclassName
{
variable declaration;
method declaration;
}
• Keyword extends is used to acquire the properties of a
superclass into a subclass.
• Now this subclass will contain its own variables and
methods as well as the variables and methods of a
superclass.
Types of Inheritance
JAVA permits only 5 types of Inheritance.
They are:
• Single Inheritance
• Multilevel Inheritance
• Hierarchical Inheritance
• Multiple Inheritance
• Hybrid Inheritance
Single Inheritance
When a Subclass is derived only from a single
Superclass, then it is known as single inheritance.
SuperClass(A)
SubClass(B)
• Here A is a superclass or Parent class and B is a
Subclass or child class.
• So B is a subclass derived from a single super class A.
JAVA Program to Implement Single Inheritance
class A
{
public void methodA( )
{
System.out.println(“I am from class A”);
}
}
class B extends A
{
public void methodB( )
{
System.out.println(“I am from class B”);
}
}
class SingleInherit
{
public static void main(String args[])
{
B objb = new B( );
Objb.methodA( );
Objb.methodB( );
}
}
OUTPUT:
I am from class A
I am from class B
Multilevel Inheritance
The process of deriving the new class from a
derived class is known as multilevel inheritance.
Base class(A)
intermediate base class(B)
Derived class(C)
Here C is a Subclass derived from B which is
derived from superclass A.
JAVA Program to Implement Multilevel Inheritance
class Animal
{
public void eat( )
{
System.out.println(“eating”);
}
}
class Dog extends Animal
{
public void bark( )
{
System.out.println(“barking”);
}
}
JAVA Program to Implement Multilevel Inheritance
Class hutch extends Dog Output:
{ Sniffing
public void sniff( ) Barking
{
System.out.println(“sniffing”); eating
}
}
Class Inherit
{
public static void main(String
args[])
{
hutch ruby = new hutch( );
ruby.sniff( );
ruby.bark( );
ruby.eat( );
}
}
Hierarchical Inheritance
The process of deriving multiple subclasses from
a single super or base class.
Class A
Class B Class C Class D
• Here the class B, C and D are the derived classes
from a single super class A.
• These three classes aquries the properities of the
class A.
JAVA Program to Implement Hierarchical Inheritance
class Animal
{
void eat()
{
System.out.println("eating...");
}
}
class Dog extends Animal
{
void bark()
{
System.out.println("barking...");
}
}
class Cat extends Animal
{
void meow( )
{
System.out.println("meowing...");
}
}
JAVA Program to Implement Hierarchical Inheritance
class Cheeta extends Animal
{
void chase()
{
System.out.println(“chasing...");
}
}
class hierarchical
{
public static void main(String args[ ])
{
Cat c=new Cat();
c.meow();
c.eat();
}
}
Output:-
meowing...
eating…
Multiple Inheritance
The process of deriving a subclass from more than one
superclass is known as multiple inheritance.
SuperClass (A) SuperClass (B)
SubClass (C)
• Multiple inheritance is not supported in java,as to reduce the complexity
and simplify the language.
• As C is a subclass derived from the superclasses A and B and having same
method names if I call it by subclass C then there will be ambiguity to call
the method of A or B class.
• But with the help of key word interface it can be achived.
Keyword Interface and its properities.
So, interface is a keyword to achive multiple inheritance
in java. lets have a small look over keyword interface.
JAVA Program to Implement Multiple Inheritance
class Kfc
{
public void friedChicken( )
{
System.out.println(“fried chicken is prepared.”);
}
}
interface Afc
{
public void friedChicken( )
{
system.out.println(“fried chicken is prepared.”);
}
}
JAVA Program to Implement Multiple Inheritance
class Customer extends Kfc implements Afc
{
Void eat( )
{
System.out.println(“eating fried chicken”);
}
}
class Multiple
{
Public static void main(String args[])
{
Customer c = new Customer( );
c.friedChicken( );
c.eat( );
}
}
Output:-
fried chicken is prepared
eating fried chicken
Advantages and Inheritance
• The main advantage of the inheritance is that it helps
in reusability of the code.
• It improves the program structure which can be
readable.
• Inheritance makes the application code more flexible
to change.
• The program structure is short and concise which is
more reliable.
Disadvantage of inheritance
• The main disadvantage of the inheritance is that the two
classes are dependent on each other.
• If the functionality of the base class is changed then the
changes have to be done on the child classes also.
• It increases the time and efforts take to jump through
different levels of the inheritance.
• If the methods in the super class are deleted then it is very
difficult to maintain the functionality of the child class.