->Is this possible?
Yes, of course. As long as your child classes implement all the abstract methods.
Actually this is the concept Polymorphism :Polymorphism is the provision of a single interface to entities of different types.
Ref to your code, the hierarchy is:
DarthVader
│
─┴─
│ │
Jedi Sith
In DarthVader you declared two abstract method: method1()
and method2()
. Then you have their implementation in child classes.
Polymorphism is that you use the abstract reference to point to an implementation class object. Like this:
DarthVader j = new Jedi();
j.method1();
For the JVM, it will correctly call the method1()
in Jedi. This concept is called Dynamic dispatch. In modern languages, it's implemented by vTable.
You can take a look at the references.