[Logo]
Forums Register Login
Question from the scjp book
Hi,
I came across a question, and was unable to understand the solution for the question.
Here it is..

Given:


Which statement(s), inserted at line 7, will compile? (Choose all that apply.)
A. Flower getType() { return this; }
B. String getType() { return "this"; }
C. Plant getType() { return this; }
D. Tulip getType() { return new Tulip(); }

The answer is A,C, and D.
and B is incorrect.

However i want to know WHY B is incorrect?
It was from the Kathie Sierra SCJP 5 Study Guide.
Well flower extends from Plant right?
So Flower inherits all of the methods and attributes from Plants.
This means that Flower already has a getType() method and the the return signature of that method is type Plant not String.
So String getType() causes an error.

Sun has a good description of this here:
http://java.sun.com/docs/books/tutorial/java/IandI/override.html

Hunter
means,
A and D are correct because they declare and return sub-types of the Super Class Plant type.
C is correct because it is correctly overriding the method getType();
B is incorrect because it is returning "String" which is not a sub-type of Super Class Plant Type.

I want to know, If I am correct..
 

in.anurag Singh wrote:means,
A and D are correct because they declare and return sub-types of the Super Class Plant type.
C is correct because it is correctly overriding the method getType();
B is incorrect because it is returning "String" which is not a sub-type of Super Class Plant Type.

I want to know, If I am correct..



Does everything you typed here look correct to you? Does it compile when you try it?

Hunter

This thread has been viewed 822 times.

All times above are in ranch (not your local) time.
The current ranch time is
Oct 23, 2018 14:01:06.