• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Amazing problem related to Method Overriding

 
Chamara Madhushan
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Marshal
Pie
Posts: 20889
75
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Henry Wong
author
Marshal
Pie
Posts: 20889
75
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 5125
11
Java Netbeans IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Chamara Madhushan
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Marshal
Pie
Posts: 20889
75
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 207
1
Eclipse IDE Java Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Chamara Madhushan
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks now I get it. Thanks a lot
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic