Introduction
Inheritance in java is a mechanism where one class acquires all the properties and behaviors of another class. The main advantage of Inheritance is reusability; we can use fields and methods of the parent class. We use Inheritance, where an “Is-A” relationship is present between two classes. The parent class in java is denoted as a superclass, whereas the inherited class is denoted as a subclass. We use the keyword extends to inherit the features of the superclass in the subclass.
Types of Inheritance
There are several types of Inheritance:
1] Single Inheritance: In this inheritance, one class acquires the property of another class.
2] Multiple Inheritance: In this inheritance, one class acquires the property of more than one class. Java does not support Multiple Inheritance. In java, multiple inheritance can be obtained with the help of an interface only.
3] Multilevel Inheritance: A chain of inheritance is called multilevel inheritance.
4] Hierarchical Inheritance: When properties of two or more classes are used in a single class.
5] Hybrid Inheritance: A combination of both single and multiple inheritance. Java does not support this type of inheritance.
Advantages of Inheritance
1] Code Reusability
2] Runtime Polymorphism can be achieved with the help of a method overriding concept.
Example of Inheritance
class Employee{
float salary=60000;
}
class Tester extends Employee{
int bonus=9000;
public static void main(String args[]){
Tester p=new Tester();
System.out.println("Tester salary is:"+p.salary);
System.out.println("Bonus of Tester is:"+p.bonus);
}
}
Advanced Concepts in Inheritance
Inheritance is not just about acquiring methods and fields from a parent class; it also involves overriding methods, accessing superclass members, and implementing polymorphism. Below are more detailed explanations and examples to expand on inheritance concepts.
Accessing Superclass Members
- The
super
keyword is used to access:- Superclass methods or variables that are hidden by subclass methods or variables.
- The parent class constructor explicitly.
Example: Using super
to Access Superclass Methods and Variables
java class Animal {
String name = "Generic Animal";
void display() {
System.out.println("I am an animal.");
}
}
class Dog extends Animal {
String name = "Dog";
void display() {
super.display(); // Call parent class method
System.out.println("I am a dog.");
}
void printNames() {
System.out.println("Parent name: " + super.name); // Access parent class variable
System.out.println("Child name: " + name); // Access child class variable
}
}
public class Main {
public static void main(String[] args) {
Dog dog = new Dog();
dog.display();
dog.printNames();
}
}
Output:
css I am an animal.
I am a dog.
Parent name: Generic Animal
Child name: Dog
Constructor Chaining in Inheritance
When a subclass object is created, the constructor of the superclass is called automatically before the subclass constructor. This ensures proper initialization of the parent class fields.
Example:
java class Animal {
Animal() {
System.out.println("Animal constructor called.");
}
}
class Dog extends Animal {
Dog() {
super(); // Explicit call to parent constructor (optional)
System.out.println("Dog constructor called.");
}
}
public class Main {
public static void main(String[] args) {
Dog dog = new Dog();
}
}
Output:
kotlin Animal constructor called.
Dog constructor called.
Method Overriding
- A subclass can provide a specific implementation for a method already defined in the parent class.
- Method overriding requires:
- Same method name.
- Same parameter list.
- IS-A relationship (inheritance).
Example:
java class Animal {
void sound() {
System.out.println("Animals make sounds.");
}
}
class Dog extends Animal {
@Override
void sound() {
System.out.println("Dogs bark.");
}
}
public class Main {
public static void main(String[] args) {
Animal animal = new Dog(); // Polymorphism
animal.sound(); // Calls overridden method in Dog class
}
}
Output:
Dogs bark.
Final Keyword in Inheritance
- Final Class: A
final
class cannot be extended.javaCopy codefinal class Animal { void eat() { System.out.println("Eating..."); } } // class Dog extends Animal {} // Compilation error
- Final Method: A
final
method cannot be overridden.javaCopy codeclass Animal { final void sound() { System.out.println("Animals make sounds."); } } class Dog extends Animal { // void sound() {} // Compilation error }
Abstract Classes and Inheritance
An abstract class can serve as a base class for other classes. It can have both abstract (incomplete) methods and concrete (complete) methods.
Example:
java abstract class Animal {
abstract void sound(); // Abstract method (no implementation)
void eat() {
System.out.println("This animal eats food.");
}
}
class Dog extends Animal {
@Override
void sound() {
System.out.println("Dogs bark.");
}
}
public class Main {
public static void main(String[] args) {
Animal dog = new Dog(); // Abstract class reference
dog.eat();
dog.sound();
}
}
Output:
This animal eats food.
Dogs bark.
Multiple Inheritance via Interfaces
Java avoids multiple inheritance with classes to prevent the diamond problem, but it allows multiple inheritance using interfaces.
Example:
java interface A {
void methodA();
}
interface B {
void methodB();
}
class C implements A, B {
public void methodA() {
System.out.println("Method A implementation.");
}
public void methodB() {
System.out.println("Method B implementation.");
}
}
public class Main {
public static void main(String[] args) {
C obj = new C();
obj.methodA();
obj.methodB();
}
}
Output:
css Method A implementation.
Method B implementation.
Downcasting and Upcasting
- Upcasting: Converting a subclass object to a superclass type. Always safe.
- Downcasting: Converting a superclass reference back to subclass type. Requires explicit casting and can throw
ClassCastException
if not done correctly.
Example:
java 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) {
Animal animal = new Dog(); // Upcasting
animal.sound();
Dog dog = (Dog) animal; // Downcasting
dog.bark();
}
}
Super Keyword:
Super Keyword is used to access the properties (data members or methods) of a superclass or parent class.
The super keyword can be used with variable, method, and constructor.
- super can be used to refer to the variable of parent class where parent class and child class both have the same field.
- super can be used to call the method of parent class when superclass and subclass have a method with the same name. It is mainly used in the case of method overriding.
- super() can be used to call the constructor of the parent class.
Final Keyword:
The final keyword can be used with a variable, a method, or a class in different aspects.
- When we want that a variable value remains constant, the final keyword is used. The value of the final variable can not be changed.
- When we use the final keyword with the method, then it can not be overridden.
- When we use the final keyword with any class, then that class cannot be extended.
One Response