Hassaan Hafeez
Hassaan Hafeez

Reputation: 107

Get Return Integer Value from a super class

This is the code I have in my first class:

public class Factorial {
public void Sequence(){
    String value = JOptionPane.showInputDialog("Please enter the number of which you want your shit.");
    System.out.println("Factorial:");
    System.out.println(fact(Integer.valueOf(value)));
    getValues(Integer.valueOf(value));
}

public static int getValues(int n){
    return n;
}

And this is a subclass of Factorial:

public class Fibonacci extends Factorial {
    public void Sequence(){
        Factorial get = new Factorial();
                //not working EDIT: Just realized this obv wont work, but still need help to get it to work
        get.getValues(n);

However I cannot get the Value returned in the getValues from the Factorial Class

Upvotes: 0

Views: 1777

Answers (2)

boxed__l
boxed__l

Reputation: 1336

Here (below) value is used to store user input

public class Factorial {
public String value;
public void sequence(){
    value = JOptionPane.showInputDialog("Please enter the number of which you want your shit.");
    System.out.println("Factorial:");
    System.out.println(fact(Integer.valueOf(value)));
    getValues(Integer.valueOf(value));
}

public static int getValues(int n){
    return n;
}
public String getValue(){return value;}
}

public class Fibonacci extends Factorial {
    //Method sequence is overridden here
    public void sequence(){
//can use getValue() here or anywhere...there is no need to use a separate object due to inheritance
}
}

Upvotes: 2

Radiodef
Radiodef

Reputation: 37875

Polymorphism is not really a bad abstraction here but there's not really a way to "help you fix what you're doing". There's sortof obvious reasons polymorphism just doesn't work the way you're trying to use it. As I mentioned, one of them being that static methods can't be overridden so your getValue can only ever return the input number. The other reason is that you're creating an instance of a superclass from within a subclass and there's never a reason this is necessary. You also proceed to call a static method on said superclass instance which is also unnecessary.

So the short of it is that while you came here to ask a question about a compiler error related to the variable n, your code shows that you are way ahead of yourself. If you want to learn polymorphism, you should start with a tutorial. What you said in a comment--"want the value of n (the user input) to be used in multiple classes"--your code in the OP isn't going to help you achieve that because it's fundamentally faulty polymorphic design. I don't mean that as an insult, it just is and you should probably start over.

Here is a more typical design that you can use as a guideline:

public class Main {
    public static void main(String[] args) {
        String input = JOptionPane.showInputDialog("Enter a number:");
        int n = Integer.parseInt(input);

        System.out.println(new Factorial().getResult(n));
    }
}

public interface Sequence {
    public int getResult(int n);
}

public class Factorial
implements Sequence {
    @Override
    public int getResult(int n) {
        int result = n;
        while(n > 1) {
            n--;
            result *= n;
        }

        return result;
    }
}

There is also not really an object-oriented reason here for Fibonacci to extend Factorial.

If you really wanted to include retrieving input with the Sequence class, you could do it neatly like this:

public abstract class Sequence {
    private static int getInput() { // static because it relies on no state
        return Integer.parseInt(
            JOptionPane.showInputDialog("Enter a number:")
        );
    }

    public final int getResultFromInput() { // simply calls getInput and getResult
        return getResult(getInput());       // sub classes will not need to do
    }                                       // anything with this method

    public abstract int getResult(int n);   // sub classes override this method
}                                           // to provide their functionality

public class Factorial
extends Sequence {
    @Override
    public int getResult(int n) {

        // same as above factorial

    }
}

And then you can do this:

System.out.println(new Factorial().getResultFromInput());

I would argue it's not really OOP-correct for a number sequence object to do things like show dialog boxes but there's not a reason you can't do it.

Also, as a minor side note, you should use long instead of int. Factorials will result in overflow very quickly with int (after 12!). Not sure when exactly you get overflow from a Fibonacci sequence, certainly not as soon as factorial.

Upvotes: 1

Related Questions