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

overidden method question

 
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
is the declaration of method duplicate in class Circle valid ?

public class Shape
{
public Shape duplicate()
{
return (Shape)this;
}

}

public class Circle extends Shape
{
public Circle duplicate ()
{
return (Circle)this;
}

}
 
Ranch Hand
Posts: 331
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

You can just compile it and see...
 
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
One hint that helped me a lot was the following:

Reference type determines the overloads.

Object type determines overrides.

So in your example you're defining "duplicate" in Shape and in its subclass "Circle". Since this is an override (it can't be an overload because you have not changed the signature) the compiler will know which class to call which means there is no ambiguity here so it will compile correctly.

Quiz time... where will the call for the following examples come from (Shape or Circle):



If you get this example you're golden.

HTH
 
Manoj Garg
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Shape s = new Shape();
s.duplicate(); - duplicate of shape class will be called

Circle c = new Circle();
c.duplicate(); - here duplicate of circle

Shape sc = new Circle();
sc.duplicate(); - here complier will check if duplicate method exists in class shape.. at run time call will be from circle class as it is override.

Thanks.
 
Noam Wolf
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
well done. So does this mean you understand now?
 
Without subsidies, chem-ag food costs four times more than organic. Or this tiny ad:
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic