Free Online Courses for Software Developers - MrBool
× Please, log in to give us a feedback. Click here to login
×

You must be logged to download. Click here to login

×

MrBool is totally free and you can help us to help the Developers Community around the world

Yes, I'd like to help the MrBool and the Developers Community before download

No, I'd like to download without make the donation

×

MrBool is totally free and you can help us to help the Developers Community around the world

Yes, I'd like to help the MrBool and the Developers Community before download

No, I'd like to download without make the donation

Java Inheritance Overview

In this article we will learn about the concepts of Inheritance and its implementation in Java. We will focus on understanding the meaning of Inheritance.

We will discuss the following

  1. Meaning and Use of inheritance?
  2. Types of Inheritance

What is Inheritance?

Reusability is an important concept of Object oriented paradigm. Inheritance is the concept of reusability and extensibility in the object oriented programming languages like C++, Java and so on.

This is better to understand with a discussion. Suppose we define and implement a class. Now we require a new class. When we study the implementation of the new class, we realize that some features of the new class has already been implemented and coded in a previous class. In this case we have two choices.

First option is we can simply go ahead and create a new class from scratch . Here we implement all the functionality as required from scratch. But in this way we will be putting our efforts in writing the code that we have already implemented in some other class. This means we are duplicating the efforts .

Second option is, we can choose to reuse the code that has been implemented already and only write the new code in the new class. This saves efforts and brings in the concept of reusability in applications. The programming implementation of reusability is achieved by a concept called Inheritance.

Inheritance is a process of developing new classes from the already developed classed.

Meaning of subclass and super class

As we said above, inheritance means to develop new classes from the already developed classes. We say that we are inheriting or deriving a class from other class. The newly created class is called the derived class or subclass or child class. The old class from which subclass is created is called as base class or super class or parent class.

We can derive a class from a class that is derived from other class and so on. Hence we can have a chain of derived classes.

Types of inheritance

Broadly saying, following are the main types of inheritance.

Single level Inheritance

In single level inheritance, one class is derived from another class. There is one super class and one subclass derived from the super class. Here is a graphical representation:,

A ----- > B

Here A is the base class or parent class and B is the derived class or child class. We can say class B is derived from class A and represent it with an arrow from A to B. The class B is the child class or the subclass whereas the class A is the parent class or super class.

Multi-level Inheritance

In multilevel inheritance, one class is derived from other class which is derived from other class. There is a chain of classes in inheritance.

A ---à B ----à C

Class B is derived from class A. Class C is derived from class B. A is the parent class of class B whereas B is the parent class of class C.

Multiple inheritance

There is one more type of inheritance, that is called multiple inheritance. In this, a subclass is derived from two or more base classes. In this case, for one derived class we can have two or more base classes. This can be represented as below:

A ----à C ß-----B

As shown in the figure, C is the sub class that is derived from two base classes A and B.

Note: We should note that Java does not support Multiple Inheritance. Programming language C++ supports multiple inheritance. But due to certain problems in multiple inheritance, Java does not support it. Note that multiple inheritance is somewhat implemented using interfaces in Java.

Implementation of Inheritance in Java

The syntax of implementing inheritance in java is by using the keyword extends.

               
 class A extends B {
         ..........................
         ..........................
}

A is the child class that is derived from parent class B. We also say that A is extended from class B. Here, class A inherits all the methods and fields of class B those are declared as public or protected.

Let us take an example now:

Example 1.We define a class A. It has and integer field named i and a constructor that initializes the value of i.

  // Declare a class A with field i and a constructor
  class A {
     int i;
     // Base class Constructor
     A (int ii) {
     i = ii; // Initialize i
   }
 }
 // Declare a class B derived from A with field j and a constructor
 class B extends A {
      int j;
      // Drived class Constructor
      B (int ii, int jj) {
         i = ii; // initializing the base class field i
         j = jj; // initialize its own field
     }
     void display(void) {
        System.out.println("The value of i is:" + i );
         System.out.println("The value of j is:” + j);                        
  }
}

In this example, class A is a base class with a field i. In the class A, we also have a constructor that initializes the value of field i. Next, we declare a class B that is derived from class A using the extends keyword as in: class B extends A .

Class B has its one field called j. Since, B is derived from A, it also inherits the field i from class A. This means that because B is inherited from class A then B actually has two fields i and j. The field j is its own field whereas the field i is what it inherits from class A.

In addition, notice the constructor of class B above. It takes two arguments ii and jj. The argument ii is used to initialize the field i, where the field i is inherited from A.

The argument jj is used to initialize the field j. So B can use i in the same way it can use field j.

Now let us see how we can use these two classes in the main method.

class Test {
                 public static void main(String args[] ) {
                                 B b = new B (10, 20);
                                 b.display();
                 }
  }

Here, in the main method, we create an object of class B in line 3. The constructor takes two arguments i.e. 10 and 20. The value 10 is passed to ii and value 20 is passed in jj. In the constructor of class B, ii is assigned to i, hence i will have 10. And the value jj is assigned to j. j will have the value 20.

Then we call the display method on object b of class B, which ouputs the value of i and j on screen.

The output will be:

The value of i is: 10

The value of j is: 20

Important thing to note here is that, we do not create the object of class A. We only created the object of class B. But class B has the class A inside it. So when we created an object of class B, an object of class A inside B is also created which has the field i stored in it.

Hence by a simple example like this, we can understand how an already implemented class can be reused to implement a new class.

All the methods and data fields that are defined in the base class as protected or public automatically become the part of the derived class. Like in the above explained example, the field i is declared in class A whereas it is used in class B as well. The class B does not declare the i variable, rather it inherits the i from A by the fact that B is a derived class from class A.

Example 2: Similarly we can implement multi-level inheritance.

In this example let us take three classes A, B and C. A is the base class. Class B should be derived from class A, and C should be derived from class B.

  class A {
  int i;
  // Base class Constructor
  A (int ii) {
  i = ii; // Initialize i
  }
  }
   
  // Declare a class B derived from A with field j and a constructor
  class B extends A {
  int j;
  // Derived class Constructor
  B (int ii, int jj ) {
  i = ii; // initializing the base class field i
  j = jj; // initialize its own field
  }
  void display (void) {
  System.out.println ("The value of i is::" + i );
  System.out.println ("The value of j is:” + j );                      
  }
  }
   
  // Declare a class B derived from A with field j and a constructor
  class C extends B {
  int k;
  // Derived class Constructor
  C (int ii, int jj, int kk ) {
  i = ii; // initializing the base class A field i
  j = jj; // initializing the base class B field j
  k = kk; // initializes its own field
  }
  void display (void) {
  System.out.println ("The value of i is::" + i );
  System.out.println ("The value of j is:” + j );                      
  System.out.println ("The value of k is:” + k );                    
  }
  }    

You should read the above code carefully. Line numbers 1-6 define a class A which has field i. The lines from 8-16 define a class B that is extended from class A. And from line 18-26, we define one more class C that is derived from B. That means we have a chain of classes as A, then its child class B and then the class C that is the child class of B. So C inherits all the features of class B as the class B is its parent class. But as class B is derived from class A, the class C also inherits the features of the class A.

Carefully notice the constructor of class A from line 21-26. The constructor initializes three fields i, j, and k. Here k is its own field declared at line 20. The field j is inherited from class B. And the field i is inherited from class A. Though class C does not directly inherit from class A, it still inherits its features from its base class B.

So by using multi-level inheritance, we are able to reuse the code of classes A and B in C.

We can use the classes B and C as depicted in the code below. The output is self-explanatory here.

class Test {
     public static void main(String args[] ) {
        B b = new B (10, 20);
         b.display();
  
        Cc = new C (100, 200, 300);
        c.display();
    }
  }
  Output will be:
                  The value of i is: 10
                  The value of j is: 20
                  The value of i is: 100
                  The value of j is: 200
                  The value of k is: 300

Conclusion:

In this tutorial we learned the concept of inheritance in java, its usage and implementation .This should have hopefully given you an idea of the importance of inheritance in programming.



Have experience over java, c++ c#, web-development, android development.

What did you think of this post?
Services
[Close]
To have full access to this post (or download the associated files) you must have MrBool Credits.

  See the prices for this post in Mr.Bool Credits System below:

Individually – in this case the price for this post is US$ 0,00 (Buy it now)
in this case you will buy only this video by paying the full price with no discount.

Package of 10 credits - in this case the price for this post is US$ 0,00
This subscription is ideal if you want to download few videos. In this plan you will receive a discount of 50% in each video. Subscribe for this package!

Package of 50 credits – in this case the price for this post is US$ 0,00
This subscription is ideal if you want to download several videos. In this plan you will receive a discount of 83% in each video. Subscribe for this package!


> More info about MrBool Credits
[Close]
You must be logged to download.

Click here to login