Java11: Inheritance

Introduction to Inheritance

Inheritance is one of the Object-Oriented Programming concepts. It allows a child class inherits the fields and methods of a parent class. A child class is also called a subclass or derived class. A parent class is also called a superclass or base class.

A UML diagram uses an unfilled triangle and a line to show the inheritance relationship between Parent and child class.

In java, inheritance is specified using the extends keyword as shown below.

class ChildClass extends ParentClass{
    // statements
}

Example to demonstrate how inheritance is implemented.

//file name: Shape.java
class Shape{
    double area;
 
    double getArea(){
        return this.area;
    }
}
//file name: Rectangle.java
class Rectangle extends Shape{
    double length;
    double width;
    double calculateArea(){
        area = length * width;
        return area;
    }
}
//file name: ShapeDemo.java
class ShapeDemo{
    public static void main(String[] args){
        Rectangle rect = new Rectangle();
        rect.length = 1.3;
        rect.width = 2.5;
        System.out.println("Area " + rect.calculateArea());
    }
}

Output

Area 3.25

The parent class is Shape and the child class is Rectangle. The area field is inherited by the Rectangle class from Shape.

Method Overriding

A method in the parent class can be overridden by the child class by defining its own functionality. This allows a child class to define its own behavior for the same method as its parent.

//file name: Shape.java
class Shape{
    double area;
    void draw(){
        System.out.println("Drawing shape");
    }
}
//file name: Circle.java
class Circle extends Shape{
    void draw(){
        System.out.println("Drawing Circle");
    }
}
//file name: ShapeDemo.java
class ShapeDemo{
    public static void main(String[] args){
        Circle cir = new Circle();
        cir.draw();
    }
}

Output

Drawing Circle

Both Shape and Circle class has a method called draw(). We learned that the child class will inherit the property and method of the parent class, but in this situation, the method is overridden by draw() in the Circle class. This is similar to overloading but between two classes.

Super keyword

A super keyword is used to access the constructor or method or event property of a parent class. To access the parent class constructor,

super(param);

Example

//file name: Shape.java
class Shape{
    double area;
    Shape(){
        this.area= 0;
    }
    Shape(double area){
        this.area = area;
    }
    double getArea(){
        return this.area;
    }
}
//file name: Rectangle.java
class Rectangle extends Shape{
    double length;
    double width;
    Rectangle(){
       this.length = 0;
       this.width = 0;
       super();
    }
    double calculateArea(){
       area = length * width;
       return area;
    }
}

To access the parent class method

super.methodName(param);

Example

//file name: Rectangle.java
class Rectangle extends Shape{
    double length;
    double width;
    double calculateArea(){
        area = length * width;
        return area;
    }
    void getArea(){
        super.getArea();
    }
}

A post by Cuber

Comments

Popular posts from this blog

Drawing Simple Pie Chart

VB.net connecting to SQL Server

VB.net