I’m hoping someone can help me out. I’ve been researching this problem for hours and nobody else seems to have had the same issue (that I’ve come across). I must be making some really bonehead mistake.
I’m new to Java and have designed a very simple Java program.
I have an abstract superclass called Student. This Student superclass has 3 extending classes called Graduate, Undergraduate, and PartTime. The Student class has several abstract methods and several non-abstract methods. I’ve verified that all abstract methods defined in Students have been implemented in all 3 extended classes.
Now I’m trying to do something that should be extremely simple. I’m trying to instantiate one of these extended classes. If I do this:
Student student = new Student();
Netbeans says Student is abstract; can't be instantiated. Ok, fine, I understand why abstract classes cannot be instantiated. Then I try this:
Student.Graduate student = new Graduate();
And Netbeans says an enclosing instance that contains studentmanager.Student.Graduate is required (studentmanager is my package name). I can’t figure out what that means. However, I did figure out that I can instantiate Student like this without errors:
Student[] student = new Student[1];
However, if I then try to do the next logical thing:
student[0] = new Graduate();
I get the same an enclosing instance... error.
Bottom line is I’d like to know how I can instantiate Graduate. Can anyone help me out? Any insight would be greatly appreciated!
Thanks.
Did you define
Graduate,Undergraduate, andPartTimeas inner classes ofStudent? That is, like this:Instances of inner classes have an implicit reference to an instance of the enclosing class, so that it can do things like access the enclosing instance’s private fields and methods. When you try to instantiate an instance of one of the subclasses, the compiler says “Hey, hold up, I don’t know what to set the implicit reference to!” and produces an error.
There are three possible solutions:
Studentclass into their own top-level classes defined in their own files.staticin front of the declarations of the subclasses. This tells the compiler that you do not want an implicit reference from the inner class to the enclosing class. This will prevent you from doing things like accessingStudent‘s private fields and methods from a subclass, but if you don’t need to do that anyway, this is moot.If you do need the subclasses to access private members of
Student, you can create an instance ofStudentand then use this special syntax to create an instance of the inner class.Student s = new Student();Graduate g = s.new Graduate();The
s.newin this example tells the compiler that the new instance ofGraduateshould have an implicit reference tos.Note that in this example this is very unlikely to be what you want. It is often useful to have instances of inner classes that can directly modify their enclosing instance’s fields for doing things like updating the state of the enclosing class in response to a callback, but I suspect that is not your intention here.