iheritance in java tutorial for beginners

Inheritance in java: introduction

The term inheritance in nature refers us to the inheritance between humans or between animals.

In the object oriented development, we borrowed this concept from the nature to enhance the quality of our code.

In fact, through the inheritance, we avoid code duplication and we increase the maintainability of our applications:

  • We define common behavior once in the parent class and all the child classes get this behavior automatically without having to copy/paste the code that implements this behavior.
  • This increases the readability and the maintainability of the code:

·       The number of code lines is minimal

·       Maintainability: the bugs are localized in the same class. So we correct the bug once inside one class and this correction is spread to the child classes.

The child classes can have, in addition to the behavior defined in the parent class, their own behavior.

The child classes do what does the parent class and do other things that their parent can't do.

In fact, this is taken from the nature were animals’s children are better than their parents.

Let's take the example of gazelle:

A gazelle with low speed will be caught by a carnivorous and will not survive.

A couple of gazelles with high speed will survive and get children which are better than their parents in term of speed.

These gazelle’s children inherit chromosomes from their parents that make them better than their parents in term of speed.

This is called natural selection.

Inheritance in java example: the extends keyword

We will present a class Doctor that is a child of the class Employee.

The class Employee is the following:

package sandbox;
public class Employee {
String firstName;
String lastName;
int age;
public void introduceYourself(){
 System.out.println("I am an employee");
            }
}

The class Employee has three attributes which have the default visibility (the package visibility).

To learn what is a package and why we use package, please check the package tutorial.

To learn about the access modifiers, please check the access modifiers tutorial.

We will deal about the visibility problem when dealing with the inheritance in a next section.

The class employee is called the super class or the parent class or the base class of the Doctor class.

The Doctor class is called the child class or the subclass or the derived class of the Doctor class.

The code of the Doctor class is the following:

package sandbox;
public class Doctor extends Employee {
}

The extends keyword says that the Doctor class inherits from the Employee class.

We have defined neither attributes nor methods inside the Doctor class.

But, the Doctor class has the stuff defined in its superclass (the default visibility allows the inheritance because the two classes are inside the sandbox package):

  • It has the three attributes of the super class (firstName, lastName and age)
  • It has the introduceYourself() method (a public method is inherited)

Here, we are reusing the code defined in the super class (Employee).

Inheritance in java and the access modifiers

The private attributes and methods defined in the super class are not inherited.

The public and protected attributes and methods defined in the super class are inherited.

Inheritance in Java and the super keyword

We can access an attribute or a method of the super class using the super keyword.

We can do it if the access modifier allows it.

These examples show some uses of the super keyword.

super(): calls the default constructor of the parent class

super(String, String, int) : calls the constructor of the super class that takes two Strings and an integer

super.age : accesses the age attribute of the parent class

super.introduceYourself() : calls the introduceYourself() method of the parent class

Inheritance in Java and constructors:

To learn what a constructor is, please check the constructor tutorial.

The child classes inherit the default constructor.

But, they don’t inherit the overloaded constructors.

In order to learn about overloading methods inside a class, please check the method overloading tutorial.

Inheritance in Java and the default constructor: example

package sandbox;

public class Employee {

            String firstName;

            String lastName;

            int age;

            public Employee() {

                        System.out.println("The Employee class default constructor is called");

            }

            public void introduceYourself(){

                        System.out.println("I am an employee");

            }

}

The code of the Doctor class is the following:

package
sandbox;
public class Doctor extends Employee {
public Doctor(){
System.out.println("TheDoctor class default constructor is called");
}
}

The main class is the following:

package
sandbox;
public class TestInheritance {
public static void main(String[] args) {
Doctor d = new Doctor();
}
}

The output of this example is the following:

The Employee class default constructor is called

The Doctor class default constructor is called

This example shows that the default constructor of the parent class is called automatically without calling it explicitly in the default constructor of the child class.

This is not true for an overloaded constructor.

Inheritance in Java and the overloaded constructors: example

An overloaded constructor of a super class is not called automatically in the correspondent overloaded constructor of a child class.

Inheritance in Java example: an overloaded constructor of the parent class is not called automatically

The code of the Employee class is the following:

package
sandbox;
public
class Employee {
String firstName;
String lastName;
int age;                      
public Employee() {
System.out.println("The Employee class default constructor is called");
}  
public Employee(String firstName, String lastName, int age) {
System.out.println("The Employee class's overloaded constructor is called");
this.firstName = firstName;
this.lastName = lastName;
this.age = age;
}
public void introduceYourself(){
System.out.println("I am an employee");
}         
}

The code of the Doctor class is the following:

package sandbox;
public class Doctor extends Employee {
public Doctor(){
System.out.println("The Doctor class default constructor is called");
}
public Doctor(String firstName, String lastName, int age) {                      
System.out.println("The Doctor class's overloaded constructor is called");
}          
}

The main class is the following:

package sandbox;
public class TestInheritance {
public static void main(String[] args) {
Doctor d = new Doctor("Mary","James",32);
}
}

The output of this program is the following:

The Employee class default constructor is called

The Doctor class's overloaded constructor is called

 

This example shows that the default constructor is called automatically (implicitly) in the default and the overloaded constructors of child classes.

The overloaded constructor of the Employee super class isn’t called implicitly in the correspondent overloaded constructor of the child class.

Inheritance in Java example: calling an overloaded constructor of the parent class in the child class 

We can call an overloaded constructor of the parent class explicitly in the child class.

This can be done using the super keyword.

The
example below shows this:
package sandbox;
public class Employee {
String firstName;
String lastName;
int age;               
public Employee() {
System.out.println("The Employee class default constructor is called");
}
public Employee(String firstName, String lastName, int age) {
System.out.println("The Employee class's overloaded constructor is called");
this.firstName = firstName;
this.lastName = lastName;
this.age = age;
}
public void introduceYourself(){
System.out.println("I am an employee");
}         
}

The code of the Doctor class is the following:

package
sandbox;
public class Doctor extends Employee {
public Doctor(){
System.out.println("The Doctor class default constructor is called");
}
public Doctor(String firstName, String lastName, int age) {                      
super(firstName, lastName, age);
System.out.println("The Doctor class's overloaded constructor is called");
}                     
}

The code of the main class is the following:

 package sandbox;

public class TestInheritance {
public static void main(String[] args) {       
Doctor d = new Doctor("Mary","James",32);
}
}

The output of the program is:

The Employee class's overloaded constructor is called

The Doctor class's overloaded constructor is called

 

In the Doctor class, the overloaded constructor of the super class is called using the super keyword.

In this case, the default constructor isn’t called.

Inheritance In Java and multiple inheritance

Java doesn’t allow the multiple inheritances unlike other programming languages like the C++.

This means that a java class cannot inherit from more than one class.

This tutorial whose title is inheritance in java arrives at its end. You can also check the oracle inheritance in java tutorial.

Please don’t forget to subscribe to like our How to Program Facebook page or to subscribe to our newsletter to get our latest JAVA tutorials.

To check our core java tutorial, please check this link.

See you in the next core java tutorial.

Post a Comment

Previous Post Next Post