Java 2nd Chapter
Java 2nd Chapter
Syntax:
Class A
{
Block of code
}
Class B extends A
{
Block of code
}
Sample program for single level inheritance:
public class A
UNIT-2
Inheritance, Overriding, Interfaces and Packages
{
public void display()
{
System.out.println("I am from class A");
}
}
// B is inheriting display method of A
class B extends A
{
public void print()
{
System.out.println("I am from class B");
}
public static void main(String[] args) {
B objB = new B();
objB.display();
// Reusing the method of A named display
objB.print();
}
}
Multi-level inheritance:
In multi-level inheritance we have only one super class and multiple sub classes. The multi-level inheritance
includes the involvement of at least two or more than two classes.
Syntax:
Class A
UNIT-2
Inheritance, Overriding, Interfaces and Packages
{
}
Class B extends A
{
}
Class C extends B
{
}
Sample programm for multi-level inheritance:
class Animal
{
void eat()
{
System.out.println("eating...");
}
}
class Dog extends Animal
{
void bark()
{
System.out.println("barking...");
}
}
class BabyDog extends Dog
{
void weep()
{
System.out.println("weeping...");
}
}
class TestInheritance2
UNIT-2
Inheritance, Overriding, Interfaces and Packages
{
public static void main(String args[])
{
BabyDog d=new BabyDog();
d.weep();
d.bark();
d.eat();
}
}
Hierarchical inheritance:
An inheritance which contain only one super class and multiple sub classes and all sub classes directly extends
super class called hierarchical inheritance.
Syntax:
class A
{
}
Class B extends A
{
}
Class c extends A
{
}
Sample program for hierarchical inheritance:
Class A
{
UNIT-2
Inheritance, Overriding, Interfaces and Packages
void input()
{
System.out.println(“Enter your name:”);
}
}
Class B extends A
{
void show()
{
System.out.println(“My name is puneeth”);
}
}
Class C extends A
{
void disp()
{
System.out.println(“My name is chowhan”);
}
}
Class demo
{
Public static void main(String[] args)
{
B r=new B();
C r2=new C();
r.input(); r.show();
r2.input(); r2.disp();
}
}
Multiple inheritance:
UNIT-2
Inheritance, Overriding, Interfaces and Packages
The multiple inheritance is having capability of creating a single class with multiple super classes. JAVA
doesn’t support multiple inheritance but we can achieve this through “interfaces” because interface contains
only abstract method, which implementation is provided by the sub classes.
Note: Class C extends A, B (A, B are super classes we can’t extend them)
Class C implements A, B (by using implement we can access the properties of A, B to C)
Syntax: Syntax:
Class A Interface A
{
{
}
}
Class B
Interface B
{
{
}
}
Class C extends A, B
Class C implements A, B
{
{
}
}
(This is not allowed in java)
Sample program for multiple intheritance:
interface AnimalEat
{
void eat();
}
interface AnimalTravel
{
void travel();
}
class Animal implements AnimalEat, AnimalTravel
{
UNIT-2
Inheritance, Overriding, Interfaces and Packages
public void eat()
{
System.out.println("Animal is eating");
}
public void travel()
{
System.out.println("Animal is travelling");
}
}
public class Demo
{
public static void main(String args[])
{
Animal a = new Animal();
a.eat(
a.travel();
}
}
Hybrid inheritance:
The hybrid inheritance is the composition of two or more types of inheritance. The main purpose of using hybrid
inheritance is to modularize the code into well-defined classes. It also provides the code reusability. The hybrid
inheritance can be achieved by using the following combinations:
Single and Multiple Inheritance (not supported but can be achieved through interface)
Multilevel and Hierarchical Inheritance
Hierarchical and Single Inheritance
Multiple and Multilevel Inheritance