Home > Mobile >  No enclosing instance of type 'Abstraction' is accessible
No enclosing instance of type 'Abstraction' is accessible

Time:09-13

I am getting this No enclosing instance of type 'Abstraction' is accessible problem while trying an abstraction class .

public static void main(String[] args) {
  Horse h = new Horse();    //getting problem here`enter code here`
  h.walk();
   
}

abstract class Animal {  
 
abstract void walk();
}


class Horse extends Animal {
    void walk() {
        System.out.println("Walks on 4 legs");
    }
}

can anyone please help me to solve this problem

CodePudding user response:

Animal is declared as an inner class, which means it must be instantiated using an instance of the enclosing class (which you have not shown).

Change Animal and Horse to be static classes.

public static void main(String[] args) {
    Horse h = new Horse();
    h.walk();  
}

static abstract class Animal {  
    abstract void walk();
}


static class Horse extends Animal {
    void walk() {
        System.out.println("Walks on 4 legs");
    }
}
  • Related