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

doubt question #57 Test 2 Enthuware

 
John Lerry
Ranch Hand
Posts: 145
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a doubt about question # 57 on Enthuware test 2.

I will summarize a bit 'all dicend that given this code:


I believed that this statements:


allowed to invoke ANY method on s, both belonging to XXX that YYY, however, in case you wish to invoke a method present in both classes (for the override) was invoked automatically the version of the subclass since it is more specific.
The correct answer, however, provides that that statement is actually this:


and the explanation was that I can not use that statement as I had made it myself because otherwise it was also necessary to insert the call of method m() within a try-catch block.
I then ask myself, with statements as I had called was it not automatically call the version of YYY?
 
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
John Lerry wrote:I then ask myself, with statements as I had called was it not automatically call the version of YYY?

You are correct! This statementwill indeed (like you said) execute the m method of the YYY class. But if you use this statement in the main method of the TestClass class, the code will not compile. Why? The compiler knows the type of the reference variable s is XXX and in this class the m method is declared with this method declaration:So the compiler sees you'll be invoking a method which declares to throw a checked exception, but in the main method you don't handle, nor declare this checked exception. So the code fails to compile!

If you use the statementthe m method of the YYY class will be executed as well. And the compiler knows the type of the reference variable s is YYY and in this class the m method is declared with this method declaration:So the compiler knows this method will never throw a checked exception, so you don't need to handle nor declare this checked exception in the main method. So the code compiles successfully!

Hope it helps!
Kind regards,
Roel
 
John Lerry
Ranch Hand
Posts: 145
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok, the concept is clear.
I would have another question, just to fix well the concept of polymorphism.

This statements (in case there was not "throws"):


allows you to invoke on the object:
- a method of XXX.
- a method of XXX inherited from class YYY.
- a method of XXX overrided and inherited from class YYY.


NOT allows you to invoke on the object:
- a method of their own class YYY.


is it correct?
 
Paul Anilprem
Enthuware Software Support
Ranch Hand
Posts: 3817
10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
John Lerry wrote:Ok, the concept is clear.
I would have another question, just to fix well the concept of polymorphism.

This statements (in case there was not "throws"):


allows you to invoke on the object:
- a method of XXX.
- a method of XXX inherited from class YYY.
- a method of XXX overrided and inherited from class YYY.


NOT allows you to invoke on the object:
- a method of their own class YYY.


is it correct?

It would be easy to understand if you just remember this one rule - compiler checks the type of a variable to decide whether a method call is valid or not irrespective of what is inherited or overridden. Thus, in your example, only methods available (i.e. methods defined in XXX and method inherited by XXX) in XXX can be called on s because s's type is XXX.

BTW, when you say, "a method of XXX inherited from class YYY", it doesn't make sense because XXX is a superclass of YYY. XXX doesn't inherit anything from YYY.

HTH,
Paul.
 
John Lerry
Ranch Hand
Posts: 145
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Paul Anilprem wrote:
It would be easy to understand if you just remember this one rule - compiler checks the type of a variable to decide whether a method call is valid or not irrespective of what is inherited or overridden. Thus, in your example, only methods available (i.e. methods defined in XXX and method inherited by XXX) in XXX can be called on s because s's type is XXX.


Which version of a method overrided choose depends on the type of the object?
For this reason if there is a version of the method within YYY through statements XXX s = new YYY() is called that version on s. or not?

Paul Anilprem wrote:
BTW, when you say, "a method of XXX inherited from class YYY", it doesn't make sense because XXX is a superclass of YYY. XXX doesn't inherit anything from YYY.

HTH,
Paul.


Yes, mistranslation.
 
Paul Anilprem
Enthuware Software Support
Ranch Hand
Posts: 3817
10
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
John Lerry wrote:
Paul Anilprem wrote:
It would be easy to understand if you just remember this one rule - compiler checks the type of a variable to decide whether a method call is valid or not irrespective of what is inherited or overridden. Thus, in your example, only methods available (i.e. methods defined in XXX and method inherited by XXX) in XXX can be called on s because s's type is XXX.


Which version of a method overrided choose depends on the type of the object?
For this reason if there is a version of the method within YYY through statements XXX s = new YYY() is called that version on s. or not?


Yes, actual execution of a method is, obviously, a run time thing and therefore it is the JVM that decides which version is to be invoked. For non-private instance methods, it decides based on the class of the actual object referred to by that variable...at run time
 
Roel De Nijs
Sheriff
Posts: 10662
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Paul Anilprem wrote:It would be easy to understand if you just remember this one rule - compiler checks the type of a variable to decide whether a method call is valid or not irrespective of what is inherited or overridden.

That's a very, very important rule. If you know this very well, you can easily spot compiler errors on the exam (and you don't have to waste time to determine the output of a few nested loops). Luckily this rule is not hard at all. Which methods you can call/invoke is determined at compile time (using the reference variable type). Which method is actually executed is decided at runtime (based on the type of the actual object).

Let's see if you have fully understood all these concepts. Which of the following lines will compile? And what's the output of each line (if it compiles of course)

Hope it helps!
Kind regards,
Roel
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic