• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Amazing problem related to Method Overriding

 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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!!!
 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Bartender
Posts: 5167
11
Netbeans IDE Opera Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 233
1
Eclipse IDE Opera Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks now I get it. Thanks a lot
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic