-1

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?

Community
  • 1
  • 1
Siddhant Rimal
  • 975
  • 2
  • 11
  • 32
  • 3
    Your code is in a bit of mess, and I'm not even going to fix the `super()` problem, because `PI` is a constant, and should probably be declared as a `static final` field in one of the classes, possibly all of them if needed. Don't pass a constant through a constructor, just define it, and then use it. – Tim Biegeleisen Mar 23 '17 at 05:33
  • *I've not given a return type in my constructor* does not even make sense – Scary Wombat Mar 23 '17 at 05:33
  • 1
    You have made a mess of this question. Please spend some time at http://stackoverflow.com/help/how-to-ask – Mohammad Faisal Mar 23 '17 at 05:56
  • @ScaryWombat It shouldn't make sense. Apparently, [this guy did just that](http://stackoverflow.com/q/16407455/5040900) and since his question was pretty similar, I just tried to clear it up. Obviously, it backfired. I'll remember to exclude any useless information in the future questions. – Siddhant Rimal Mar 23 '17 at 08:24

4 Answers4

2

Why is it not working for my code?

Because super(pi) should be called from Circle(double pi) as

Circle(double pi){
  super(pi);
  this.pi = pi;
}

not from soutresult().


super(pi) is actually a call to the constructor of parent and in your case, this can not be called from soutresult().

Also, in your Calculate class you must add a constructor with receiving double type argument as:

Calculate(double pi){
  this.pi = pi;
}

For more info on super() have a look at super() in Java

Community
  • 1
  • 1
Mohammad Faisal
  • 5,783
  • 15
  • 70
  • 117
0
class Calculate{
    double radius = 10.0;
    double pi;

    Calculate(){}

    Calculate(double pi) {
    this.pi = pi;
    }

    final double circumference(){  //final method cannot be overridden
        return 2*pi*radius;
    }
}

final class Circle extends Calculate{ //final class cannot be extended

    Circle(){}

    Circle(double pi){
        super(pi);
    }

    public void soutresult(){
        System.out.println("The circumference of Circle is = "+circumference());
    }
} 

super() can only be called in constructor. however you can call super.memberfunctionane in other member functions. when you create an object with Circle(4.3) super gets called however since Calculate(double pi) initializes pi, you need to do this.pi = pi and need not declare another variable with the same name

saikumarm
  • 1,565
  • 1
  • 15
  • 30
0

Along with many of the comments, I don't quite get what you are trying to do here.

However, the proximate cause of your error is that you are trying to call super() from the method public void soutresult() This does not work, as the method Is not a Constructor. Super is a very special call, which can only be used within a constructor.

Constructors do not have a return type. A Constructor for your Circle would look like public Circle(double testPI) Calling super(testPi); is the equivalent of saying "Set up the Calculate parts of me using the testPI value." You would also need to have an appropriate public Calculate(double pi) constructor defined in the Calculate class.

SailsMan63
  • 239
  • 3
  • 14
  • Thanks for the thorough explanation. I learnt about super() only very recently and I'm not quite familiar with it yet. This question proves that my concept was unclear or outright ambiguous. As for the intent of the question, [please refer here](http://stackoverflow.com/a/42970470/5040900) – Siddhant Rimal Mar 23 '17 at 08:16
  • Also, I was confused because in a hurry to see different answers I mistook Oval for a method, instead of a constructor [here](http://stackoverflow.com/a/22103914/5040900). – Siddhant Rimal Mar 23 '17 at 08:21
0

Heeding to popular suggestions, I've changed by code a little bit since. This is the new version. It passes on non-final variable instead of pi. I've used definition of pi natively. While there were concerns about passing a final variable, and some unfortunate confusions, my intent was to show another snippet of code I had read earlier. Now, I understand why my code didn't run earlier. I appreciate everybody's feedback and would also like to know what I could do to make this better(because someone mentioned that my code's a big mess. Kindly point out so that I can learn)

This is the newer version. I've commented to describe the objective. This is obviously a vague exercise that I was simply attempting to make interesting. I would appreciate feedback on how to make this better(and its shortcomings!) while remaining faithful to the objectives outlined below.

/**
 * WAJP to calculate circumference of circle using final & this keyword
 * 
 * We shall demonstrate the following things:
 * - Final variable can only be set once during runtime
 * - Final method cannot be overridden with @Override annotation
 * - Final class cannot be extended
 * - 'this' keyword is used to target the class that it is in
 * 
 * Execution Method:
 * - There are 4 classes with Question2 as the class with main function.
 * - The Handler class handles Get-Post request and creates abstraction layer
 * - Question2 demonstrates final variable property
 * - Handler demonstrates this keyword
 * - Circle demonstrates final class property
 * - Calculate demonstrates final method property
 */
package on20170322;

/**
 *
 * @author Siddhant
 */
public class Question2{
    final double radius =10.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 radius;
    Handler(){
        this.radius=q2.radius;
    }
    void sendFinalObject(){
        circle= new Circle(radius);
    }
    Circle getFinalObject(){
        return circle;
    }
}

class Calculate{
    double radius;

    Calculate(double radius){
        this.radius = radius;
    }

    final double circumference(){  //final method cannot be overridden
        return 2*radius*Math.PI;
    }
}

final class Circle extends Calculate{ //final class cannot be extended

    Circle(double radius){
    super(radius);
    }

    public void soutresult(){
        System.out.println("The circumference of Circle, with radius(r)="+radius+", is (p)="+circumference());
    }
//    Overriding final methods is not possible
//    @Override
//    double circumference(){
//        return 2*radius*Math.PI;
//    }
}    
Community
  • 1
  • 1
Siddhant Rimal
  • 975
  • 2
  • 11
  • 32