Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

difference between overriding & hiding

 
janki tangeda
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
  • 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?
 
Steven Bell
Ranch Hand
Posts: 1071
  • Mark post as helpful
  • send pies
  • 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
  • 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
  • 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.
 
Pawanpreet Singh
Ranch Hand
Posts: 264
Eclipse IDE Java Spring
  • Mark post as helpful
  • send pies
  • 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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic