Hi this is an extension to the discussion on topic overriding , hiding and dynamic method dispacth..
class Super { int a=2; static int b=4; Super() { method(); printone(); } static void method() { System.out.println("Inside Super "+b+"\n"); } void printone() { System.out.println("Inside Super "+a+"\n"); } }
public class sub extends Super { int a=3; //*************************** void printone() { System.out.println("Inside Sub "+a); } static void method() { System.out.println("Inside Sub "+b+"\n"); }
public static void main(String arg[]) { sub s=new sub(); s.printone(); } } gives output Inside Super b=4 Inside sub a=0 Inside sub a=3 well this is OK with respect to the priority of initialisation.. If the marked line is commented out output will be as follows Inside Super b=4 Inside sub a=2 Inside sub a=2 It is said in earlier discussion that instance members are initialised to default values (even though it has got initialization expression ) and after the constructor return it's invocation, instance varianles will be initialised to those explicit values given in the initialisation expression..
but why it is happening in second case that it is printing a as 2 ans 2.. I am posting this topic on respective discussion also.. some body can expalin this elaborately and clearly.. thanks 'n regds. vbreddy
Jane Griscti
Ranch Hand
Joined: Aug 30, 2000
Posts: 3141
posted
0
Hi vb, You're getting the second <code>Inside Sub 2</code> as the result of <code>s.printone()</code> because you've commented out <code>int a=3</code> in the sub class. Which means the field variable a from the super class is the only one available. At least, I think that's what's going on. ------------------ Jane [This message has been edited by Jane Griscti (edited October 20, 2000).] [This message has been edited by Jane Griscti (edited October 20, 2000).]