Tuesday, June 24, 2008

Field Hiding in Java - fields are only hidden not overridden


Field Hiding in Java - fields are only hidden not overridden

Field in Java are only hidden and not actually overridden (that doesn't mean that we'll get a compilet time error while trying this, instead they are not overridden in its true sense). Overriding means the member should be invoked based on the run time type of the object and not based on the declared type. But binding for fields in Java is always static and hence it's based on the declared type of the object reference only. Read more about Static Binding in this article - Dynamic Binding vs Static Binding >>

In case of methods, only those methods are overriden which are inherited and hence static methods are also not overridden but hidden only and they follow Static Binding only. private members (methods or fields both) are neither hidden nor overridden. They also follow Static Binding and they can not be accessed directly from any other class (including sub classes) except the class which have them. Remember, Hidden doesn't mean here we can't access the members from the subclass. So, don't confuse with being not accessible (in case of private members - fields or methods) and being hidden.

static -> non-static, narrowing visibility, diff return type - all allowed

We have much more luxury with Field Hiding as compared to what we have with static method hiding in Java. Even a static field can be hidden with a non-static field with the same name in the sub class. Field Hiding doesn't care about the return type as well. You can even narrow the visibility of a hidden member, which you can't do with methods. For example, a 'public' field in the super class can be hidden with a 'protected' field in the sub class. If we club all these luxuries then we may say that a 'public static String' type field can be hidden with a 'protected int' field in the sub class (notice the changes: static -> non-static, public -> protected, String -> int).

Example: a demo example highlighting Field Hiding in Java

package test;
class FieldHiding {
public String InstanceField = "Instance Field in SuperClass";
public static String StaticField = "Static Field in SuperClass";
public static int StaticIntField = 1;
}
public class FieldHidingDemo extends FieldHiding {
public String InstanceField = "Instance Field in SubClass";
public String StaticField = "Static Field changed to Instance Field in SubClass";
protected String StaticIntField = "public static int in super class -> protected String in subclass";

public static void main(String[] args) {
FieldHiding fieldHiding1 = new FieldHiding();
FieldHiding fieldHiding2 = new FieldHidingDemo();
FieldHidingDemo fhd = new FieldHidingDemo();

System.out.println(fieldHiding1.InstanceField);
System.out.println(fieldHiding1.StaticField);
System.out.println(FieldHiding.StaticField);
System.out.println(fieldHiding1.StaticIntField);
System.out.println(FieldHiding.StaticIntField);

System.out.println(fieldHiding2.InstanceField);
System.out.println(fieldHiding2.StaticField);
System.out.println(fieldHiding2.StaticIntField);

System.out.println(fhd.InstanceField);
System.out.println(fhd.StaticField);
System.out.println(fhd.StaticIntField);
}

}

Output:-
Instance Field in SuperClass
Static Field in SuperClass
Static Field in SuperClass
1
1
Instance Field in SuperClass
Static Field in SuperClass
1
Instance Field in SubClass
Static Field changed to Instance Field in SubClass
public static int in super class -> protected String in subclass

Notice the output in red to see how a field declared static can be hidden with a non-static field in the sub class and how can we narrow the visibility of hidden fields which we can't do for hidden methods in Java.


Liked the article? You may like to Subscribe to this blog for regular updates. You may also like to follow the blog to manage the bookmark easily and to tell the world that you enjoy GeekExplains. You can find the 'Followers' widget in the rightmost sidebar.



Share/Save/Bookmark


4 comments:

Anonymous said...

Thanks for valuable information.

/Subbu

Дима said...

useful for me

Дима said...

useful for me

Shridhar Mali said...

Thanks for this imp post...

BTW

static -> non-static, narrowing visibility, diff return type - all allowed

in this "return type" should be only "type" I guess.