WikiGalaxy

Personalize

Java Inheritance: Introduction

Concept Overview:

Inheritance in Java is a mechanism where one class acquires the properties (fields) and behaviors (methods) of another class. The class which inherits the properties of another is known as the subclass (derived class, child class), and the class whose properties are inherited is known as the superclass (base class, parent class).

Benefits of Inheritance:

Code Reusability, Method Overriding, and Polymorphism are key benefits of using inheritance in Java.


class Animal {
  void eat() {
    System.out.println("This animal eats food.");
  }
}

class Dog extends Animal {
  void bark() {
    System.out.println("The dog barks.");
  }
}

public class TestInheritance {
  public static void main(String args[]) {
    Dog d = new Dog();
    d.eat();
    d.bark();
  }
}
    

Console Output:

This animal eats food.

The dog barks.

Java Inheritance: Single Inheritance

Understanding Single Inheritance:

Single Inheritance is when a class inherits from only one superclass. This is the simplest form of inheritance in Java.

Example Explanation:

In this example, the class `Car` inherits from the class `Vehicle`, acquiring its properties and methods.


class Vehicle {
  void move() {
    System.out.println("The vehicle is moving.");
  }
}

class Car extends Vehicle {
  void drive() {
    System.out.println("The car is being driven.");
  }
}

public class TestSingleInheritance {
  public static void main(String args[]) {
    Car c = new Car();
    c.move();
    c.drive();
  }
}
    

Console Output:

The vehicle is moving.

The car is being driven.

Java Inheritance: Multilevel Inheritance

Understanding Multilevel Inheritance:

Multilevel Inheritance is when a class is derived from another derived class. This creates a hierarchy of inheritance.

Example Explanation:

In this example, `Dog` inherits from `Animal`, and `Bulldog` inherits from `Dog`, forming a multilevel inheritance chain.


class Animal {
  void eat() {
    System.out.println("This animal eats food.");
  }
}

class Dog extends Animal {
  void bark() {
    System.out.println("The dog barks.");
  }
}

class Bulldog extends Dog {
  void growl() {
    System.out.println("The bulldog growls.");
  }
}

public class TestMultilevelInheritance {
  public static void main(String args[]) {
    Bulldog b = new Bulldog();
    b.eat();
    b.bark();
    b.growl();
  }
}
    

Console Output:

This animal eats food.

The dog barks.

The bulldog growls.

Java Inheritance: Hierarchical Inheritance

Understanding Hierarchical Inheritance:

Hierarchical Inheritance occurs when multiple classes inherit from a single superclass. This allows different classes to share a common behavior.

Example Explanation:

In this example, both `Cat` and `Dog` inherit from the `Animal` class, showcasing hierarchical inheritance.


class Animal {
  void eat() {
    System.out.println("This animal eats food.");
  }
}

class Dog extends Animal {
  void bark() {
    System.out.println("The dog barks.");
  }
}

class Cat extends Animal {
  void meow() {
    System.out.println("The cat meows.");
  }
}

public class TestHierarchicalInheritance {
  public static void main(String args[]) {
    Dog d = new Dog();
    Cat c = new Cat();
    d.eat();
    d.bark();
    c.eat();
    c.meow();
  }
}
    

Console Output:

This animal eats food.

The dog barks.

This animal eats food.

The cat meows.

Java Inheritance: Method Overriding

Understanding Method Overriding:

Method Overriding occurs when a subclass provides a specific implementation for a method already defined in its superclass. This allows for dynamic method dispatch and polymorphism.

Example Explanation:

In this example, the `Dog` class overrides the `eat()` method of the `Animal` class to provide a specific implementation.


class Animal {
  void eat() {
    System.out.println("This animal eats food.");
  }
}

class Dog extends Animal {
  @Override
  void eat() {
    System.out.println("The dog eats bones.");
  }
}

public class TestMethodOverriding {
  public static void main(String args[]) {
    Animal a = new Dog();
    a.eat();
  }
}
    

Console Output:

The dog eats bones.

Java Inheritance: Using 'super' Keyword

Understanding the 'super' Keyword:

The `super` keyword in Java is used to refer to the immediate parent class object. It is used to access parent class methods and constructors.

Example Explanation:

In this example, the `Dog` class uses `super.eat()` to call the `eat()` method of its superclass, `Animal`.


class Animal {
  void eat() {
    System.out.println("This animal eats food.");
  }
}

class Dog extends Animal {
  void eat() {
    super.eat();
    System.out.println("The dog eats bones.");
  }
}

public class TestSuperKeyword {
  public static void main(String args[]) {
    Dog d = new Dog();
    d.eat();
  }
}
    

Console Output:

This animal eats food.

The dog eats bones.

Java Inheritance: Constructor Chaining

Understanding Constructor Chaining:

Constructor chaining in Java is the process of calling one constructor from another constructor with respect to the current object. It can be within the same class or between parent and child classes.

Example Explanation:

In this example, the `Dog` class constructor calls the `Animal` class constructor using `super()`.


class Animal {
  Animal() {
    System.out.println("Animal is created.");
  }
}

class Dog extends Animal {
  Dog() {
    super();
    System.out.println("Dog is created.");
  }
}

public class TestConstructorChaining {
  public static void main(String args[]) {
    Dog d = new Dog();
  }
}
    

Console Output:

Animal is created.

Dog is created.

Java Inheritance: Abstract Classes

Understanding Abstract Classes:

An abstract class in Java is a class that cannot be instantiated and is used to declare common characteristics for subclasses. It can contain abstract methods (without a body) and concrete methods (with a body).

Example Explanation:

In this example, `Animal` is an abstract class with an abstract method `sound()`, which is implemented by the `Dog` class.


abstract class Animal {
  abstract void sound();
}

class Dog extends Animal {
  void sound() {
    System.out.println("The dog barks.");
  }
}

public class TestAbstractClass {
  public static void main(String args[]) {
    Animal a = new Dog();
    a.sound();
  }
}
    

Console Output:

The dog barks.

Java Inheritance: Interface Implementation

Understanding Interfaces:

An interface in Java is a reference type, similar to a class, that can contain only constants, method signatures, default methods, static methods, and nested types. Interfaces cannot contain instance fields.

Example Explanation:

In this example, the `Animal` interface is implemented by the `Dog` class, which provides the implementation for the `sound()` method.


interface Animal {
  void sound();
}

class Dog implements Animal {
  public void sound() {
    System.out.println("The dog barks.");
  }
}

public class TestInterface {
  public static void main(String args[]) {
    Animal a = new Dog();
    a.sound();
  }
}
    

Console Output:

The dog barks.

logo of wikigalaxy

Newsletter

Subscribe to our newsletter for weekly updates and promotions.

Privacy Policy

 • 

Terms of Service

Copyright © WikiGalaxy 2025