This question might sound redundant because the title is exactly the same as this other question. However, there is a simple difference - I've not given a return type in my constructor. With that said, I must be doing something else thats equally stupid and I can't seem to figure out what. I've seen a similar execution work here. Why is it not working for my code?
This is part of the code to demonstrate how final keyword works(ignore the comments):
class Calculate{
double radius = 10.0;
double pi;
final double circumference(){ //final method cannot be overridden
return 2*pi*radius;
}
}
final class Circle extends Calculate{ //final class cannot be extended
double pi;
Circle(){}
Circle(double pi){
this.pi = pi;
}
public void soutresult(){
super(pi);
System.out.println("The circumference of Circle is = "+circumference());
}
}
Problem Description
The trouble is, this answer shows a nice execution of the same thing, while in Netbeans, I'm getting call to super must be first statement in constructor
error. The error shows at super(pi)
. I want to be able to send the value of pi from Circle to Calculate
To Clarify, here's the complete code
package on20170322;
/**
*
* @author Siddhant
*/
public class Question2{
final double pi =22/7.0; //final variable sets constant value
public static void main(String[] args) {
Handler handler = new Handler();
handler.sendFinalObject();
Circle circle = handler.getFinalObject();
circle.soutresult();
}
}
class Handler{
Question2 q2 = new Question2();
Circle circle;
double pi;
Handler(){
this.pi=q2.pi;
}
void sendFinalObject(){
circle= new Circle(pi);
}
Circle getFinalObject(){
return circle;
}
}
class Calculate{
double radius = 10.0;
double pi;
final double circumference(){ //final method cannot be overridden
return 2*pi*radius;
}
}
final class Circle extends Calculate{ //final class cannot be extended
double pi;
Circle(){}
Circle(double pi){
this.pi = pi;
}
public void soutresult(){
super(pi);
System.out.println("The circumference of Circle is = "+circumference());
}
}
What am I missing?