Java Inheritance is a fundamental concept in object-oriented programming that allows a new class to inherit properties and behaviors (fields and methods) from an existing class. This promotes code reusability and establishes a hierarchical relationship between classes.
Key Concepts
Superclass (Parent Class): The class whose properties and methods are inherited.
Subclass (Child Class): The class that inherits from the superclass.
Usage
Inheritance is used to create a new class that is a type of an existing class. It helps in extending the functionality of a class without modifying it, thereby adhering to the Open/Closed Principle of software design.
classAnimal{voideat(){System.out.println("This animal eats food.");}}classDogextendsAnimal{voidbark(){System.out.println("The dog barks.");}}publicclassInheritanceExample{publicstaticvoidmain(String[] args){Dog dog =newDog();
dog.eat();// Inherited method
dog.bark();// Subclass method}}āļāļąāļāđāļāļĨāļ·āđāļāļāđāļāļĒ
In this example, Dog is a subclass that inherits the eat method from the Animal superclass. The Dog class also has its own method, bark.
Example 2: Overriding Methods
classVehicle{voidrun(){System.out.println("The vehicle is running.");}}classCarextendsVehicle{@Overridevoidrun(){System.out.println("The car is running safely.");}}publicclassMethodOverridingExample{publicstaticvoidmain(String[] args){Car car =newCar();
car.run();// Overridden method}}āļāļąāļāđāļāļĨāļ·āđāļāļāđāļāļĒ
Here, the Car class overrides the run method of the Vehicle class to provide a specific implementation.
Example 3: Using super Keyword
classPerson{String name ="John";voiddisplay(){System.out.println("Person's name: "+ name);}}classEmployeeextendsPerson{String name ="Doe";voiddisplay(){System.out.println("Employee's name: "+ name);super.display();// Call superclass method}}publicclassSuperKeywordExample{publicstaticvoidmain(String[] args){Employee emp =newEmployee();
emp.display();}}āļāļąāļāđāļāļĨāļ·āđāļāļāđāļāļĒ
In this example, the super keyword is used to call the display method of the superclass Person from the subclass Employee.
Tips and Best Practices
Use Inheritance Judiciously: Only use inheritance when there is a true "is-a" relationship. For example, a Dog is an Animal.
Avoid Deep Inheritance Hierarchies: Keep inheritance hierarchies shallow to maintain simplicity and reduce complexity.
Prefer Composition Over Inheritance: Consider using composition (has-a relationship) instead of inheritance when appropriate, as it offers more flexibility.
Use @Override Annotation: Always use the @Override annotation when overriding methods to improve code readability and prevent errors.
Access Control: Use the protected access modifier for superclass members that should be accessible in subclasses but not to the outside world.
Java Inheritance is a fundamental concept in object-oriented programming that allows a new class to inherit properties and behaviors (fields and methods) from an existing class. This promotes code reusability and establishes a hierarchical relationship between classes.
Key Concepts
Usage
Inheritance is used to create a new class that is a type of an existing class. It helps in extending the functionality of a class without modifying it, thereby adhering to the Open/Closed Principle of software design.
Syntax
Examples
Example 1: Basic Inheritance
In this example,
Dogis a subclass that inherits theeatmethod from theAnimalsuperclass. TheDogclass also has its own method,bark.Example 2: Overriding Methods
Here, the
Carclass overrides therunmethod of theVehicleclass to provide a specific implementation.Example 3: Using
superKeywordIn this example, the
superkeyword is used to call thedisplaymethod of the superclassPersonfrom the subclassEmployee.Tips and Best Practices
Dogis anAnimal.@OverrideAnnotation: Always use the@Overrideannotation when overriding methods to improve code readability and prevent errors.protectedaccess modifier for superclass members that should be accessible in subclasses but not to the outside world.