Write a program to describe the use of extends and super keywords in java taking an example of rectangle and square?

Introduction:

Hii everyone, today we are going to learn about very interesting keywords in java i.e. extends and super.It’s good to know the advantages first so that we can understand the speciality of the keyword. Java supports inheritance means we can inherit the features of a parent in a child with its add-on features, in java programming parent class is known as “superclass” and child class is known as “subclass”, extends is a keyword which is being used to add some features in a subclass.super() can be used to call the constructor of immediate superclass from the subclass constructor.

Description Of Program:

Here we are taking an example of rectangle and square, before initiating this example we need to take a decision that what should be considered as a superclass a rectangle or a square.After analysing we came to the conclusion that rectangle is a superclass and square is a subclass.First of all, we will code for the superclass i.e. rectangle.In rectangle code, we will import a scanner function so that we can make the function a user-flexible function, then we will make a constructor then we will make functions to find the area, perimeter and diagonal of the rectangle. Moving ahead, here is the explanation for subclass i.e. square.Initially, we will extend the subclass  square to superclass rectangle and will use super keyword so that the constructor of the rectangle class can be used here, again you will see we make a function and in that function “a” parameter is stored in “l” and “b”, then in main function just call the function of the superclass and we get the features in subclass.Hope you find it helpful.

Thank You!

CODE For Superclass”Rectangle Class”:

package oops;

import java.util.Scanner;
public class Rectangle {
double l,b;
public Rectangle(){
Scanner s=new Scanner(System.in);
System.out.println(“Enter the value of lenght and breadth taken in rectangle”);
l=s.nextDouble();
b=s.nextDouble();
}

public Rectangle(double l, double b) {//Constructor of rectangle function
this.l = l;
this.b = b;
}

public double area(){
return l*b;
}

public double per(){
return 2*(l+b);
}
public double diagonal(){
double d=(l*l+b*b);
d=Math.sqrt(d);
return d;
}

public static void main(String[] args) {
Rectangle r=new Rectangle();
System.out.println(“Area Of Rectangle= “+r.area());
System.out.println(“Perimeter Of Rectangle= “+r.per());
System.out.println(“Diagonal Of Rectangle= “+r.diagonal());
}
}

CODE For Subclass “Square Class”:

package oops;

import java.util.Scanner;

public class Square extends Rectangle{
public Square(double a)
{
super(a,a);
}

public Square()
{
super(0,0);
System.out.println(“Enter the side”);
Scanner s=new Scanner(System.in);
double a=s.nextDouble();
l=a;
b=a;
}
public static void main(String[] args) {
Square s= new Square();
System.out.println(“Area= “+s.area());
System.out.println(“Diagonal= “+s.diagonal());
System.out.println(“Perimeter= “+s.per());
}
}

OUTPUT Of SuperClass “Rectangle” and SubClass “Square”

Leave a Reply