Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Calling unimplemented abstract method

 
Thomas Hauck
Ranch Hand
Posts: 95
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is from an Enthuware test question.

The code in Example 1 compiles .

Line numbers with respect to Example 1:
line 1. Assign null to the local variable x of type Calculator .
line 2. Call the abstract method.

Example 1:


Question: Why does the Java compiler allow you to call an abstract method ?

----
In practice you would not write code like this.
The purpose of the test question is to show you that it does compile.

It would seem more logical to me that the compiler would require that the abstract method be implemented in a concrete subclass before being called.

The compiler does not give an error saying: "Cannot call abstract method"



____________________________________________________________________
I know that you can call a method using an interface reference variable as in

Example 2:



But in example 2, the class House contains a concrete implementation of the abstract method clean().




 
R. Jain
Ranch Hand
Posts: 375
1
Java Python Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thomas Hauck wrote:Question: Why does the Java compiler allow you to call an abstract method ?

Compiler doesn't bother to look whether the method being called has a definition or not. What all it needs is just a "method declaration". If the compiler finds a method declaration in the "declared type" of the reference it is invoked upon, it will be "OK".

The actual method invoked is decided at runtime, based on the actual object being referred to by that reference. And if the reference is assigned "null", you will get a "NullPointerException", as in this case.

Thomas Hauck wrote:The compiler does not give an error saying: "Cannot call abstract method"

It would be incorrect from the compiler's end to say something like that, because compiler doesn't even know which method is going to be bound at runtime, as already noted above.
 
Roel De Nijs
Sheriff
Posts: 10662
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thomas Hauck wrote:The compiler does not give an error saying: "Cannot call abstract method"

If that's the case, you can get in serious troubles when polymorphism comes into play. A little example to illustrate based on your code. I develop a Calculator API, so other people can create every calculator they want.

I have the following classes:

I compile these classes, create a jar and publish them on a public (maven) repository. Now everyone can create one's preferred calculator: StandardCalculator, ScientificCalculator and even a HumanBrain No problems at all! But if the compiler would give an "Cannot call abstract method" error (like you suggested) I can never compile the CalculatorUtils class! But the compiler also guarantees the add-method in CalculatorUtils will never be invoked by an object of a class which didn't implement the add-method. The compiler makes sure:
  • you can't create an instance of an abstract class
  • if a non-abstract class extends from an abstract class all abstract methods are implemented



  • Thomas Hauck wrote:But in example 2, the class House contains a concrete implementation of the abstract method clean().

    True! But only because you made the example like that. You can easily create a similar code snippet as your 1st example but with interface (instead of class). The reason of course the same as with the abstract class. Let me add the action to the word:


    Hope it helps!
     
    • Post Reply
    • Bookmark Topic Watch Topic
    • New Topic