Java Inheritance Explained: A Beginner's Guide to OOP Concepts
What is OOPs?
OOP stands for object oriented programming it is a programming paradigm based on the concept of objects. In this anything is treated as an object. Java is one of the programing language which uses object oriented programming.
Why we use OOPs concepts?
It is used to implement code reusability in an efficient way.
No code repetition.
Write code once and reuse in multiple places.
It improves performance.
Principles of OOPs
There are 4 OOPs principles
Inheritance
Encapsulation
Polymorphism
Abstraction
Detailed explanation about inheritance
Inheritance :
The process of acquiring properties from one class to another class is called inheritance. Inheritance can be achieved by using “extends” keyword. Only non static methods can be inherited, static methods cannot be inherited because we have only one copy of static members. During inheritance process constructor chaining is compulsory. Constructor chaining between super class and sub class happens in two ways.
Implicit way
Explicit way
Implicit way :
Incase super class contains default constructor compiler adds call to super statement without argument in subclass constructor. Hence constructor chaining is implicit.
Explicit way :
Incase super class contains constructor with arguments, developer adds call to super statement with arguments in subclass constructor. Hence constructor chaining is explicit.
The purpose of inheritance is to eliminate repetitive code between classes and achieve code reusability between the classes. Inheritance is also called as “IS-A” relation.
There are 5 types of inheritance
Single Inheritance
Multilevel Inheritance
Hierarchical Inheritance
Multiple Inheritance (Not possible)
Hybrid inheritance
Single Inheritance :
The process of inheriting the properties and behaviour of one super class to one sub class is called “Single Inheritance”.
Example of Single Inheritance :
// Base class
class Animal {
String name;
void eat() {
System.out.println(name + " is eating.");
}
}
// Derived class
class Cat extends Animal {
void meow() {
System.out.println(name + " says meow!");
}
public static void main(String[] args) {
Cat cat = new Cat();
cat.name = "Whiskers";
cat.eat();
cat.meow();
}
}:
Multilevel Inheritance :
A class can inherit from a superclass, and then another class can inherit from that subclass, creating a multilevel hierarchy. This process is called “Multiple Inheritance”. Multilevel inheritance helps in reusing code and establishing a clear relationship between classes.
Example for Multilevel Inheritance :
class Animal {
public void eat() {
System.out.println("This animal eats food.");
}
}
class Mammal extends Animal {
public void giveBirth() {
System.out.println("This mammal gives birth to live young.");
}
}
class Dog extends Mammal {
public void bark() {
System.out.println("The dog barks.");
}
}
public class Main {
public static void main(String[] args) {
Dog dog = new Dog();
dog.eat();
dog.giveBirth();
dog.bark();
}
}
Hierarchical Inheritance :
A single parent class is extended by multiple child classes is called as “Hierarchical Inheritance”. It allows the user to reuse the code and promotes modularity. It allows customisation of classes.
Example for Hierarchical Inheritance :
class ClassA {
public void displayA() {
System.out.println("This is ClassA");
}
}
class ClassB extends ClassA {
public void displayB() {
System.out.println("This is ClassB");
}
}
class ClassC extends ClassA {
public void displayC() {
System.out.println("This is ClassC");
}
}
public class HierarchicalInheritance {
public static void main(String[] args) {
ClassB objB = new ClassB();
objB.displayA();
objB.displayB();
ClassC objC = new ClassC();
objC.displayA();
objC.displayC();
}
}
Multiple Inheritance :
In java multiple Inheritance is not possible by using classes because of “Diamond Problem” i.e., ambiguity in constructor chaining and ambiguity in method execution, but we can achieve multiple Inheritance by using interfaces. An interface is similar to class,but its methods are abstract by default.
Example for Multiple Inheritance using interface :
interface Animal {
void eat();
}
interface Mammal {
void breathe();
}
class Bat implements Animal, Mammal {
@Override
public void eat() {
System.out.println("Bat eats insects.");
}
@Override
public void breathe() {
System.out.println("Bat breathes air.");
}
}
public class Main {
public static void main(String[] args) {
Bat bat = new Bat();
bat.eat();
bat.breathe();
}
}
Hybrid Inheritance :
The combination of single, Multilevel and Hierarchical Inheritance is called Hybrid inheritance. Hybrid Inheritance inherits the properties from all these inheritance.
Example for Hybrid Inheritance :
interface A {
void methodA();
}
interface B extends A {
void methodB();
}
interface C {
void methodC();
}
class D implements B, C {
public void methodA() {
System.out.println("Method A from interface A");
}
public void methodB() {
System.out.println("Method B from interface B");
}
public void methodC() {
System.out.println("Method C from interface C");
}
}
public class HybridInheritanceExample {
public static void main(String[] args) {
D objD = new D();
objD.methodA();
objD.methodB();
objD.methodC();
}
}
This article explores Object-Oriented Programming (OOP) concepts, focusing on principles like Inheritance, Encapsulation, Polymorphism, and Abstraction. Detailed explanations of inheritance types, including Single, Multilevel, Hierarchical, Multiple, and Hybrid inheritance, are provided. Java code examples illustrate these concepts, highlighting how OOP enables code reusability, modularity, and improved performance. The article also discusses how interfaces can achieve multiple inheritance, addressing Java's restrictions due to the "Diamond Problem."