JAVA CHALLENGE #7: Interfaces and Abstract classes

Who is stronger? Master Yoda or Darth Vader? What will the output be? Solve this challenge and find out!

abstract.PNG

Before seeing the answer below, try it out!

Answer: At first we create an anonymous inner class of Darth Vader, that means any class that implements DarthVader interface. We have only one getNumber() method.

With MasterYoda, things are different. We created an abstract class with the getNumber() method and another one in the inner class of MasterYoda. This is the same as DarthVader, it is an anonymous inner class. This challenge is pretty tricky because you can see that there is a method overloading both methods. Which one will be invoked? See that we are passing a new Integer(FORCE) for the getNumber() method. For this reason, the method from the abstract class will be invoked and MasterYoda will have the force of 20.

The answer is:
30
The stronger Jedi is: Master Yoda

May the force be with you!

Written by
Rafael del Nero
Join the discussion

2 comments
  • Hi Ng Sharma, not that the getNumber is overloaded, not overridden, that makes all the difference because since we are passing a new Integer and not a primitive int, the MasterYoda method will be invoked not the LukeSkywalker method. Keep the code on!