Java Inheritance

Inheritance in Java

What is Inheritance?

Inheritance is a fundamental concept in object-oriented programming (OOP). It allows one class to inherit the properties and behaviors of another class, creating a parent-child relationship between the classes.

How Does It Work?

In Java, you can create a base class (or parent class) with common features. Other classes, known as derived classes (or child classes), can inherit and use these features.

Why Use Inheritance?

  1. Reusability: Use the same code in different places.
  2. Organization: Group related features together.
  3. Specialization: Create specialized versions of a class.

Commonly used Inheritance


1. Single Inheritance: A class inherits from one base class.

Single Inheritance Example:

public class Animal {
    public void eat() {
        System.out.println(“Animal is eating.”);
    }
}
public class Dog extends Animal {
    public void bark() {
        System.out.println(“Dog is barking.”);
    }
}

Explanation:

public class Animal {

    // This line defines a new class called ‘Animal’.
    // A class is like a blueprint for creating objects with specific features and behaviors.

public void eat() {

        // This line defines a method called ‘eat’ inside the ‘Animal’ class.
        // A method is a block of code that performs a specific task.

System.out.println(“Animal is eating.”);

        // This line prints the text “Animal is eating.” to the console.

public class Dog extends Animal {

    // This line defines a new class called ‘Dog’ that inherits from the ‘Animal’ class by using the ‘extends’ keyword.
    // Inheriting means ‘Dog’ will have all the features of ‘Animal’ plus its own features.

public void bark() {

        // This line defines a method called ‘bark’ inside the ‘Dog’ class.
        // The ‘bark’ method is specific to the ‘Dog’ class.

System.out.println(“Dog is barking.”);

        // This line prints the text “Dog is barking.” to the console.

2. Hierarchical Inheritance: Multiple classes inherit from one base class

Hierarchical Inheritance Example:

public class Animal {
    public void eat() {
        System.out.println(“Animal is eating.”);
    }
}
public class Dog extends Animal {
    public void bark() {
        System.out.println(“Dog is barking.”);
    }
}
public class Cat extends Animal {
    public void meow() {
        System.out.println(“Cat is meowing.”);
    }
}

Explanation:

public class Animal {

    // This line defines a new class called ‘Animal’.
    // A class is like a blueprint for creating objects with specific features and behaviors.

public void eat() {

        // This line defines a method called ‘eat’ inside the ‘Animal’ class.
        // A method is a block of code that performs a specific task.

System.out.println(“Animal is eating.”);

        // This line prints the text “Animal is eating.” to the console.

public class Dog extends Animal {

    // This line defines a new class called ‘Dog’ that inherits from the ‘Animal’ class by using the ‘extends’ keyword.
    // Inheriting means ‘Dog’ will have all the features of ‘Animal’ plus its own features.

public void bark() {

        // This line defines a method called ‘bark’ inside the ‘Dog’ class.
        // The ‘bark’ method is specific to the ‘Dog’ class.

System.out.println(“Dog is barking.”);

        // This line prints the text “Dog is barking.” to the console.

public class Cat extends Animal {

    // This line defines a new class called ‘Cat’ that also inherits from the ‘Animal’ class by using the ‘extends’ keyword.
    // This means ‘Cat’ will have all the features of ‘Animal’ plus its own features.

public void meow() {

        // This line defines a method called ‘meow’ inside the ‘Cat’ class.
        // The ‘meow’ method is specific to the ‘Cat’ class.

System.out.println(“Cat is meowing.”);

        // This line prints the text “Cat is meowing.” to the console.

Multilevel Inheritance: A class inherits from a derived class.

Multilevel Inheritance Example:

public class Animal {
    public void eat() {
        System.out.println(“Animal is eating.”);
    }
}
public class Mammal extends Animal {
    public void breathe() {
        System.out.println(“Mammal is breathing.”);
    }
}
public class Dog extends Mammal {
    public void bark() {
        System.out.println(“Dog is barking.”);
    }
}

Explanation:

public class Animal {

    // This line defines a new class called ‘Animal’.
    // A class is like a blueprint for creating objects with specific features and behaviors.

public void eat() {

        // This line defines a method called ‘eat’ inside the ‘Animal’ class.
        // A method is a block of code that performs a specific task.

System.out.println(“Animal is eating.”);

        // This line prints the text “Animal is eating.” to the console.

public class Mammal extends Animal {

    // This line defines a new class called ‘Mammal’ that inherits from the ‘Animal’ class by using the ‘extends’ keyword.
    // Inheriting means ‘Mammal’ will have all the features of ‘Animal’ plus its own features.

public void breathe() {

        // This line defines a method called ‘breathe’ inside the ‘Mammal’ class.
        // The ‘breathe’ method is specific to the ‘Mammal’ class.

System.out.println(“Mammal is breathing.”);

        // This line prints the text “Mammal is breathing.” to the console.

public class Dog extends Mammal {

    // This line defines a new class called ‘Dog’ that inherits from the ‘Mammal’ class by using the ‘extends’ keyword.
    // Inheriting means ‘Dog’ will have all the features of ‘Mammal’ plus its own features.

    public void bark() {

        // This line defines a method called ‘bark’ inside the ‘Dog’ class.
        // The ‘bark’ method is specific to the ‘Dog’ class.

System.out.println(“Dog is barking.”);

        // This line prints the text “Dog is barking.” to the console.

Tasks:
1. Single Inheritance:
Create a base class Vehicle with a method start that prints “Vehicle started”. Derive a class Car from Vehicle and add a method drive that prints “Car is driving”. Create an object of Car in the main method, call the start and drive methods.

2. Hierarchical Inheritance:
Create a base class Person with a method walk that prints “Person is walking”. Derive two classes Student and Teacher from Person. Add a method study in Student that prints “Student is studying” and a method teach in Teacher that prints “Teacher is teaching”. Create objects of Student and Teacher in the main method and call their respective methods.

3. Multilevel Inheritance:
Create a base class ElectronicDevice with a method turnOn that prints “Device turned on”. Derive a class Computer from ElectronicDevice and add a method boot that prints “Computer is booting”. Further derive a class Laptop from Computer and add a method charge that prints “Laptop is charging”. Create an object of Laptop in the main method, call all methods.

4. Inheritance with Constructors:
Create a base class Building with a property address and a constructor to initialize this property. Derive a class House from Building and add a property numberOfRooms. Add a constructor to House that calls the base class constructor. Create an object of House in the main method and set all properties.

Course Video

YouTube Reference :

Frequently Asked Questions

Still have a question?

Let's talk

The training covers Java inheritance, including its concepts, syntax, types, and practical applications.

Yes, it includes examples demonstrating single inheritance, method overriding, and the use of the super keyword.

Yes, exercises involve creating parent and child classes, implementing inheritance hierarchies, and practicing method overriding.

Yes, it’s designed for beginners with clear explanations and step-by-step examples.

A Java IDE like Eclipse, IntelliJ IDEA, or an online compiler will suffice.

Yes, it covers single inheritance, multilevel inheritance, and hierarchical inheritance.

Yes, the training is self-paced and accessible online anytime.

Yes, it explains method overriding with examples and the use of the @Override annotation.

It typically takes a few hours to complete, depending on your learning pace.

Yes, this Java Inheritance training is completely free on Iqra Technology Academy’s website.