Inheritance is one of the four fundamental concepts (Encapsulation, Inheritance, Polymorphism, and Abstraction) in Java OOP. It is the mechanism in Java to allow classes to inherit data and methods from another class. It provides a way to establish hierarchical relationships between classes, promoting code reuse, and improve maintainability.
Key concepts of inheritance
- Parent class (Superclass)
- The class whose properties and behaviours are inherited is the superclass or parent class. The inheritance in Java is implemented by the
extends
keyword.
- The class whose properties and behaviours are inherited is the superclass or parent class. The inheritance in Java is implemented by the
- Subclass
- The class that inherits from the superclass is called the subclass or child class.
- The subclass can use the properties and methods of the superclass and can also have additional properties and methods or override the inherited ones.
- Single inheritance
- Single inheritance is supported in Java, meaning one class can only inherit from one superclass. However, a class can implement multiple interfaces.
- Accessibility
- Public and protected fields of a superclass can be directly accessed by its subclasses. (More about access modifier in Access Modifier)
Generic animal sound
Bark
Super keyword
In Java, when you instantiate a child object, the constructor of the parent class is also invoked. This process ensures the parent class is initialised before the child class. This invocation happens implicitly through a call to the super()
to invoke the super class constructor, which is inserted by Java compiler if not explicitly provided by the programmer.
Implicit call
If you do not explicitly call the super()
method is the subclass constructor, the Java compiler will automatically call the non-argument constructor of the superclass.
Parent constructor called
Child constructor called
Explicit call
You can explicitly call the super class constructor by using the super()
method. You can supply arguments to this call.
Parent constructor called: Hello from Child
Child constructor called
The Object class
In Java, every class is implicitly a subclass of the java.lang.Object
class if it does not explicitly extend another class. When a class in initialised, there is always an implicit call to the Object Class constructor.
Method overriding
Method Overriding is different from Method Overloading
The Method Overloading occurs when a class has more than one method with the same name but different parameters (different type or number of parameters, or both). Overloaded methods can have different return types and access modifiers.
Method overriding is where subclass provides a specific implementation of a method that already defined in the superclass.
- The method signature must be the same (method name, return type, and parameters)
- Allows dynamic method dispatch (runtime polymorphism)
- The overridden method in the subclass should have the same or more accessible access modifier
- Use
@Override
annotation for readability and compile-time checking
Generic animal sound
Bark
Back to parent page: Java Standard Edition (Java SE) and Java Programming
Web_and_App_DevelopmentProgramming_LanguagesJavaInheritanceMethod_OverridingSuper_Keyword