What is Inheritance in Java ? | Different types of Inheritance

 


What is Inheritance? what are the different types of Inheritance?


Inheritance is a mechanism in object-oriented programming (OOP) that allows a new class to inherit properties and methods from an existing class. The existing class is called the base class, parent class, or superclass, and the new class is called the derived class, child class, or subclass.


Inheritance allows the derived class to inherit properties and methods from the base class, and also allows the derived class to add or override properties and methods. This means that the derived class can reuse the functionality of the base class without having to re-implement it.


The syntax for inheritance in Java is to use the keyword extends when defining the derived class, followed by the name of the base class.


There are several types of inheritance in object-oriented programming (OOP), including:


Single inheritance: A derived class inherits properties and methods from a single base class. This is the simplest and most common form of inheritance. 

For example, a "Dog" class can inherit properties and methods from an "Animal" class.


class Animals {
 
    private String name;
 
    private int age;
 
    public void speak() {
 
        System.out.println("I am an animal and I can speak");
 
    }
 
}
 
class Dog extends Animals {
 
    private boolean isTrained;
 
    public void bark() {
 
        System.out.println("Woof woof!");
 
    }
 
}




Multi-level inheritance: A derived class inherits properties and methods from a base class, which in turn inherits properties and methods from another base class. This creates a hierarchical relationship between classes. 

For example, a "Poodle" class can inherit properties and methods from a "Dog" class, which in turn inherits properties and methods from an "Animals" class.



class Animals {
 
    private String name;
 
    private int age;
 
    public void speak() {
 
        System.out.println("I am an animal and I can speak");
 
    }
 
}
 
class Dog extends Animals {
 
    private boolean isTrained;
 
    public void bark() {
 
        System.out.println("Woof woof!");
 
    }
 
}
 
class Poodle extends Dog {
 
    private String furColor;
 
    public void doTrick() {
 
        System.out.println("I can do a trick");
 
    }
 
}




Multiple inheritance: A derived class inherits properties and methods from multiple base classes. This is not supported in Java through classes but it is achievable through Interfaces.



Hierarchical Inheritance: When one class is inherited by multiple classes, it is called hierarchical inheritance. 

Example:


class Animals {
 
    private String name;
 
    private int age;
 
    public void speak() {
 
        System.out.println("I am an animal and I can speak");
 
    }
 
class Dog extends Animals {
 
    private boolean isTrained;
 
    public void bark() {
 
        System.out.println("Woof woof!");
 
    }
 
}
 
class Cat extends Animals {
 
    private String furColor;
 
    public void meow() {
 
        System.out.println("Meow meow!");
 
    }
 
}
 
class Lion extends Animals {
 
    private boolean isWild;
 
    public void roar() {
 
        System.out.println("Roar!");
 
    }
 
}


In this example, the class "Animals" is the base class that is inherited by multiple derived classes "Dog", "Cat" and "Lion". Each derived class has its own specific properties and methods that are not present in the base class.


Hybrid Inheritance: Combination of more than one type of inheritance.

It's important to note that Java doesn't support multiple inheritance, but it can be achieved by using interfaces.




In summary, inheritance is a mechanism in object-oriented programming that allows a derived class to inherit properties and methods from a base class. This allows for code reuse, encapsulation, and abstraction. 

There are several types of inheritance, including single inheritance, multi-level inheritance, multiple inheritance and hierarchical inheritance. Java does not support multiple inheritance, but it can be achieved through the use of interfaces.

Post a Comment

Post a Comment (0)

Previous Post Next Post