• 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

Overload or Override..How??

 
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If im correct, we can't overload based only on return type. But the example from K&B shows that it is possible and it is said to be an OVERRIDE !
How is it possible an overriden method having different return types ??
referring to the
getRental method in both classes

class Rental
{
private List rentalPool;
private int maxNum;
public Rental( int maxNum, List rentalPool )
{
this.maxNum = maxNum;
this.rentalPool = rentalPool;
}
public Object getRental()
{
return rentalPool.get(0);
}
}
class CarRental extends Rental
{
public CarRental( int maxNum, List<Car> rentalPool )
{
super( maxNum, rentalPool );
}
public Car getRental() // how can it be an override ??
{
return (Car)super.getRental();
}
}
 
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Ajay,

The method getRental() is overriding only as it satisfies the contract.

1) getRental() is inherited
2) The over riding method has same arguements as that of Overriden method (In our case no arguements at all)
3) return type satisfies IS-A relation. Car extends Object.

Because of the above three reasons, getRental() is over riden not Overloading.

For Overloading, minimum requirement is the arguement list. Arguements should not be same .
 
Ajay Divakaran
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Oh!! I forgot.
its called covariant return type
thank You!
 
Are we home yet? Wait, did we forget the tiny ad?
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic