Inheritance
In the case of humans, just like a child inherits some properties and behaviour from its parent, similarly in Java one class can inherit the properties and behaviour from another class. So here we have a parent class and a child class.
The child or subclass class inherits the properties(variables) and behaviour(methods) from its parent class or super class.
We use extends keyword to inherit a class.
So here we majorly have 3 benefits.
- Code reuse
- Method enhancement
- Organized Code Structure
1. Code reuse:
Here Instead of writing the same code again and again, a new class can reuse the existing code.
Example:
// Parent class
class Animal {
String name;
int age;
void displayInfo() {
System.out.println("Name: " + name);
System.out.println("Age: " + age);
}
}
// Child class reusing variables and method from Animal
class Dog extends Animal {
//So here in Dog class we have not declared variables and methods of Animal class,
//it directly inherits from its parent class.
void bark() {
System.out.println(name + " is barking");
}
}
public class HelloWorld {
public static void main(String[] args) {
Dog d = new Dog();
// Reusing variables from parent class
d.name = "Tommy";
d.age = 3;
// Reusing method from parent class
d.displayInfo();
// Child class method
d.bark();
}
}2. Method enhancement:
Here we can modify or enhance the existing method of the parent class in the child class.
Example:
// Parent class
class Payment {
//This method only process the amount.
void processPayment(double amount) {
System.out.println("Processing payment of Rs. " + amount);
}
}
// Child class enhancing the method of the parent class
class UpiPayment extends Payment {
//Now this method enhanced with some cashback implementation.
@Override
void processPayment(double amount) {
// basic payment logic from parent
super.processPayment(amount);
// enhanced logic in child class
double cashback = amount * 0.02;
double finalAmount = amount - cashback;
System.out.println("UPI Cashback applied: Rs. " + cashback);
System.out.println("Final amount after cashback: Rs. " + finalAmount);
}
}
public class HelloWorld {
public static void main(String[] args) {
UpiPayment upi = new UpiPayment();
upi.processPayment(1000);
}
}3. Organized Code Structure:
From the example, we can understand how it helps in organizing the code.
Example:
Without Inheritance
class Dog {
void eat() {
System.out.println("Eating food");
}
void bark() {
System.out.println("Barking");
}
}
class Cat {
void eat() {
System.out.println("Eating food");
}
void meow() {
System.out.println("Meowing");
}
}Here eat() is repeated in both classes. Code is duplicated and not well organized
Example:
With Inheritance
class Animal {
void eat() {
System.out.println("Eating food");
}
}
class Dog extends Animal {
void bark() {
System.out.println("Barking");
}
}
class Cat extends Animal {
void meow() {
System.out.println("Meowing");
}
}So here Common method (eat) is placed in the parent class Animal,
Specific methods are kept in child classes
Code is organized properly in a hierarchy
Inheritance Types In Java
- Single Inheritance
- Multilevel Inheritance
- Hierarchical Inheritance
- Multiple Inheritance
- Hybrid Inheritance
1. Single Inheritance (Simple Inheritance):
In single Inheritance, we have one parent (superclass) and one direct child (subclass).
Example:
class Animal {
void eat() {
System.out.println("Animal is eating");
}
}
class Dog extends Animal {
void bark() {
System.out.println("Dog is barking");
}
}
public class HelloWorld {
public static void main(String[] args) {
Dog d = new Dog();
d.eat(); // inherited method
d.bark(); // current class method
}
}2. Multilevel Inheritance:
In multilevel inheritance, a subclass inherits from a parent class, and another subclass inherits from that subclass. For example, if class B inherits from class A, and class C inherits from class B, then class C indirectly inherits from class A through class B.
Example:
class Animal {
void eat() {
System.out.println("Animal eats food");
}
}
class Dog extends Animal {
void bark() {
System.out.println("Dog barks");
}
}
class Puppy extends Dog {
void weep() {
System.out.println("Puppy weeps");
}
}
public class HelloWorld {
public static void main(String[] args) {
Puppy p = new Puppy();
p.eat();
p.bark();
p.weep();
}
}
//Animal
// |
// Dog
// |
// Puppy
//output
//Animal eats food
//Dog barks
//Puppy weeps3. Hierarchical Inheritance:
In Hierarchical Inheritance, there is only one parent class and multiple, or we can say more than one, subclasses inherit from that parent class. For example, A is a parent class, and classes B, C, and D inherit from class A.
Example:
class Animal {
void eat() {
System.out.println("Animal eats");
}
}
class Dog extends Animal {
void bark() {
System.out.println("Dog barks");
}
}
class Cat extends Animal {
void meow() {
System.out.println("Cat meows");
}
}
public class HelloWorld {
public static void main(String[] args) {
Dog d = new Dog();
d.eat();
d.bark();
Cat c = new Cat();
c.eat();
c.meow();
}
}
// Animal
// / \
// Dog Cat
//output
//Animal eats
//Dog barks
//Animal eats
//Cat meows4. Multiple Inheritance:
In Multiple Inheritance ther will be only one subclass and it inherits from multiple or more than one parent class. For example, if classes A and B are parent classes, and class C inherits from both class A and class B.
Note: Java does not support multiple inheritance using classes because it can create confusion when the same method is inherited from multiple parent classes. However, Java supports multiple inheritance through interfaces.
Why Java Does Not Support Multiple Inheritance:
Example:
class A {
void show() {
System.out.println("A");
}
}
class B {
void show() {
System.out.println("B");
}
}
// ERROR
class C extends A, B {
}Here, class A and class B both have the same method, show(). When a subclass inherits from both class A and class B, there is confusion about which show() method should be inherited , whether from class A or class B.
This confusion is called ambiguity (method ambiguity), also known as the Diamond Problem.
Like method ambiguity, there is also a problem of constructor ambiguity. As we know, when a subclass object is created, the constructors of its parent classes are automatically called using super().
Example:
class A {
A() {
System.out.println("Constructor A");
}
}
class B {
B() {
System.out.println("Constructor B");
}
}
class C extends A, B {
}Now, when you create an object of class C, Java gets confused: should it call the constructor of class A first, or class B first, or in what order should both be called?"
Because of these ambiguity issues or diamond problem, Java does not support multiple inheritance through classes
But this problem is resolved in the case of Java Interfaces.
5. Hybrid Inheritance:
Hybrid Inheritance, as the name suggests, is when we combine two or more types of inheritance. This is called Hybrid Inheritance.
For example, if class A is a parent class and class B and C inherit from class A (hierarchical inheritance), and then class D inherits from class B (multilevel inheritance), then this combination of different types of inheritance is called Hybrid Inheritance.
Example: single + multilevel + hierarchical
class A {
void showA() {
System.out.println("Class A method");
}
}
// Hierarchical Inheritance
class B extends A {
void showB() {
System.out.println("Class B method");
}
}
class C extends A {
void showC() {
System.out.println("Class C method");
}
}
// Multilevel Inheritance
class D extends B {
void showD() {
System.out.println("Class D method");
}
}
public class HelloWorld {
public static void main(String[] args) {
D obj = new D();
obj.showA(); // from A
obj.showB(); // from B
obj.showD(); // from D
}
}Constructor chaining
As we know, when there are multiple constructors, we can call one constructor from another constructor, which is known as constructor chaining. Similar to constructor chaining within the same class, It is also possible to perform constructor chaining between a parent class and a child class.
Similarly, just as we use the this keyword to call a constructor within the same class, we use the super keyword to call a constructor from the parent class in a child class. And super() must be the first statement in a constructor.
Example:
class Animal {
Animal() {
System.out.println("Animal constructor");
}
}
class Dog extends Animal {
Dog() {
super(); // Calls Animal constructor
System.out.println("Dog constructor");
}
}
class Puppy extends Dog {
Puppy() {
super(); // Calls Dog constructor
System.out.println("Puppy constructor");
}
}
public class HelloWorld {
public static void main(String[] args) {
Puppy p = new Puppy();
//output:
// Animal constructor
//Dog constructor
//Puppy constructor
}
}See you in next page....🙂