About Lesson
Inheritance is a fundamental concept in Object-Oriented Programming (OOP) that allows one class to inherit the fields and methods of another class. It inherits all of the members defined by the superclass and adds its own, unique elements. It promotes code reuse and establishes a natural hierarchical relationship between classes. In Java, inheritance is implemented using the extends
keyword.
You can only specify one superclass for any subclass that you create. Java does not support the inheritance of multiple superclasses into a single subclass.
Concepts
- Superclass (Parent Class): The class whose properties and methods are inherited by another class.
- Subclass (Child Class): The class that inherits the properties and methods of another class.
- Reusability: Inheritance promotes reusability by allowing existing classes to be extended and enhanced.
- Method Overriding: Subclasses can provide specific implementations of methods that are defined in their superclass.
- Access Modifiers: Inherited members can have different access levels (private, protected, public, default) which control their visibility. Although a subclass includes all of the members of its superclass, it cannot access those members of the superclass that have been declared asĀ private.
- Hierarchical Classification: Inheritance establishes a natural hierarchy, making the system easier to understand and maintain.
- Extensibility: New functionality can be added to existing classes by creating subclasses.
Example
// Superclass
public class Animal {
// Fields
private String name;
// Constructor
public Animal(String name) {
this.name = name;
}
// Getter for name
public String getName() {
return name;
}
// Method to describe the animal sound
public void makeSound() {
System.out.println("Some generic animal sound");
}
}
// Subclass
public class Dog extends Animal {
// Constructor
public Dog(String name) {
super(name); // Call the constructor of the superclass
}
// Overriding the makeSound method
@Override
public void makeSound() {
System.out.println("Woof");
}
// Additional method
public void fetch() {
System.out.println(getName() + " is fetching the ball");
}
public static void main(String[] args) {
Dog dog = new Dog("Buddy");
System.out.println("Name: " + dog.getName()); // Output: Name: Buddy
dog.makeSound(); // Output: Woof
dog.fetch(); // Output: Buddy is fetching the ball
}
}
Types of Inheritance
- Single Inheritance: A class inherits from one superclass.
- Multilevel Inheritance: A class is derived from a subclass, which is also derived from another superclass. The derived class (subclass) inherit features from all of their ancestors (superclass). The properties and methods of the topmost class in the hierarchy are available to the lowest class through intermediate classes.
- Hierarchical Inheritance: Multiple classes inherit from a single superclass.
- Multiple Inheritance: NOT supported in Java.
Classes that cannot be inherited
- Final Classes: A class declared with the
final
keyword cannot be inherited.
final class FinalClass {
// Class implementation
}
// The following will produce a compilation error
class SubClass extends FinalClass {
// Error: cannot inherit from final FinalClass
}
- Private Classes: Classes defined inside other classes (inner classes) as
private
cannot be accessed from outside the enclosing class and thus cannot be inherited.
class OuterClass {
private class PrivateInnerClass {
// Inner class implementation
}
}
// The following will produce a compilation error
class SubClass extends OuterClass.PrivateInnerClass {
// Error: PrivateInnerClass has private access in OuterClass
}
- Anonymous classes: are local classes without a name and cannot be inherited because they are defined and instantiated in a single expression.
- Enums in Java are implicitly
final
, meaning they cannot be subclassed.
enum MyEnum {
VALUE1, VALUE2;
}
// The following will produce a compilation error
class SubClass extends MyEnum {
// Error: cannot inherit from final MyEnum
}
Method declared as final
cannot be overriden by any subclass. Please note they are inherited by the subclasses.
class BaseClass {
// final method
public final void display() {
System.out.println("This is a final method in BaseClass.");
}
// non-final method
public void show() {
System.out.println("This is a non-final method in BaseClass.");
}
}
class SubClass extends BaseClass {
// Attempting to override a final method will result in a compile-time error
// public void display() {
// System.out.println("This is an overridden method in SubClass.");
// }
// Overriding a non-final method
@Override
public void show() {
System.out.println("This is an overridden method in SubClass.");
}
}
public class Main {
public static void main(String[] args) {
SubClass obj = new SubClass();
obj.display(); // This is a final method in BaseClass.
obj.show(); // This is an overridden method in SubClass.
}
}