1

I'm new to Java and is trying to learn the concept of inheritance. When I tried to call the EggLayer's identifyMyself() method from the main class's identifyMyself method() using

System.out.println(EggLayer.super.identifyMyself());

it works as expected. However, when I tried to call the EggLayer's identifyMyself() method from the main class's main method() using the same statement, the compiler generate an error saying:"not a enclosing class: EggLayer".

Could someone please explain to me why this is the case?

  interface Animal {
    default public String identifyMyself() {
        return "I am an animal.";
    }
}

 interface EggLayer extends Animal {
    default public String identifyMyself() {
        return "I am able to lay eggs.";
    }
}

 interface FireBreather extends Animal { 
     @Override
     default public String identifyMyself(){
         return "I'm a firebreathing animal";
     }
 }

public class Dragon implements EggLayer, FireBreather {
    public static void main (String... args) {
        Dragon myApp = new Dragon();
        System.out.println(myApp.identifyMyself());
        /**
        *Not allowed, compiler says "not a enclosing class: EggLayer"
        *System.out.println(EggLayer.super.identifyMyself());
        */
    }

    public String identifyMyself(){
        //Call to EggLayer.super.identifyMyself() allowed
        System.out.println(EggLayer.super.identifyMyself());
        return "im a dragon egglayer firebreather";

    }
}
halfer
  • 19,824
  • 17
  • 99
  • 186
Thor
  • 9,638
  • 15
  • 62
  • 137

1 Answers1

1

the problem in your code is that your dragon class implements two interfaces with : default public String identifyMyself() method both returning different things . Also eggLayer is not a class it is an interface

Agbalaya Rasaq
  • 125
  • 1
  • 10