-1

I am learning inheritance in Java, and I am trying to solve the error in the following code:

public static void main(String[] args) {
    Yellow yellow=new Yellow("yellow");
    yellow.ChangeColor();
}
abstract class SpecialColor{
    String color;
    
    SpecialColor(String color){
        this.color=color;
    }

    abstract void ChangeColor();
    
    public String ShowColor(String color) {
        return color;
    }
    
}

class Yellow extends SpecialColor{

    Yellow(String color) {
        super(color);
        
    }
    
    @Override
    void ChangeColor() {
        color="yellow";
        System.out.println("The color is "+color);
    }
    
}

The task I am trying to do basically says that I need to create an abstract class SpecialColor with 2 properties - the color name (String) and two methods - abstract void ChangeColor(), the other displays the color name, while class Yellow extends this class and implements abstract methods and the name needs to change to "yellow", and to show the use of these classes in main. The error I have is in the 1st line of code in the main method - "No enclosing instance of type is accessible", which I tried to solve making the Yellow class static, but then a new error pops up on super(color) line that says "No enclosing instance of type sestijanuar8 is available due to some intermediate constructor invocation". I am having a hard time understanding abstract classes and methods, so any form of help would be appreciated!

  • I think these two classes and your main method are all inside another class that you have omitted to post. Please post a [mre]. – khelwood Jan 07 '22 at 18:18
  • I suggest one class per file for beginners. It helps you focus on the main concepts. – japhy Jan 07 '22 at 18:22
  • Yes, they are, but I was hesitant to copy-paste the entire code. My sincere apologies for the long post, I am fairly new here and I am still understanding how posting on StackOverflow works. – Igor Heslenji Jan 07 '22 at 18:23
  • I read it and found useful information. Thank you again for helping me! – Igor Heslenji Jan 09 '22 at 13:24

2 Answers2

1

Your classes are inside another class, and they are not static, so they are inner classes. That means they can only be instantiated with reference to an instance of the containing class.

Either move them out of the containing class, or declare them static. Then you will be able to instantiate them.

khelwood
  • 55,782
  • 14
  • 81
  • 108
0

Because of your error message "No enclosing instance of type is accessible", I can deduce that all your code is within an enclosing class, which makes sense, because otherwise your main method isn't in a class, which isn't allowed.

Make both classes SpecialColor and Yellow static. If you don't, then the class is a within a particular instance of the enclosing class, which you don't have, because you're in main, a static method.

If you make one of the classes static but not the other, then you still have the same problem, just maybe in a different location.

Nothing you've written indicates that an enclosing class instance is necessary for the operation of your classes, so just make both of them static, so that they don't depend on an instance of the enclosing class, which you aren't using.

rgettman
  • 176,041
  • 30
  • 275
  • 357