Krishan Patel
Krishan Patel

Reputation: 5

Calling variable to subclass from superclass

I'm having no luck calling a variable from my superclass to my subclass. Can anyone help out?

//SUPERCLASS
public class Circle {

  protected double radius;
  protected double area;

  //Some code to construct object and initialize radius

  //Return Calculated Area
  protected double getArea() {
    area = Math.pow(radius, 2) * Math.PI;
    return area;
  }

}

//SUBCLASS
public class Cone extends Circle {

  private double height;

//Some more code with constructors and different methods

  public double getVolume() {
    {
      return (area * height / 3)
    }
  }

There is a lot more to the code but the main problem I'm having is within the subclass, the 'area' variable is 0.00 and I'm unsure how to get it equal to the 'area' that is calculated in the superclass

Upvotes: 0

Views: 1406

Answers (5)

Gobi
Gobi

Reputation: 21

Here variable area is an instance variable so it's default value is set to 0.0d. Refer to this link. If you want to change the area value then want to call getArea() method. Check below code,

public double getVolume() {
      return (getArea()* height / 3)
}

Upvotes: 1

Necromancer
Necromancer

Reputation: 929

Add Constructors for both super class and sub-class like the following.

//Super Class
public class Circle {

  protected double radius;
  protected double area;

  public Circle(double radius) {
    this.radius = radius;
    this.area = getArea();
  }

  protected double getArea() {
    area = Math.pow(radius, 2) * Math.PI;
    return area;
  }
}

//Sub Class
public class Cone extends Circle {

private double height;

public Cone(double radius, double height) {
    super(radius);
    this.height = height;
}

public double getVolume() {
    {
      return (area * height / 3);
    }
  }
}

After that, you can use getVolume() method of sub-class.

public class Test {

  public static void main(String[] args) {
    Cone c = new Cone(3.0,5.0);
    System.out.println(c.getVolume());
  }
}

Upvotes: 0

saravana kumaran
saravana kumaran

Reputation: 1

Unless you set the value to radius and area, it remains 0. I assume you've set the value. You should use this keyword to get the set value. It would be easy to find the flaw if you put the entire code and not just hiding it as a comment.

Upvotes: 0

CoronA
CoronA

Reputation: 8095

try:

public double getVolume() {
   return (getArea() * height / 3)
}

Besides: A circle should be initialized with its radius in the constructor and not have a field area, because it is dependent on the radius:

public class Circle {

  protected final double radius;

  public Circle(double radius) {
    this.radius = radius
  }

  public double getArea() {
    return Math.pow(radius, 2) * Math.PI;
  }
}

And a cone is not a proper sub class of a circle, the cone should have a field Circle baseShape.

Upvotes: 1

Zumry Mohamed
Zumry Mohamed

Reputation: 9558

You are using protected its totally fine to inherit the variable to subclass . here is the correct answer

//SUPERCLASS
public class Circle {

  protected double radius;
  protected double area;

  //Some code to construct object and initialize radius

  //Return Calculated Area
  protected double getArea() {
    area = Math.pow(radius, 2) * Math.PI;
    return area;
  }

}

//SUBCLASS
public class Cone extends Circle {

  private double height;

//Some more code with constructors and different methods

  public double getVolume() {
    {
      return (getArea() * height / 3)
    }
  }

Upvotes: 0

Related Questions