Inheritance
- Acquiring the properties of one class to another is called inheritance.
- Inheritance is required to implement code reusability, IS-A relationship, and method overriding
(polymorphism).
- 'extends' keyword is used to implement inheritance.
Syntax:
class Child extends Parent
{
// body
}
- Super class = base class
- Sub class = derived class
- To access the properties of parent class, create an object only for child class.
Types of Inheritance:
1) Single level → Single parent, single child
Parent → Child
2) Multi level → One parent, multiple levels of children
Parent → Child1 → Child2 → Child3
3) Multiple Inheritance → Single child with multiple parents
(Not supported in Java directly)
4) Hybrid Inheritance → Combination of multiple and hierarchical
5) Hierarchical Inheritance → One parent, multiple children
Parent → Child1, Child2, Child3
Note:
- Object class of java.lang package is the parent class of all classes in Java.
- Java doesn’t support class-level multiple inheritance.
Example Programs:
1) Single level inheritance:
class Animal {
Animal() {
System.out.println("Parent class constructor");
}
void display() {
System.out.println("I am from Animal class");
}
}
class Dog extends Animal {
Dog() {
System.out.println("Child class constructor");
}
void bark() {
System.out.println("Dog is barking");
}
}
public class SLI_Demo {
public static void main(String[] args) {
Dog ob = new Dog();
ob.display();
ob.bark();
}
}
--------------------------------------------------
2) Multi level inheritance:
class Animal {
Animal() {
System.out.println("Parent class constructor");
}
void display() {
System.out.println("I am from Animal class");
}
}
class Dog extends Animal {
Dog() {
System.out.println("Child class constructor");
}
void bark() {
System.out.println("Dog is barking");
}
}
class BabyDog extends Dog {
BabyDog() {
System.out.println("Baby Dog constructor");
}
void weep() {
System.out.println("I am Baby Dog");
}
}
public class MLI_Demo {
public static void main(String[] args) {
BabyDog ob = new BabyDog();
ob.display();
ob.bark();
ob.weep();
}
}
--------------------------------------------------
3) Hierarchical inheritance:
class Animal {
void display() {
System.out.println("This is Animal class");
}
}
class Dog extends Animal {
void bark() {
System.out.println("Dog is barking");
}
}
class Cat extends Animal {
void meow() {
System.out.println("Cat is meowing");
}
}
public class HI_Demo {
public static void main(String[] args) {
Dog ob1 = new Dog();
Cat ob2 = new Cat();
ob1.display();
ob1.bark();
ob2.meow();
}
}
--------------------------------------------------
Super Keyword:
- Super keyword is used to refer immediate parent class members (constructors, methods,
variables).
class Parent {
int m;
Parent(int z) {
m = z;
}
}
class Child extends Parent {
int m;
Child(int x, int y) {
super(x);
m = y;
}
void display() {
System.out.println("m of Child: " + m);
System.out.println("m of Parent: " + super.m);
}
}
public class Demo {
public static void main(String[] args) {
Child ob = new Child(100, 200);
ob.display();
}
}
--------------------------------------------------
Method Overriding:
- Two methods with same name, same type of parameters and same number of parameters, one in
parent class and other in child class, is called method overriding.
- Method overriding refers to polymorphism (runtime).
- Overriding is preferred when we are not satisfied with parent class implementation.
Example:
class Father {
void property() {
System.out.println("I will give you money");
}
void marriage() {
System.out.println("You marry XYZ girl");
}
}
class Son extends Father {
void marriage() {
System.out.println("I will marry ABC girl");
}
}