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

Method overriding and reference casting

 
Dasa Beg
Greenhorn
Posts: 5
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi guys,

I need your help.

An example is given, which is a little bit confusing to me regarding to the method invoking.
The correct answers are also listed.
Can someone take a look, and describe it if possible, just for a better understanding?

- Which two statements will generate the output "Super String"?






Given:

a) System.out.println(new SubString().toString());
b) System.out.println(new SuperString().toString("String")); ---> CORRECT
c) System.out.println(((Object) new SubString()).toString());
d) System.out.println(((Object) new SuperString()).toString()); ---> CORRECT
e) System.out.println(((Object) new SubString()).toString("String"));
f) System.out.println(((Object) new SuperString()).toString("String"));

The correct answers would be b) and d).

Thanks in advance!

Kind regards
 
Jude Niroshan
Ranch Hand
Posts: 132
5
Eclipse IDE Java Postgres Database
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Dasa,
Welcome to ranch.

Quoted from http://docs.oracle.com/javase/tutorial/java/IandI/override.html

The ability of a subclass to override a method allows a class to inherit from a superclass whose behavior is "close enough" and then to modify behavior as needed. The overriding method has the same name, number and type of parameters, and return type as the method that it overrides. An overriding method can also return a subtype of the type returned by the overridden method. This subtype is called a covariant return type.


Every user defined object is a sub class of 'Object' class. I think you definitely know about that. We do not extends it for each and every single class we write in java. All objects in Java implicitly extend Object, so I'd say it's redundant.Therefore, in your question, it is a valid overriding in the SubString class for toString().

I'm also curious about the explained answer that someone will put to this.
 
Roel De Nijs
Sheriff
Posts: 10662
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It looks confusing at first, but with a few very simple rules everything will be crystal clear.

Rule 1: every non-final inherited method can be overridden by a subclass (if the superclass is of course also non-final). Example: the Object class has a toString method (no parameters) and this method is overridden by the SuperString class. And this toString method is again overridden by the SubString class as well.

Rule 2: you can overload any method by just changing the parameter list. So the SuperString class can define a toString method with 1 String parameter. Note: this method is also overridden by the SubString class (so rule 1 is applied).

Rule 3 (a very important one): the type of the reference variable determines which methods can be invoked, which method is actually executed will be determined by the actual object the reference variable is refering to. Some examples to illustrate.
The statement from option (e) is equivalent to The Object class doesn't define a toString method with a String parameter, so this code doesn't compile. Note: for the same reason the statement in option (f) won't compile either.

The statement frm option (d) is equivalent to The Object class defines a toString method without parameters, so the code compiles. The actual object is of type SuperString, because the SuperString class overrides the toString method, the toString method in the SuperString class is executed, generating the desired output Super String.

Hope it helps!
Kind regards,
Roel
 
Brian Brumpton
Ranch Hand
Posts: 40
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@ Roel De Nijs - Reading your answer demonstrates a very different way than I think. The way you approached the question, by first eliminating the answers that don't compile, quickly narrows down the number of possible choices you have to work through. May I borrow your brain for my next attempt at the exam?
 
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
Brian Brumpton wrote:The way you approached the question, by first eliminating the answers that don't compile, quickly narrows down the number of possible choices you have to work through.

I was not approaching this question to answer it, I was trying to explain and clear the doubts of the original poster (and possibly other ranchers). So I was illustrating my 3rd rule and that's why I started with the compilation error (because that's the 1st part of rule 3 as well).

When I answer questions on the actual exam, I always go through every possible answer and convince myself why an answer is correct/wrong. Because I have eliminated a few answers already, it's indeed easier to pick the correct answer(s) when in doubt. And in the exam software you can visualize an eliminated answer (right click with the mouse on a windows computer), definitely an improvement.

Brian Brumpton wrote:May I borrow your brain for my next attempt at the exam?

Don't think that will be of much help as you would get very distracted of all these little voices

 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic