Two Laptop Bag*
The moose likes Beginning Java and the fly likes Amazing problem related to Method Overriding Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Amazing problem related to Method Overriding" Watch "Amazing problem related to Method Overriding" New topic
Author

Amazing problem related to Method Overriding

Chamara Madhushan
Greenhorn

Joined: Nov 17, 2012
Posts: 4
this code works properly because getType methods in two classes doesn't override & prints "getType Vehicle".




Following code does not compile because getType methods in two classes try to override but they don't.
Why this theory didn't apply in Example 01??



please tell me the theory behind this??

regards!!!
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18545
    
  40

Chamara Madhushan wrote:this code works properly because getType methods in two classes doesn't override & prints "getType Vehicle".




The getType() method of the Car class does indeed override the getType() method of the Vehicle class -- why do you think that it doesn't???

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18545
    
  40

Chamara Madhushan wrote:
Following code does not compile because getType methods in two classes try to override but they don't.
Why this theory didn't apply in Example 01??



please tell me the theory behind this??


Return types matter when overridding a method. The Vehicle getType() method returns a Vehicle, so the overriding method must also return a Vehicle. A Car IS-A Vehicle. A String is NOT IS-A Vehicle.

Henry
Darryl Burke
Bartender

Joined: May 03, 2008
Posts: 4523
    
    5

In your second code sample, Vehicle#getType() returns a Vehicle. Since Car extends Vehicle -- every Car is-a Vehicle -- you can't override the method to return String; the returned type must be a Vehicle (which includes any subclass of Vehicle, which as said, is-a Vehicle). And a String is definitely not a Vehicle.

See the Requirements in Overriding and Hiding in the JLS. Actually, it would be worth your while to go through that entire chapter.


luck, db
There are no new questions, but there may be new answers.
Chamara Madhushan
Greenhorn

Joined: Nov 17, 2012
Posts: 4
Henry Wong wrote:
The getType() method of the Car class does indeed override the getType() method of the Vehicle class -- why do you think that it doesn't???
Henry


If it Override as you said can you explain why an output as "getType Vehicle" comes instead of "getType CAR" in example 01???
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18545
    
  40

Chamara Madhushan wrote:
Henry Wong wrote:
The getType() method of the Car class does indeed override the getType() method of the Vehicle class -- why do you think that it doesn't???
Henry


If it Override as you said can you explain why an output as "getType Vehicle" comes instead of "getType CAR" in example 01???


It prints "getType Vehicle" because you instantiated a Vehicle object. You actually need a Car for it to behave like a car.

To extend your analogy to extremes, you can also say that Vehicle extends the Object class, so why if I say "new Object()", this object doesn't behave like a car?

Henry
Rajdeep Biswas
Ranch Hand

Joined: Mar 26, 2012
Posts: 186

Chamara Madhushan wrote:
Henry Wong wrote:
The getType() method of the Car class does indeed override the getType() method of the Vehicle class -- why do you think that it doesn't???
Henry


If it Override as you said can you explain why an output as "getType Vehicle" comes instead of "getType CAR" in example 01???

What do you think why "getType Car" should be the output? The object is of "Vehicle" type, and from Vehicle class' constructor, you are calling getType() method of same class.


Edit: I recommend you visit this: Polymorphism Blues


The biggest gamble will be to ask a question whose answer you know in that it will challenge your theory | www.TechAspire.blogspot.in
Chamara Madhushan
Greenhorn

Joined: Nov 17, 2012
Posts: 4
Thanks now I get it. Thanks a lot
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Amazing problem related to Method Overriding
 
Similar Threads
How is this Possible?
Override
Casting
superclass and subclass
Static methods