This week's book giveaway is in the Testing forum.
We're giving away four copies of Data Structures the Fun Way: An Amusing Adventure with Coffee-Filled Examples and have Jeremy Kubica on-line!
See this thread for details.
  • 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
  • Devaka Cooray
  • Ron McLeod
  • Jeanne Boyarsky
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Martijn Verburg
  • Frits Walraven
  • Himai Minh

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!
 
permaculture is giving a gift to your future self. After reading this tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic