Inheritance: Object Oriented Programming Using JAVA
Inheritance: Object Oriented Programming Using JAVA
Inheritance: Object Oriented Programming Using JAVA
INHERITANCE
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
SubClass(B)
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)
Derived class(C)
class Animal
{
public void eat( )
{
System.out.println(“eating”);
}
}
Second prorgam
class Dog extends Animal
{
public void bark( )
{
System.out.println(“barking”);
}
}
JAVA Program to Implement Multilevel Inheritance
Class hutch extends Dog ruby.sniff( );
{ ruby.bark( );
public void sniff( ) ruby.eat( );
}
{ System.out.println(“sniffin }
g”); Output:
} Sniffing
}
Barking
Class Inherit
{ eating
3rd program
public static void main(String
args[])
{
hutch ruby = new hutch( );
Hierarchical Inheritance
The process of deriving multiple subclasses from
a single super or base class.
Class A
SubClass (C)
• 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.
class Multiple
{ 7th program
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.