Inheritance in Java

Inheritance in Java

Inheritance is a key concept in object-oriented programming that allows a class to inherit properties and behaviors from another class. It facilitates code reuse, enhances modularity, and enables the creation of hierarchical relationships between classes. In this article, we will explore the fundamentals of inheritance in Java with examples to illustrate its usage.

1. Basic Concepts of Inheritance:

Inheritance involves creating a new class, called a subclass or derived class, based on an existing class, called a superclass or base class. The subclass inherits the attributes and methods of the superclass, and it can also have additional features or override existing ones.

// Superclass (Base class)
class Vehicle {
void start() {
System.out.println(“Vehicle is starting.”);
}

void stop() {
System.out.println(“Vehicle is stopping.”);
}
}

// Subclass (Derived class)
class Car extends Vehicle {
void accelerate() {
System.out.println(“Car is accelerating.”);
}
}

Highlights:

  • The Car class extends the Vehicle class, inheriting the start and stop methods.
  • The accelerate method is specific to the Car class.

2. Method Overriding:

Method overriding allows a subclass to provide a specific implementation for a method that is already defined in its superclass.

class Animal {
void makeSound() {
System.out.println(“Some generic sound.”);
}
}

class Dog extends Animal {
@Override
void makeSound() {
System.out.println(“Bark! Bark!”);
}
}

Highlights:

  • The Dog class overrides the makeSound method inherited from the Animal class.
  • The @Override annotation indicates that the method is intended to override a superclass method.

3. Access Modifiers in Inheritance:

Access modifiers control the visibility of members (fields, methods) in a class. Inheritance allows the subclass to access the members of the superclass based on their access modifiers.

// Superclass
class Parent {
private int privateMember;
protected int protectedMember;
public int publicMember;
}

// Subclass
class Child extends Parent {
void accessMembers() {
// Cannot access privateMember
// Can access protectedMember and publicMember
}
}

Highlights:

  • private members are not accessible in the subclass.
  • protected and public members are accessible in the subclass.

4. Constructors in Inheritance:

Constructors in the superclass are not inherited by the subclass, but the subclass can invoke the constructor of the superclass using the super keyword.

class Vehicle {
Vehicle() {
System.out.println(“Vehicle constructor”);
}
}

class Car extends Vehicle {
Car() {
super(); // Invokes the constructor of the superclass
System.out.println(“Car constructor”);
}
}

Highlights:

  • The Car class invokes the constructor of the Vehicle class using super().

5. The super Keyword:

The super keyword is used to refer to the superclass. It can be used to call the superclass methods, access superclass fields, and invoke the superclass constructor.

class Animal {
void eat() {
System.out.println(“Animal is eating.”);
}
}

class Dog extends Animal {
void eat() {
super.eat(); // Calls the eat method of the superclass
System.out.println(“Dog is eating.”);
}
}

Highlights:

  • super.eat() calls the eat method of the superclass.

6. Key Concepts:

a. Multiple Inheritance:

  • Java supports single inheritance, meaning a class can extend only one superclass. However, it supports multiple interface inheritance.

b. Abstract Classes and Interfaces:

  • Abstract classes and interfaces provide additional tools for designing complex class hierarchies.

7. Conclusion:

Inheritance is a powerful mechanism in Java that promotes code reuse and facilitates the creation of well-structured and modular code. By leveraging inheritance, developers can build hierarchies of classes that model real-world relationships and take advantage of polymorphism to write flexible and extensible programs. Understanding the principles of inheritance is essential for mastering object-oriented programming in Java.

Leave a Comment

Your email address will not be published. Required fields are marked *