learn2code

Inheritance in Java with Example Programs (PDF)

inheritance in java with example programs pdf

Unlock the Secrets of Reusability and Extensibility in Your Java Applications

What is Inheritance?

Imagine a family tree. Parents pass down traits to their children, who inherit those characteristics while also developing their own unique qualities.

In Java programming, inheritance works similarly. It’s a fundamental pillar of object-oriented programming (OOP) that enables you to create new classes (child classes or subclasses) based on existing classes (parent classes or superclasses).

This fosters code reusability, eliminates redundancy, and promotes a logical organization of your software’s structure.

Download Inheritance in Java with Example Programs (PDF)

Why Inheritance Matters

  • Code Reusability: No need to reinvent the wheel. Reuse fields (variables) and methods from parent classes in your child classes.
  • Extensibility: Easily extend the functionality of existing classes without modifying the original code.
  • Readability: Inheritance mirrors real-world relationships, making your code easier to understand.
  • Polymorphism: Treat objects of different classes interchangeably, enhancing flexibility.

How Inheritance Works: The Basics

  1. The extends Keyword:
    • In Java, you use the extends keyword to establish the inheritance relationship.
class Animal {  // Parent class (Superclass)
   // ... (attributes and methods)
}

class Dog extends Animal {  // Child class (Subclass)
   // ... (additional attributes and methods)
}
  1. Constructors and Inheritance:
    • Child classes don’t inherit constructors directly from their parent classes.
    • Use the super() keyword within the child class constructor to invoke the parent class constructor.
  2. Overriding Methods:
    • A child class can provide its own implementation of a method inherited from the parent class. This is called method overriding.
    • Overriding lets you customize the behavior of inherited methods.

Illustrative Example: Vehicles

class Vehicle { // Parent class
    String brand;
    int year;

    public void start() {
        System.out.println("The vehicle starts.");
    }
}

class Car extends Vehicle { // Child class
    int numDoors;

    public void openTrunk() {
        System.out.println("The trunk opens.");
    }
}

// Usage
Car myCar = new Car();
myCar.brand = "Toyota"; 
myCar.year = 2024;
myCar.numDoors = 4;

myCar.start();    // Inherited from Vehicle
myCar.openTrunk(); // Specific to Car

Types of Inheritance in Java

  • Single Inheritance: One class inherits from a single parent class.
  • Multilevel Inheritance: A chain of inheritance where Class C inherits from Class B, which inherits from Class A.
  • Hierarchical Inheritance: Multiple classes inherit from a common parent class.

Note: Java does not support multiple inheritance (inheriting from multiple parent classes) directly with classes, but it does through interfaces.

Advanced Concepts

  • Abstract Classes and Methods
  • Interfaces
  • Casting
  • The final Keyword

Start building your first program today and discover the power of structured code!

Follow this link to deepen your understanding about inheritance in Java.

Leave a Reply

Harish

Typically replies within a hours