• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Bear Bibeault
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Jj Roberts
  • Carey Brown
Bartenders:
  • salvin francis
  • Frits Walraven
  • Piet Souris

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?
 
Good night. Drive safely. Here's a tiny ad for the road:
the value of filler advertising in 2020
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic