Java Object-Oriented Programming Tutorial on Inheritance & ‘extends’ Keyword with Stone River eLearning

We have already discussed a few concepts of Object Oriented Programming that gives it an edge over the procedural languages. One of the most commonly used aspects of Object Oriented Programming is the inheritance of parent class by the child class. Inheritance, as the name suggests, is a process that allows one object to acquire all the properties and behavior of the parent object.

The idea behind inheritance in JAVA is to create new classes built on the existing classes and reuse the existing methods and variables of the existing class in the new class. Further, inheritance in JAVA allows the user to include additional functionalities to the class that he/she is working on while inheriting the functionalities of the parent class as well.

 

How to implement inheritance in JAVA?

Unlike C++, which has a specified syntax including class names and return type in a specified order, JAVA being a pure Object Oriented Programming language, relies upon pre-defined keywords for the implementation of its various concepts. The keyword used to implement inheritance in JAVA is called “extends”.  Let us look at an example of inheritance in JAVA to understand the concept in a better way.

Eg:


public class student

{

int id;

string name;

public void get_details()

{

id = 11;

name = “Ollie”

}

public void show

{

System.out.println(“ID: “+id);

System.out.println(“NAME: “+name);

}}

public class Result extends student

{

int total;

public void report()

{

Total =485;

get_details();

show();

System.out.println(“TOTAL: “ +total);

}}

public class Test

{

public static void main(String[] args)

{

Result r = new Result();

r.report();

}

}

In the above example, the class “ Result “ inherits the parent class “ student” and accesses the methods “get_details()” and “show()” of the parent class.

 

Note:

  1. “extends” keyword is used to perform inheritance in JAVA.
  2. The base class in the inheritance process is called Super class or Parent class while the derived class is called sub class or child class.
  3. JAVA does not support multiple inheritance i.e one class cannot inherit the properties from more than one parent class. However; interface can be used to perform the job of multiple inheritance. We will discuss more about interface in the next blog posts.

The constructor of a parent class cannot be inherited by the base class. It is because constructor is not a member function of the parent class, however; the constructor of the parent class can be invoked from a child class. The other way of getting the values of the constructor of the parent class in the derive class, as explained in the video lecture, is by using the “super” keyword.

Eg:


public class Box

{

double d1, d2;

public Box(double x, double y)

{

d1=x;

d2=y;

}

public void show

{

System.out.println(“Dimension 1: “ +d1);

System.out.println(“Dimension 2: “ +d2);

}}

public class Box 2 extends Box

{

double z;

public Box2(double x, double y)

{

super (x,y);

}

public void area()

[

z=d1*d2;

System.out.println(“AREA: “ +z);

}}

public class Test

{

public static void main (String[] args)

{

Box2 ob = new Box2(6,5);

ob.area();

}

}

As it can be seen in the above example, the “super” keyword is used in the constructor of the child class to fetch the values from the constructor of the parent class, which is later used in the method called “area()”.

 

Note:

  1. The super keyword should be used in the first line of the constructor of the class. If anything precedes the “super” keyword in the constructor of the derived class, then the “super” keyword won’t work.
    Inheritance will be used extensively while writing JAVA codes for applications and therefore, good understanding of this topic helps build a base for the other related concepts in JAVA.  We will be discussing about abstract classes and interfaces in the next blog posts.

 

(Visited 133 times, 1 visits today)