Java Inheritance

Java Inheritance is a powerful feature of the Java programming language that allows developers to reuse code and build complex hierarchies of classes. In this blog, we will explore Java Inheritance in detail, including how to declare and use it, and provide example code snippets to illustrate each concept.

What is Inheritance?

Inheritance is the process by which one class acquires the properties and behaviors of another class. The class that is being inherited is called the parent class, base class, or super class. The class that inherits from the parent class is called the child class, derived class, or sub class.

Declaring Inheritance in Java:

To declare inheritance in Java, you use the extends keyword. Here is an example:

class ParentClass {
   // parent class code
}

class ChildClass extends ParentClass {
   // child class code
}

In the above example, we declare a parent class called ParentClass and a child class called ChildClass that extends the ParentClass.

Using Inherited Properties and Methods:

Once a child class inherits from a parent class, it can use all of the properties and methods of the parent class. Here is an example:

class ParentClass {
   int age;
   
   void displayAge() {
      System.out.println("Age: " + age);
   }
}

class ChildClass extends ParentClass {
   void setAge(int newAge) {
      age = newAge;
   }
}

public class Main {
   public static void main(String[] args) {
      ChildClass obj = new ChildClass();
      obj.setAge(25);
      obj.displayAge();
   }
}

In the above example, we declare a parent class called ParentClass that has an age property and a displayAge method. We then declare a child class called ChildClass that inherits from ParentClass and has a setAge method.

In the main method, we create an object of the ChildClass and set the age to 25 using the setAge method. We then call the displayAge method to display the age property, which is inherited from the parent class.

Overriding Inherited Methods:

In some cases, a child class may want to change the behavior of a method that it has inherited from the parent class. This is called method overriding. Here is an example:

class ParentClass {
   void display() {
      System.out.println("Parent class method");
   }
}

class ChildClass extends ParentClass {
   void display() {
      System.out.println("Child class method");
   }
}

public class Main {
   public static void main(String[] args) {
      ChildClass obj = new ChildClass();
      obj.display();
   }
}

In the above example, we declare a parent class called ParentClass that has a display method. We then declare a child class called ChildClass that inherits from ParentClass and overrides the display method.

In the main method, we create an object of the ChildClass and call the display method. The output will be “Child class method” because we have overridden the display method in the child class.

Conclusion:

Java Inheritance is a powerful feature that allows developers to reuse code and build complex hierarchies of classes. With the examples provided, you can get started with Java Inheritance and take your programming skills to the next level.