• 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
  • Tim Cooke
  • paul wheaton
  • Liutauras Vilda
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Saloon Keepers:
  • Scott Selikoff
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
  • Frits Walraven
Bartenders:
  • Stephan van Hulst
  • Carey Brown

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!
 
Politics n. Poly "many" + ticks "blood sucking insects". Tiny ad:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic