Java Inheritance for Beginners Explained with Examples – MPS


This tutorial will guide you on what is Inheritance in Java. You will know how to use Inheritance for a class and reuse its properties.

Basics of Inheritance in Java

You can go through the following sections to learn about Java Inheritance.

Inheritance tree

Concept Inheritance in Java

Description:

Inheritance is one of the most-used features of Object-Oriented Programming. It denotes a methodology through which a newly built class extracts features (methods and variables/fields) from an already existing class. With the implementation of inheritance, information gets available in a hierarchical order.

According to the hierarchical style, the class which inherits the features of the already existing class is known as a subclass or a derives/child class.

The class down the hierarchy, from whom the features are inherited is called superclass or base/parent class.

In Java, you can use the extends keyword to implement the concept of inheritance. The use of inheritance is realized when the programmer does not wish to define the same set of methods or fields for the new class if they are already defined once.

Syntax:

The general syntax to implement inheritance is given below. You can’t extend a class as long as it is not defined:

class Parent 

   // methods
   // fields

   // ……


class Child extends Parent 

   // already supports the methods and fields in Parent class
   // additional features

Java Inheritance

Inheritance example:

The example below will help you visualize the implementation of inheritance.

class Calculator 
    int add(int a , int b)
    
        return a + b;
    
    
    int sub(int a , int b)
    
        return a - b;
    


public class AdvancedCalculator extends Calculator 
    int mult(int a , int b)
    
        return a * b;
    
    
    int div(int a , int b)
    
        return a / b;
    

    public static void main(String args[])
    
        AdvancedCalculator cal = new AdvancedCalculator();
        
        System.out.println( cal.add(1, 2) );
        System.out.println( cal.sub(1, 2) );
        System.out.println( cal.mult(1, 2) );
        System.out.println( cal.div(1, 2) );
    

The superclass AdvancedCalculator uses already defined add and sub-methods and adds more functionality (product and division). When the object cal is created, a copy of contents from the Calculator class is created. It is important to note constructors are not inherited by subclass as they are neither methods nor fields. However, the constructor of the parent class can be invoked by the child class.

Therefore, the output is:

3
-1
2
0

Super keyword

Description:

The super keyword is used when we need to invoke the constructor of the superclass from within the subclass. In case members and fields of both, the classes have identical names, the super keyword is used to differentiate between the superclass members from the subclass members. super keyword is very much like this keyword but only used for the superclass, within the subclass.

If the superclass has a predefined parametrized constructor, the subclass needs to invoke the superclass constructor using the super keyword.

Super keyword example:

The example below shows the use of super keyword:

class Parent 

    int num = 1;
    int a;
    
    Parent(int a)
    
        this.a = a;
    
    
    void disp( ) 
        System.out.println("Number from Parent class " + num);
    
    
    void getA( ) 
        System.out.println("Value of a " + a);
    


public class Child extends Parent 

    int num = 2;
    
    Child(int a) 
        super( a );
    
    
    void disp( ) 
        System.out.println("Number from Child class " + num);
    
    
    void methodToCallSuperKeyword( ) 
        System.out.println(super.num);
        super.disp();
    

    public static void main(String args[]) 
    
        Child child = new Child(100);
        
        child.methodToCallSuperKeyword();
        System.out.println(child.num);
        
        child.disp();
        child.getA();
    

The output for the above snippet is:

1
Number from Parent class 1
2
Number from Child class 2
Value of a 100

The instanceof operator can be used to check if a particular instance belongs to a class. For an inherited class, the instance of the subclass is an instanceof both subclass and superclass.

Thus in the example above:

System.out.println(child instanceof Child);
System.out.println(child instanceof Parent);

Outputs:

true
true

Based on the example above, Parent is the superclass of Child class. In terms of the IS-A relationship, we can justify Child IS-A Parent. With the extends keyword, all the properties of a subclass are inherited in the superclass, except for the private members.

HAS-A relation is used to determine if a class has a certain thing. For example in the above example the Child class HAS-A methodToCallSuperKeyword().

It must be kept in mind that Java does not support Multiple Inheritance. This means one class cannot extend multiple classes. The concept of multiple Inheritance is implemented via interfaces as discussed before.

Java Recommended  Post

Source link

Have any Question or Comment?

Leave a Reply

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

Categories

You have successfully subscribed to myprogrammingschool

There was an error while trying to send your request. Please try again.

My Programming School will use the information you provide on this form to be in touch with you and to provide updates and marketing.