Data Types and Operators
(Non-Primitive Data Types)
Control Flow Statements
Conditional Statements
Looping Statements
Branching Statements
Object-Oriented Programming (OOP)
Exception Handling
Collections Framework
Overview of Collections
Java I/O
Multithreading
GUI Programming with Swing
Advanced Topics
JAVA CODE
Java Basics
Working with Objects
Arrays, Conditionals, and Loops
Creating Classes and Applications in Java
More About Methods
Java Applet Basics
Graphics, Fonts, and Color
Simple Animation and Threads
More Animation, Images, and Sound
Managing Simple Events and Interactivity
Creating User Interfaces with the awt
Windows, Networking, and Other Tidbits
Modifiers, Access Control, and Class Design
Packages and Interfaces
Exceptions
Multithreading
Streams and I/O
Using Native Methods and Libraries
Under the Hood
Java Programming Tools
Working with Data Structures in Java
Advanced Animation and Media
Fun with Image Filters
Client/Server Networking in Java
Emerging Technologies
appendix A :- Language Summary
appendix B :- Class Hierarchy Diagrams
appendix C The Java Class Library
appendix D Bytecodes Reference
appendix E java.applet Package Reference
appendix F java.awt Package Reference
appendix G java.awt.image Package Reference
appendix H java.awt.peer Package Reference
appendix I java.io Package Reference
appendix J java.lang Package Reference
appendix K java.net Package Reference
appendix L java.util Package Reference

In Java, inheritance is a fundamental concept in object-oriented programming (OOP) that allows a class (referred to as a subclass or derived class) to inherit fields and methods from another class (referred to as a superclass or base class). This promotes code reusability and establishes a natural hierarchical relationship between classes. Java supports several types of inheritance:

1. Single Inheritance:

  • Description: A class inherits from only one superclass. This is the most common type of inheritance.

Example:

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

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

				
			

2. Multilevel Inheritance:

  • Description: A class is derived from another class, which is also derived from another class, forming a chain.

Example:

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

class Mammal extends Animal {
    void walk() {
        System.out.println("This mammal walks.");
    }
}

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

				
			

3. Hierarchical Inheritance:

  • Description: Multiple classes inherit from a single superclass.

Example:

				
					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.");
    }
}

				
			

4. Multiple Inheritance (through Interfaces):

  • Description: Java does not support multiple inheritance directly to avoid complexity and ambiguity. However, it can be achieved through interfaces, where a class can implement multiple interfaces

Example:

				
					interface CanRun {
    void run();
}

interface CanSwim {
    void swim();
}

class Amphibian implements CanRun, CanSwim {
    public void run() {
        System.out.println("This amphibian can run.");
    }

    public void swim() {
        System.out.println("This amphibian can swim.");
    }
}

				
			

5. Hybrid Inheritance (through Interfaces):

  • Description: A combination of two or more types of inheritance. Since Java does not support multiple inheritance directly, hybrid inheritance can be achieved using interfaces.

Example:

				
					interface CanFly {
    void fly();
}

interface CanSwim {
    void swim();
}

class Bird {
    void layEggs() {
        System.out.println("This bird lays eggs.");
    }
}

class Duck extends Bird implements CanFly, CanSwim {
    public void fly() {
        System.out.println("This duck can fly.");
    }

    public void swim() {
        System.out.println("This duck can swim.");
    }
}

				
			

Key Points of Inheritance in Java

  • Reusability: Inheritance promotes code reusability. Methods and fields from the parent class can be reused in the child class.
  • Method Overriding: A subclass can provide a specific implementation of a method already defined in its superclass.
  • Polymorphism: Inheritance supports polymorphism, where a subclass object can be treated as an instance of its superclass.
  • Super Keyword: Used to refer to the immediate parent class object.
  • Constructor Chaining: Inheritance supports constructor chaining, where the constructor of the subclass can call the constructor of the superclass.

Example with Overriding and Super Keyword

				
					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.");
    }

    void bark() {
        System.out.println("The dog barks.");
    }

    void showSuperEat() {
        super.eat(); // Calls the eat method of the superclass (Animal)
    }
}

public class TestInheritance {
    public static void main(String[] args) {
        Dog dog = new Dog();
        dog.eat(); // Calls the overridden method in Dog
        dog.bark();
        dog.showSuperEat(); // Calls the superclass method using super keyword
    }
}

				
			

In this example, ‘Dog‘ overrides the ‘eat‘ method of ‘Animal‘, and the ‘super.eat()‘ call allows access to the superclass’s ‘eat‘ method.

Scroll to Top