• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Again overriding

 
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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

 
Ranch Hand
Posts: 3141
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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).]
 
reply
    Bookmark Topic Watch Topic
  • New Topic