Site icon SATMMER

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:

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:

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:

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:

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:

6. Key Concepts:

a. Multiple Inheritance:

b. Abstract Classes and Interfaces:

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.

Exit mobile version