Inheritance:
Inheritance in Java allows you to create a hierarchy of classes where a subclass inherits the properties and behaviors of a superclass.
Parent Class : The class from which properties are inherited is called Parent Class. We also call Parent class as Base or Super Class.
Child Class : The class which inherits the properties of parent class is called Child Class. Child Class is also called Sub Class or Derived Class.
Here is a basic layout, how inheritance is implemented. We have to write the keyword "extends" with base class and after that write the name of Parent Class.
class Animal {
String name;
public Animal(String name) {
this.name = name;
}
public void eat() {
System.out.println(name + " is eating.");
}
}
class Dog extends Animal {
public Dog(String name) {
// Call superclass constructor
super(name);
}
public void bark() {
System.out.println(name + " is barking.");
}
}
public class Main {
public static void main(String[] args) {
Dog dog = new Dog("Buddy");
dog.eat(); // Inherited from Animal
dog.bark(); // Specific to Dog
}
}
Protected Access Modifier:
The protected members are not accessible outside the class. They are only accessible only in child class.// Superclass
class Animal {
protected String name; // Protected field
public Animal(String name) {
this.name = name;
}
protected void makeSound() {
System.out.print(name+" makes sound.");
}
}
// Subclass
class Dog extends Animal {
public Dog(String name) {
super(name);
}
public void bark() {
System.out.println(name + " barks.");
}
public void interact() {
// Accessing protected method
makeSound();
}
}
public class Main {
public static void main(String[] args) {
Dog dog = new Dog("Buddy");
// Accessing the protected field
//and method from the subclass
System.out.println("Name: " + dog.name);
dog.makeSound();
// Using a method in the subclass
//that calls the protected method
dog.interact();
}
}
Levels of Inheritance:
- Single level
- Multi-level
- Hierarchical
- Multiple
- Hybrid
Single level Inheritance:
When one child class inherits attributes or properties or both from a parent class, this type of inheritance is called single level inheritance.// Superclass (Base Class)
class Vehicle {
String brand;
int year;
public Vehicle(String brand, int year) {
this.brand = brand;
this.year = year;
}
public void start() {
System.out.println("Starting the vehicle.");
}
public void stop() {
System.out.println("Stopping the vehicle.");
}
}
// Subclass (Derived Class)
class Car extends Vehicle {
int doors;
public Car(String brand, int year, int doors)
{
// Call the superclass constructor
super(brand, year);
this.doors = doors;
}
public void accelerate() {
System.out.println("Car is accelerating.");
}
public void brake() {
System.out.println("Car is braking.");
}
}
public class Main {
public static void main(String[] args) {
Car car = new Car("Toyota", 2022, 4);
// Access fields and methods
System.out.println("Brand: " + car.brand);
System.out.println("Year: " + car.year);
car.start();
car.stop();
// Access specific fields and methods
System.out.println("Doors: " + car.doors);
car.accelerate();
car.brake();
}
}
Multi-level Inheritance:
In multi-level inheritance, we have multiple levels of inheritance. Multilevel inheritance occurs when a subclass is derived from another subclass.class Animal {
String name;
public Animal(String name) {
this.name = name;
}
public void eat() {
System.out.println(name + " is eating.");
}
}
class Mammal extends Animal {
public Mammal(String name) {
super(name);
}
public void run() {
System.out.println(name + " is running.");
}
}
class Dog extends Mammal {
public Dog(String name) {
super(name);
}
public void bark() {
System.out.println(name + " is barking.");
}
}
public class Main {
public static void main(String[] args) {
Dog dog = new Dog("Buddy");
dog.eat(); // Inherited from Animal
dog.run(); // Inherited from Mammal
dog.bark(); // Specific to Dog
}
}
Hierarchical Inheritance:
In Hierarchical inheritance, we have multiple child classes and a single parent class.class Animal {
String name;
public Animal(String name) {
this.name = name;
}
public void eat() {
System.out.println(name + " is eating.");
}
}
class Dog extends Animal {
public Dog(String name) {
super(name);
}
public void bark() {
System.out.println(name + " is barking.");
}
}
class Cat extends Animal {
public Cat(String name) {
super(name);
}
public void meow() {
System.out.println(name + " is meowing.");
}
}
public class Main {
public static void main(String[] args) {
Dog dog = new Dog("Buddy");
Cat myCat = new Cat("Whiskers");
dog.eat(); // Inherited from Animal
dog.bark(); // Specific to Dog
myCat.eat(); // Inherited from Animal
myCat.meow(); // Specific to Cat
}
}
In Java, we can not implement multiple and hybrid inheritance directly. We can achieve these concepts using interfaces.
Note : Inheritance allow the creation of a class hierarchy, allowing a subclass to inherit the properties and behaviors of a superclass. There are two main classes involved:
Parent Class (Superclass or Base Class) : The class whose properties are inherited.
Child Class (Subclass or Derived Class) : The class that inherits the properties of the base class.