aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes difference between overriding & hiding Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "difference between overriding & hiding" Watch "difference between overriding & hiding" New topic
Author

difference between overriding & hiding

janki tangeda
Ranch Hand

Joined: Jun 07, 2005
Posts: 54
Can anybody pls explain the difference between overriding and hiding.What's the purpose of hiding static variables and methods?
Steven Bell
Ranch Hand

Joined: Dec 29, 2004
Posts: 1071
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

Joined: Jun 07, 2005
Posts: 54
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

Joined: Dec 29, 2004
Posts: 1071
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.
Pawanpreet Singh
Ranch Hand

Joined: Jun 12, 2005
Posts: 264

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.
 
Don't get me started about those stupid light bulbs.
 
subject: difference between overriding & hiding
 
Similar Threads
static overriding
overriding vs hiding
Overriding static methods in subclass
static methods-overriding
what is the blaze result?