• 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

overriding , Hiding

 
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Here is my understanding ..please correct me if Iam wrong.
1. Overriding is for instance methods ( same signature ). By signature I think it is ONLY method name and parameters list. Not return type.
2. Hiding is for Class methods.
class a
{
static void pnt()
{//code
}
}
class a extends b
{
static void pnt() //Here pnt() method is hiding
{//code
}
}
---
class a
{
void pnt() { }
}
class b extends a
{
void pnt() {} // Here pnt() method is overriding.
}
1. You cannot override instance/static method.
2. You cannot override static/instance method.
3. No return type and throws conflict clash
4. public method can be overrided by public access
5. protected method can be overrided by public or protected.
6. Default access can be overrided by public access
Overriding of fields is called Hiding.
You can hide static / non static field .
You can hide non static / static field
You can hide return -type non compatible.
Is that complete things about Overriding , hiding . Do I miss any important points.
Thanks
 
Trailboss
Posts: 23778
IntelliJ IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think perspective makes all the difference.
Overriding and hiding are pretty much the same thing, with the exception being perspective.

If we have the code
Cow c = new Calf();
and we call c.speak(); the Calf method overrides the Cow method.
If we say
Calf c = new Calf();
and we call c.speak(); the Calf method hids the Cow method.
Does this help?
 
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Paul- I think you're using a different definition of "hiding" than the one used in the JLS. "Hiding" can apply to variables and to static methods, but not to non-static methods - those can only be overridden (or overloaded, but that's something else). In your example speak() in Calf overrides speak() in Cow, period. It doesn't matter what the reference type of the variable c is, because the speak() method will be looked up at run time using the type of the object which c references - a Calf in this case.
For an example of hiding, you could make both speak() methods static. Then of course there it would make a difference what type c was declared as.
A general distinction between overriding and hiding: with overriding, if you have an instance of the overriding class, there is no way you can use it to access the overridden method (except from within the code for the overriding class itself, using super.methodName()). It doesn't matter if you try to cast the instance to its superclass - the overriding method will still be called:
<code><pre> Calf c = new Calf();
((Cow) c).speak();</pre></code>
The cast to Cow is ineffective in this case - speak() in Calf is still the one that will be called.
However, in the case of hiding, it is always possible to access a hidden method or variable by casting or otherwise using the appropriate class in the first place. In the above example, if speak() were declared static in both classes, then ((Cow) c).speak() would successfully invoke the hidden speak() method from class Cow. Of course, it would have been more direct to just call Cow.speak() in this case anyway - but the point is, the hidden method is accessible. Similarly, it is possible to access hidden variables in much the same way.
Looking back at psethura's list, I think it's pretty much accurate. One section I'd edit somewhat:
1. You cannot override or hide an instance method with a static method.
2. You cannot override or hide a static method with an instance method.
3. An overriding or hiding method must have the same return type.
3.5. An overriding or hiding method can't throw any checked exceptions not previously declared.
4. public method can be overridden or hidden only by public access
5. protected method can be overridden or hidden by public or protected.
6. Default access can be overridden or hidden by public, protected, or default access
 
You don't know me, but I've been looking all over the world for. Thanks to the help from this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic