0

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

1
  • 2
    You should run getArea() before using the area. Commented Jun 10, 2016 at 3:55

5 Answers 5

1

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.

Sign up to request clarification or add additional context in comments.

Comments

1

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) } 

Comments

0

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()); } } 

Comments

0

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) } } 

Comments

0

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.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.