TransWikia.com

How to call the method of the subclass using method of Parent class?

Stack Overflow Asked by NmdvDisha on February 3, 2021

I have a Shape method that has two arguments first is the width and the second is the height. I have two subclasses, one is the rectangle and another is triangle. I want to call the area()
the method defined in both triangle and rectangle with the help of the area() of the Shape class.
I have written this piece of code but when I am calling the area() method of derived classes using the area() method of the parent class, I am getting an error.
So how to do this?

public class Shape {

    double width, height;

    public Shape(double w, double h)
    {
        this.height = h;
        this.width = w;
    }
    public void area(Object shape){ // area method of parent class
       shape.area(); // here I am getting error.
    }
}
class triangle extends Shape{

    triangle tri;
    public triangle(double w, double h) {
        super(w, h);
    }
    public void area()// area method of derived class
    {
        double area = (1/2)*width*height;
        System.out.println("The area of triangle is: "+area);
    }
}

class rectangle extends Shape{

    rectangle rect;
    public rectangle(double w, double h) {
        super(w, h);
    }
    public void area() // area method of derived class
    {
        double area = width*height;
        System.out.println("The area of rectangle is: "+area);
    }
}

2 Answers

You want to override the method and let the subclasses implement it. You do not need to call any method from Shape.area() at all!

public abstract class Shape {
    float width, height;
    Shape(float width, float height) {
       this.width = width;
       this.height = height;
    }
    public abstract float area();
}

public class Rectangle extends Shape {
    public Rectangle(float width, float height) {
       super(width, height);    
    }
    @Override
    public float area() { return width * height; }
}

public class Triangle extends Shape {
    public Triangle(float width, float height) {
        super(width, height);
    }
    @Override
    public float area() { return (width*height) / 2; }
}

With that in place, you can do:

Shape shape = new Triangle(50f, 50f);
float areaOfTri = shape.area(); // dispatches to Triangle.area()

shape = new Rectangle(50f, 50f);
float areaOfQuad = shape.area(); // dispatches to Rectangle.area()

Answered by Polygnome on February 3, 2021

As @Turing85 pointed out abstract methods and classes are the thing you are looking for. If you want to understand the concept there are a lot of helpful articles out on the internet.

What would help you is the following:

public class Shape {

    double width, height;

    public Shape(double w, double h)
    {
        this.height = h;
        this.width = w;
    }

    abstract public void area()
}

class triangle extends Shape{

    triangle tri; //Not sure if you need this. If you want to access a object within 
                  //itself use this
    public triangle(double w, double h) {
        super(w, h);
    }

    public void area()// area method of derived class
    {
        double area = (1/2)*this.width*this.height;
        System.out.println("The area of triangle is: "+area);
    }
}

class rectangle extends Shape{

    rectangle rect;
    public rectangle(double w, double h) {
        super(w, h);
    }
    
    public void area() // area method of derived class
    {
        double area = this.width*this.height;
        System.out.println("The area of rectangle is: "+area);
    }
}

Explanation: The abstract keyword in the parent class is used when you want all classes that extend this class to implement a certain method - in this case the area() method - but the implementations differ. If you don't implement area() in a class that extends Shape, you will get an error.

As commented in the triangle class you don't need to save a reference to the object itself within the object. For this use case, you would use this. E.g. this.area() or this.width.

And last but not least, you could change your area method to return the double value of the area. That is not the problem but could later help you if you want to process that value further.

Answered by Max Hockeborn on February 3, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP