Inheritance
Inheritance in Java
Inheritance is a fundamental concept in Object-Oriented Programming (OOP) that allows a class (subclass or derived class) to inherit properties and behaviors from another class (superclass or base class). This promotes code reuse and establishes a relationship between classes. In Java, inheritance is achieved using the extends
keyword.
Syntax:
class Superclass {
// Fields and methods of the superclass
}
class Subclass extends Superclass {
// Fields and methods specific to the subclass
}
Example:
// Superclass
class Animal {
String species;
Animal(String species) {
this.species = species;
}
void eat() {
System.out.println("Animal is eating");
}
void sleep() {
System.out.println("Animal is sleeping");
}
}
// Subclass
class Dog extends Animal {
String breed;
Dog(String species, String breed) {
super(species); // Call the constructor of the superclass
this.breed = breed;
}
void bark() {
System.out.println("Dog is barking");
}
Access Modifiers in Inheritance:
- Public Members: Public members of the superclass are accessible in the subclass.
- Protected Members: Protected members of the superclass are accessible in the subclass. Additionally, they are accessible to other classes in the same package.
- Default (Package-Private) Members: Default members of the superclass are accessible in the subclass if both classes are in the same package.
- Private Members: Private members of the superclass are not directly accessible in the subclass.
Method Overriding:
Inheritance allows a subclass to provide a specific implementation for a method that is already defined in its superclass. This is known as method overriding.
// Superclass
class Animal {
void makeSound() {
System.out.println("Some generic sound");
}
}
// Subclass
class Dog extends Animal {
@Override
void makeSound() {
System.out.println("Bark! Bark!");
}
super
Keyword:
The super
keyword is used to refer to the superclass, allowing you to access its fields, methods, or constructors.
// Subclass constructor using super keyword
class Dog extends Animal {
String breed;
Dog(String species, String breed) {
super(species); // Call the constructor of the superclass
this.breed = breed;
}
Types of Inheritance:
- Single Inheritance: A subclass extends only one superclass.
- Multiple Inheritance (Interface-based): A subclass implements multiple interfaces. Java doesn't support multiple inheritance through classes to avoid the diamond problem, but it supports it through interfaces.
- Multilevel Inheritance: A subclass extends another subclass, creating a chain of inheritance.
Constructor Chaining:
In constructor chaining, a subclass constructor can call either its superclass constructor or another constructor within the same class using the super
or this
keyword.
// Superclass
class Animal {
Animal() {
System.out.println("Animal constructor");
}
}
// Subclass
class Dog extends Animal {
Dog() {
super(); // Call the constructor of the superclass
System.out.println("Dog constructor");
}
Inheritance is a powerful mechanism that enables code reuse and facilitates the creation of a hierarchy of classes, leading to more organized and modular code. However, it's essential to use inheritance judiciously to avoid issues such as tight coupling and to favor composition over inheritance when appropriate.