Chapter 25:Understanding Inheritance, Method Overriding, Specialized Methods, and the super and final Keywords in Java
In this post, we’ll dive deep into the core concepts of inheritance in Java, covering topics like overridden vs. specialized methods, using the super
keyword to access parent class members, and the final
keyword to impose certain restrictions on classes, methods, and variables.
1. Inheritance in Java
Inheritance is a key concept in object-oriented programming that allows a class to inherit properties and methods from another class. In Java:
The
extends
keyword is used to create a subclass (child class) from a superclass (parent class).The child class inherits fields, methods, and constructors of the parent class, except those marked as
private
.
Example:
class Animal {
void sound() {
System.out.println("Animal makes a sound");
}
}
class Dog extends Animal {
void bark() {
System.out.println("Dog barks");
}
}
public class Main {
public static void main(String[] args) {
Dog dog = new Dog();
dog.sound(); // Inherited method
dog.bark(); // Method specific to Dog class
}
}
Key Points:
Scope of Inheritance: Child classes inherit public and protected members but not private members.
Usability: Inheritance improves code reusability and creates a hierarchical structure.
2. Overridden vs. Specialized Methods
2.1 Overridden Methods
Overridden methods provide a new implementation for a method inherited from the parent class. They have the same name, return type, and parameters.
class Animal {
void sound() {
System.out.println("Animal makes a sound");
}
}
class Dog extends Animal {
@Override
void sound() {
System.out.println("Dog barks");
}
}
Key Points:
The
@Override
annotation is not required but is recommended to improve code readability.The access modifier of an overridden method cannot be more restrictive than that of the parent class method.
2.2 Specialized Methods
Specialized methods are unique to the child class and are not present in the parent class.
class Animal {
void sound() {
System.out.println("Animal makes a sound");
}
}
class Dog extends Animal {
void fetch() {
System.out.println("Dog fetches the ball");
}
}
Here, fetch()
is a specialized method in Dog
and does not exist in Animal
.
3. Using the super
Keyword
The super
keyword in Java is used to refer to the parent class and can be used in various contexts.
3.1 Accessing Parent Class Methods
If a method in the child class overrides a method from the parent class, super.methodName()
can be used to call the parent’s version of the method.
class Animal {
void sound() {
System.out.println("Animal makes a sound");
}
}
class Dog extends Animal {
void sound() {
super.sound(); // Calls Animal's sound() method
System.out.println("Dog barks");
}
}
3.2 Accessing Parent Class Constructors
The super
keyword can also be used to invoke the parent class constructor.
class Animal {
Animal() {
System.out.println("Animal constructor");
}
}
class Dog extends Animal {
Dog() {
super(); // Calls Animal constructor
System.out.println("Dog constructor");
}
}
Key Points:
super
can only be used inside a method or constructor of the child class.It helps to avoid naming conflicts between parent and child classes.
4. The final
Keyword
The final
keyword is used in Java to impose restrictions on classes, methods, and variables.
4.1 Final Classes
A class declared as final
cannot be subclassed. This is often used for security or design reasons.
final class Vehicle {
void type() {
System.out.println("Vehicle type");
}
}
// This would cause an error
// class Car extends Vehicle {}
4.2 Final Methods
A final
method cannot be overridden by subclasses.
class Animal {
final void sound() {
System.out.println("Animal sound");
}
}
class Dog extends Animal {
// This would cause an error
// void sound() { System.out.println("Dog sound"); }
}
4.3 Final Variables
A variable declared with final
becomes a constant and cannot be modified after initialization.
class Constants {
final int MAX_SPEED = 120;
void display() {
System.out.println("Max speed: " + MAX_SPEED);
}
}
Key Points:
Final Class: Prevents inheritance.
Final Method: Prevents overriding.
Final Variable: Prevents reassignment.
Summary Table of Access Modifiers with Inheritance
Modifier | Within Class | Within Package | Outside Package | Through Inheritance |
public | Yes | Yes | Yes | Yes |
protected | Yes | Yes | No | Yes |
Default | Yes | Yes | No | No |
private | Yes | No | No | No |
Conclusion
This post covered the essentials of inheritance in Java, distinctions between overridden and specialized methods, as well as the functionality and restrictions imposed by the super
and final
keywords. These features enhance code flexibility, encapsulation, and maintainability in Java programming.