Inheritance-in-Java12
Inheritance-in-Java12
The extends keyword indicates that we are making a new class that
derives from an existing class. The meaning of "extends" is to increase
the functionality.
Java Inheritance
class Employee{
float salary=40000;
}
class Programmer extends Employee{
int bonus=10000;
public static void main(String args[]){
Programmer p=new Programmer();
System.out.println("Programmer salary is:"+p.salary);
System.out.println("Bonus of Programmer is:"+p.bonus);
}
}
Test it Now
Programmer salary is:40000.0
Bonus of programmer is:10000
Benefits of Inheritance
Inheritance offers several advantages, including:
Code Reusability: Inherited members from a superclass can be reused
in subclasses, reducing redundant code and promoting a modular
approach to software development.
Hierarchical Organization: Inheritance facilitates the creation of well-
structured class hierarchies, improving code readability and
maintainability.
Polymorphism: Subclasses can override superclass methods, allowing
for polymorphic behavior, where methods can behave differently
based on the object type at runtime.
Easier Maintenance: Changes made to a superclass automatically
propagate to its subclasses, ensuring consistency and simplifying
maintenance efforts.