Static and Inheritance

Static members(variables as well as methods) are class level entities and associated with the class. There are some unique behaviour when we override(it is not correct work, hiding is the correct word)  these members. Public static members are inherited in all the subclass chain until some subclass hide (not override) these members. Lets see this by example. First without overriding:-

public class Super{
public static int i;
public static void sayHello(){
System.out.println("Super Hello");
}
}

public class Sub extends Super{
}

class test
{
public static void main(String args[])
{
Super sup = new Super();
sup.sayHello();
System.out.println(sup.i);

Sub sub = new Sub();
sub.sayHello();
System.out.println(sub.i);

sub.i = 20;
System.out.println("After modifying i");
System.out.println(sup.i);
System.out.println(sub.i);
}
}

Output:-
C:\Users\ViKu>java test
Super Hello
0
Super Hello
0
After modifying i
20
20

Which indicates that even tough we have not written any code in Sub.java these values are taken from  Super class.

One interesting point is that same instance of variables are used the super as well as subclass. So modifying one will change the other’s.

Now lets overwrite these methods in Sub and see the behaviours:-

New Sub.java is:-

public class Sub extends Super{
public static int i = 30;
public static void sayHello(){
System.out.println("Sub hello");
}
}

Output:
C:\Users\ViKu>java test
Super Hello
0
Sub hello
30
After modifying i
0
20

Here we see that the behaviour in Sub is hidden by Sub. Now class Sub has its own behaviour and member variable which is not shared. So point is that Subclass keep inheriting the static members until some subclass hide these.

Now coming to the last point, you might have notices that I am using “Hide” instead of overriding. There is some difference between hiding and overriding.  Lets see this by example:-

Modify the test.java as:-

class test
{
public static void main(String args[])
{
Super sup = new Super();
sup.sayHello();
System.out.println(sup.i);

Sub sub = new Sub();
sub.sayHello();
System.out.println(sub.i);

Super super2 = new Sub();
super2.sayHello();
System.out.println(super2.i);
}
}

Output:-
C:\Users\ViKu>java test
Super Hello
0
Sub hello
30
Super Hello
0

If you check last 2 lines, in normal (not a static) method polymorphism would have applied(i.e. overriding) and we would have got the behaviour of Sub. But in case of hiding(ie with static methods) we get behaviour of the variable, not the object it is pointing to. So this is the basic difference between “overriding” and “hiding”, in overriding polymorphism principle applies and we get behaviour of the actual objet which variable points to but in case of hiding we get the behaviour of the variable.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s