2
public class PolyMorphic {

public static void main(String[] args) {

    PolyMorphic.printNumber(new IntNumber(1));
    PolyMorphic.printNumber(new DoubleNumber(4.54));

}

public static void printNumber(MyNumber N) {
    N.print(N);
    System.out.println();
}

public abstract class MyNumber{
    abstract void print(MyNumber N);
}

public class IntNumber extends MyNumber{
    int x;

    IntNumber(){
        x = 3;
    }

    IntNumber(int x){
        this.x = x;
    }


    void print(MyNumber N) {
        double temp = (double)x;
        System.out.printf("%.2f",temp);

    }
}


public class DoubleNumber extends MyNumber{
    double x;

    DoubleNumber(){
        x = 3.23;
    }

    DoubleNumber(double x){
        this.x = x;
    }


    void print(MyNumber N) {
        double temp = x;
        System.out.printf("%.2f",temp);

    }
}

 }

So I am trying to create a method in the PolyMorphic class named printNumber which is polymorphic and can print(to the console) either an intNumber with two decimal places to the right or a DoubleNumber with three decimal places to the right. Such as PolyMorphic.printNumber(new IntNumber(1));

My Problem is this:

On the Lines:

PolyMorphic.printNumber(new IntNUmber(1));
PolyMorphic.printNumber(new DoubleNumber(4.54));

This is the error message:

" No enclosing instance of type PolyMorphic is accessible. Must qualify the allocation with an enclosing instance of type PolyMorphic (e.g. x.new A() where x is an instance of PolyMorphic)."

It gives me it for both instances and I am confused to as why It is not working. IF someone could just point me in the right direction I would be really appreciative.

Thank you.

Sastrija
  • 3,284
  • 6
  • 47
  • 64
sealsix
  • 73
  • 1
  • 2
  • 9
  • Possible duplicate of [Java - No enclosing instance of type Foo is accessible](http://stackoverflow.com/questions/9560600/java-no-enclosing-instance-of-type-foo-is-accessible) – fabian Mar 04 '16 at 00:32

2 Answers2

1

Your inner classes require an instance of your PolymorphicClass because of the way you declared them. However, in your case, you don't need this, so you can mark your inner classes as static:

public static class IntNumber

and

public static class DoubleNumber

This is a Java design feature.

One other solution would be to operate on an instance of PolymorphicClass:

Polymorphic p = new Polymorphic();
p.printNumber(new IntNumber(1));
p.printNumber(new DoubleNumber(4.54));

EDIT:

You also need:

public static abstract class MyNumber
Luchian Grigore
  • 253,575
  • 64
  • 457
  • 625
  • Sorry, New to the website and was just anxious about getting a solution. I appreciate your help, and your timely answer. – sealsix Dec 13 '11 at 09:40
1

Don't nest your MyNumber class and its daughters inside of Polymorphic.

Nesting classes like that is only appropriate when the nested class (MyNumber, IntNumber, DoubleNumber) is part of the implementation of the enclosing class (Polymorphic). In your case, the only relationship between the two classes is that Polymorphic is calling methods on the Number classes.

By the way, the compiler has already told you one solution to your problem, if you would take the trouble to read and understand what it said. Be grateful, for few compilers are as obliging.

Edit - why is anyone downvoting a reply that is both correct and adds additional information to that provided by other answers?

Andrew Spencer
  • 15,164
  • 4
  • 29
  • 48
  • It has to be done this way, the professor specifically wants it this way, and I know it tells me what to do but I can't get it to work, I've been up for two days finishing other finals and this is the last thing giving me trouble and it really shouldn't be. – sealsix Dec 13 '11 at 09:29
  • Seriously though, keeping the classes nested but declaring them static, as the other posters suggested, will achieve a working solution - but is less good design. – Andrew Spencer Dec 13 '11 at 09:37