• 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

What is static method hiding?

 
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Static method cannot be overridden but can be hidden.

Can you explain what is meant by static method getting hidden. An example will be great.
Thanks.
 
Ranch Hand
Posts: 2023
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Static Methods CAN NOT be Overridden .
 
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
class A {
static void hi(){} // version 1
final static void hi(){} // version 2
}

class B extends A {
static void hi() {} // new version
}

So the new version in B will hide the version in A. That is:
1. If you don't define the "new version" in B, and you call B.hi(); you get version 1.
2. If you define new version in B, you call B.hi(); you get the new version.
3. If instead of version 1 you sub in version 2 in A, then compiler will not allow you to define a "new version" in B. try it.
 
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Static methods are not overridden, that is the method called doesnot depend on the acutal type of object rather it depends on the type of class.
In the above ex:


A a = new A(); //case 1
a.hi(); // calls version 1
B b = new B(); //case 2
b.hi(); //calls new version
A a = new B(); //case 3
a.hi(); //calls method in A that is version 1.

Case 3: calling of a method is not dependant on the actual object (B) rather it is dependant upon the type of class (A) here which is not the concept of polymorphism/overriding.

Case 2: calls the method defined in B but not in A since method in B is hiding method in A.
 
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This is also in our SCJP FAQ.
reply
    Bookmark Topic Watch Topic
  • New Topic