0

I have an issue regarding the super keyword in java. Follow the example below:

public class Circle {
private double radius;
private double area; 

public void setRadius(double radius){
    this.radius = 1; 

}
public double getRadius(){
    return this.radius;
}
public void setArea(double radius){
    this.area = area;
}
public double getArea(){
    return this.area = Math.PI * radius * radius;
}
}


public class Cylinder extends Circle {
    private double height;
    public Cylinder(){
        super();
        height = 1;
    }
    public Cylinder(double height){
        super();
        this.height = height;
    }
    public Cylinder(double radius, double height){
        super();
        this.height = height;
        public double getHeight(){
            return height;
        }
    }
public double getVolume(){
    return getArea()*height;
}
}

My point is that, when I use super() in the subclass, how java will know which constructor to call in my subclass ? Since in my superclass, I have two constructors with no arguments; public double getRadius() and public double getArea()

Makoto
  • 104,088
  • 27
  • 192
  • 230
  • A constructor for Circle would be named `Circle`. Since you didn't explicitly declare one, the compiler created one, with no arguments. That's what would be used for all the `super` calls above, since there is no "better" fit. – Hot Licks Oct 12 '14 at 19:10

2 Answers2

5

There is only one constructor in your super class, the default no argument constructor which is not explicitly defined within the class. Each of the subclass's constructors will invoke this constructor in the super class.

getRadius() and getArea() are just methods within the super class, they are not constructors. Remember that constructors always take the form of:

[access modifier] [Class Name](/* arguments optional*/){}

So a constructor for the Circle class would look like:

public Circle(/*Arguments could go here */){

}

If there were more than one constructor in the super class, let's say:

public Circle(int radius){
  //....
}

public Circle(double radius){
  //....
}

The compiler would use the arguments to determine which constructor to call.

Kevin Bowersox
  • 93,289
  • 19
  • 159
  • 189
  • Can you please elaborate further? – user3545953 Oct 12 '14 at 19:09
  • On which part? The most important thing to grasp is that `getRadius` and `getArea` are not constructors they are regular methods. The only constructor for the super class is the default constructor, which the compiler adds to the class if no constructor is present. – Kevin Bowersox Oct 12 '14 at 19:12
  • Can you explain on 'this' keyword ? For example, what does this mean? this.name = name; – user3545953 Oct 12 '14 at 19:19
  • This refers to the current object or the instance of the class type. So `this.name` refers to the `name` variable declared on the class, as opposed to a local variable named `name`. This is a good way to distinguish between a class instance variable and a local variable. I'm not sure of your comfort level with Java, but it might be a good idea to pick up a reference book. Also see this answer: http://stackoverflow.com/a/3728188/714969 – Kevin Bowersox Oct 12 '14 at 19:36
0

Constructor name will be the class name. You are talking about the methods there.

class classname{

   classname(){// constructor name as class name.
   }

}
Android
  • 54
  • 5