Inheritance In Java

We are examining Java inheritance with examples, in order to enhance learning.

Java’s inheritance concept is one of the cornerstones of Object-Oriented Programming.

We use Java inheritance when there Is-A relationship among objects.

Java Inheritance – Superclass and Subclass

In Java, it is feasible for classes to share characteristics and methods.

When it comes to Java inheritance, we split the “inheritance idea” into two parts:

  1. The class that inherits from another class is known as a subclass (Child Class).
  2. The class being inherited is known as the superclass (Parent Class).

The extends keyword can be used to inherit from a class.

The Java Language (subclass) in the following example inherits the properties and operations from the Programming Languages (superclass):

Example: 

class ProgrammingLanguages{ // Parent class is created herevoid print_bio(){ // A no return type method is created in the Parent Class System.out.println("Java is one of the most in-demand language!!"); } } // Parent class ends hereclass JavaLanguage extends ProgrammingLanguages{ // A child class is created here which extends from the parent classpublic static void main(String[] args) { // Main MethodJavaLanguage my_object=new JavaLanguage(); // Creating an object of the child class in order to access the attributes and methods of the parent class my_object.print_bio(); // Parent method calling from child class using object } // Output: Java is one of the most in-demand language!! }

Example: 

class Fruit{ // Here we have created the parent class boolean isAppleTasty=true; // A boolean variable named as isAppleTasty is used here }class Apple extends Fruit{ // We created another class Apple and extended it from Fruit (Parent) classpublic static void main(String[] args) {Apple my_obj=new Apple(); // Object of Child class is created here System.out.println("Is Apple tasty ?? : "+my_obj.isAppleTasty); // Printing the output by accessing parent attribute using object } // Output: Is Apple tasty ?? : true }

Example: 

class Airplanes { protected String founder_Boeing = "William Boeing"; // An attribute of class Airplanes public void bio() { // A method of Aiplanes class System.out.println("The first Airplane was founded by the Wright Brothers "); } }class Boeing extends Airplanes{ private String model_number = "Boeing-737"; // Attribute with a private modifier from the Boeing x public static void main(String[] args) {// Create an object named as my_boeing Boeing my_boeing= new Boeing();// Invoke the method bio() from the Airplanes(Parent Class) by using an objectmy_boeing.bio();// We use here the remaining attributes from the Airplanes(Parent Class) and Boeing class (Child class)System.out.println("The Founder of Boeing Airplane os : "+my_boeing.founder_Boeing); System.out.println("The most famous Boeing plane is: "+my_boeing.model_number);}// Output 1: The first Airplane was founded by the Wright Brothers // Output 2: The Founder of Boeing Airplane os : William Boeing // Output 3: The most famous Boeing plane is: Boeing-737 }

Have you noticed the protected modifier in Airplanes Class?

A protected access modifier was applied to the founder_boeing attribute in the Airplanes class.

The Boeing class would not be able to access it if it was set to private.

Why And When To Use “Inheritance”?

– It facilitates code reuse: You can reuse attributes and methods from existing classes when making new ones.

Hint: Look at the next chapter, Polymorphism, for an example of inherited methods being utilized to accomplish different tasks.



The final Keyword

You can employ the final keyword to prevent other classes from inheriting from a class.

A Java error will appear if you attempt to access a final class.

final class My_Final{

}
class My_Final_Child extends My_Final{
    
}

Example: 

final class Football { protected String footBall_founders = "The Chinese"; public void football_bio() { System.out.println("Football/Soccer as a game ranks the first in the world"); } }class Footballer extends Football { private String mrx_name = "Cristiano Ronaldo"; public static void main(String[] args) { Footballer my_object= new Footballer(); my_object.football_bio(); System.out.println("Football founders: "+my_object.footBall_founders + "\nFamous Footballer: " + my_object.mrx_name); } }

Output:

java: cannot inherit from final Football

Another Example: 

final class NFL { protected String nfl_start_date = "October 3, 1920"; public void nfl_Into() { System.out.println("The NFL was formed in 1920 as the American Professional Football Association (APFA) "); } }class NFL_teams extends NFL { private String [] nfl_team = {"1) Arizona Cardinals ","2) Baltimore Ravens ","3) Atlanta Falcons ","4) Buffalo Bills"}; public static void main(String[] args) { NFL_teams my_object= new NFL_teams(); my_object.nfl_Into(); System.out.println("Football founders: "+my_object.nfl_start_date); System.out.println("\nFive team names: \n"); for (int mrx=0;mrx<my_object.nfl_team.length;mrx++){ System.out.println(my_object.nfl_team[mrx]); } } }

 

Output:

java: cannot inherit from final NFL


Java Inheritance Benefits

  • Java’s inheritance mechanism is most practical for the reusability of code. Child classes can directly operate the code in the parent class.
  • The inheritance mechanism in Java allows us to achieve polymorphism.
We value your feedback.
+1
0
+1
0
+1
0
+1
0
+1
0
+1
0
+1
0

Subscribe To Our Newsletter
Enter your email to receive a weekly round-up of our best posts. Learn more!
icon

Leave a Reply

Your email address will not be published. Required fields are marked *