I found this code of javatpoint. But when I try to compile this it gives me an error.
Exception in thread "main" java.lang.Error: Unresolved compilation problems:
No enclosing instance of type Hierarchical is accessible. Must qualify the allocation with an enclosing instance of type Hierarchical (e.g. x.new A() where x is an instance of Hierarchical).
No enclosing instance of type Hierarchical is accessible. Must qualify the allocation with an enclosing instance of type Hierarchical (e.g. x.new A() where x is an instance of Hierarchical).
at Inheritance.Hierarchical.main(Hierarchical.java:28)
Why am I getting this error ?? Eclipse
tells me to make the classes static but I don't understand why ??
package Inheritance;
public class Hierarchical {
static void bayern() {
System.out.println("bayern");
}
class Hierarchical2 extends Hierarchical {
static void barcelona() {
System.out.println("barcelona");
}
}
class Hierarchical3 extends Hierarchical {
static void Madrid() {
System.out.println("madrid");
}
}
public static void main(String[] args) {
Hierarchical2 h2 = new Hierarchical2();
Hierarchical3 h3 = new Hierarchical3();
h2.barcelona();
h2.bayern();
h3.Madrid();
h3.bayern();
}
}