The Artima Developer Community
Sponsored Link

Java Answers Forum
Need help regarding this code snippet

2 replies on 1 page. Most recent reply: Feb 15, 2005 6:33 PM by Kishori Sharan

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 2 replies on 1 page
Sanjay Perera

Posts: 42
Nickname: angel6
Registered: Apr, 2004

Need help regarding this code snippet Posted: Feb 15, 2005 12:34 AM
Reply to this message Reply
Advertisement
Guys, folowing is a code snippet. When running this, the output will be 5,sub. 5 is shown from Super and sub is shown from Sub. How is this happening????

class Super
{ int index = 5;
public void printVal()
{ System.out.println( "Super" );
}
}
class Sub extends Super
{ int index = 2;
public void printVal()
{ System.out.println( "Sub" );
}
}
public class Runner
{ public static void main( String argv[] )
{ Super sup = new Sub();
System.out.print( sup.index + "," );
sup.printVal();
}
}
//output: 5,sub


Sudhakar

Posts: 2
Nickname: sudhu
Registered: Feb, 2005

Re: Need help regarding this code snippet Posted: Feb 15, 2005 1:37 AM
Reply to this message Reply
class Super
{ int index = 5;
public void printVal()
{ System.out.println( "Super" );
}
}
class Sub extends Super
{ int index = 2;
public void printVal()
{ System.out.println( "Sub" );
}
}
public class Runner
{ public static void main( String argv[] )
{ Super sup = new Sub();
System.out.print( sup.index + "," );
sup.printVal();
}
}
//output: 5,sub

In your code when you assign the super class the instance of subclass the variable sup has handle to subclass object. Variable index exists in both super and subclass. You are overriding the super class variable, which is equivalent to hidding the variable 'index' in the subclass. Hence when sup can't find the hidden variable in 'subclass' and since sup has only knowledge of it's own variable 'index', it'll use it's own copy of 'index' which has been initialised to 5.
Subclasses don't inherit a superclass's member if the subclass declares a member with the same name. In the case of member variables, the member variable in the subclass hides the one in the superclass.
In case of the method it is pretty clear that sup calls sub's method and hence 'sub' is printed.

Kishori Sharan

Posts: 211
Nickname: kishori
Registered: Feb, 2002

Re: Need help regarding this code snippet Posted: Feb 15, 2005 6:33 PM
Reply to this message Reply
Hidden fields and methods are resolved at compile time using the TYPE of the reference variable. So, s.index will point to index member variable of the TYPE of s. See the modifed Runner class's output. We have only one object of Sub class. We can refer to Super's index or Sub's index depedning on the variable sup, sup1 and (Sub)sup TYPE. In first case, TYPE is Super, so you get 5. In second and third, TYPE is sub, so you get 2.

public class Runner {
	public static void main( String argv[] ) {
		Super sup = new Sub();
		Sub sup1 = (Sub) sup;
 
		System.out.print( sup.index + "," );
		System.out.print( sup1.index + "," );
		System.out.print( ((Sub)sup).index + "," );
		sup.printVal();
	}
}

Flat View: This topic has 2 replies on 1 page
Topic: help needed.... Previous Topic   Next Topic Topic: JCreator/Eclipse

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use