Couldn't be able to access the Methods and Variables by using Inheritance Concept

53 views Asked by At

In the class level, I've created methods and declared variables. When I'm calling the methods in the main method, it's throwing an error as :

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

package day11;

public class HirachyInha {

    class Parent {
        void display(int a) {
            System.out.println(a);
        }
    }

    class Child1 extends Parent {
        void show(int b) {
            System.out.println(b);
        }
    }

    class Child2 extends Parent {
        void print(int c) {
            System.out.println(c);
        }
    }

    public static void main(String[] args) {
        Child1 child1 = new Child1();

        child1.display(100);
        child1.show(200);
    }
}

Not understanding why am getting an error while calling the methods from main method.

2

There are 2 answers

0
hsb On

use the keyword static with the inner classes. that should take care of this particular problem.

0
Robert On

You are nesting the Parent, Child1, and Child2 classes within the HirachyInha class. That means they are tied to an instance of the outer class, here HirachyInha. But the main method is static (i.e., not bound to an instance of the HirachyInha class), and you don't create an instance of the HirachyInha class, so you cannot access the inner classes. See the nested / inner classes tutorial for details.

Usually you use inner classes for classes that are tied closely together, for example, the Map.Entry is closely tied to Map.

To get your example to work, you can either move the inner classes out of HirachyInha (still within the same .java file) or give each class its own .java file. I'd usually go with each class in its own file, unless the inner classes are really a detail of the outer class, like Map with MapItem. Having one class per file is Java convention and it makes it easy to find a class.

Alternatively, you can declare the inner classes as static (e.g., static class Parent). That way you don't need an instance of HirachyInha to use them. Again, I'd only do this when these classes are really related. In your case it looks like you just put them into HirachyInha to have only one class in the file, or for grouping reasons, but not because they are logically related.

If you keep non-static inner classes, you can use them as member variables of the outer class, for example:

Child1 c1;

public HirachyInha() {
    c1 = new Child1();
}

public static void main(String[] args) { 
    HirachyInha hi = new HirachyInha();
    hi.c1.display(100);
}

Or, as the compiler error says:

public static void main(String[] args) {
    HirachyInha hi = new HirachyInha();
    Child1 c = hi.new Child1();
    c.display(100);
} 

(Unrelated: there is no error thrown; throwing implies that something can catch the exception. You get a compiler error. Your class file does not even exist, and can certainly not catch anything.)