• 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

difference between overriding & hiding

 
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Can anybody pls explain the difference between overriding and hiding.What's the purpose of hiding static variables and methods?
 
Ranch Hand
Posts: 1071
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The reason static variables and methods are hidden is that they cannot be overridden. Static members belong to the Class and not the instance. Static members are also resolved at compile time not runtime.

example:


What does it print and why?

It prints Super because the method is static and resolved at compile time based on the type of reference. Just another reason you should never call a static member from an instance always from the class type.
 
janki tangeda
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks.Now i understand the difference.I have another question based on your explanation.Can we figure out which error's are compile time and which are run time?Why are objects resolved only during the runtime?The RHS of an object instantiation makes it clear which object the refernce at LHS refers to.
Then why can't the compiler find out which object the reference variable refers to?
 
Steven Bell
Ranch Hand
Posts: 1071
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Because we work within multithreaded environments, can employ bytecode manipulation, and I'm sure a host of other reasons, the compiler can only 'know' the declared type of an object. Java allows alot of cool tricks.
 
Ranch Hand
Posts: 264
Eclipse IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
static methods and variabels(static and non static) can not be overridden, they can only be hidden. But rules for hiding both static methods and static variables are different.
For hiding a static method, all rules of overriding a method are applied.
a.) name should be same
b.) parameter sequence and types should be same
c.) same static modifier should be there
d.) and access modifier can be widen not narrower (means check that init() in A class with default access modifier and in B it could be protected or public but can not be private)

class A
{
static void init(int i)
{
}
}
class B extends A
{
public static void init(int i)
{
}
}


>>>>>>>>>>>>>>Now in case of member variables(local)/class variables (static)...Keep on thing in mind, we can;t override any type of variable, variables can only be hidden.
Rule for variable to be hidden
variabel name should be same (no matters what is type of variable)

class A
{
int local=10;
static int i_am_static=2;
}
class B extends A
{
private String local="I am hidning local of A";
static String i_am_static="I am hiding static variable of A";

public static void main(String args[])
{
B b = new B();
System.out.println("b.local :"+b.local);
System.out.println("b.i_am_static :"+b.i_am_static);

A a =b;
System.out.println("a.local :"+a.local);
System.out.println("a.i_am_static :"+a.i_am_static);

}
}
/***

output is

b.local :I am hidning local of A
b.i_am_static :I am hiding static variable of A
a.local :10
a.i_am_static :2
*/

I think this will help you upto some extent.
reply
    Bookmark Topic Watch Topic
  • New Topic