• 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

Please explain me the flow of this simple prog.......

 
Ranch Hand
Posts: 229
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
--------------------------------------------------------------------------------

This code was given by Mr vbreddy,PLEASE can some body explain me the flow of this code.....before I go MAD...
why the out put is:
Inside Super b=4
Inside sub a=0
Inside sub a=3
Harpal................................
(I added some UBB codes here -Mapraputa)
[This message has been edited by Mapraputa Is (edited October 21, 2000).]
 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Hi Harpal,
Surprising that you did not ask me this question in the class!!!
My explanation goes like this.Somebody correct me if i am wrong.
Once you have created the object of the sub class 'sub', the no arg cons of the sub class is called .Because you have not provided any no args cons in the sub class the default cons is called
which in turn places a "super()" call to the super class.
Now in the no arg cons of the super class , there is a call to
a STATIC method method().Now you know that static methods are not
overriden and hence the method() which belongs to the super class
is called which prints the value "Inside Super b=4".
The point you have to keep in mind here is that the "this" reference in the super class Cons is actually a sub class type
as , a sub class reference "s" was responsible for calling the super class cons.

Next there is a call to a non static method printone().
Because this method is overriden in the sub class and because the "this" reference is of sub class type it actually calls the
printone methos which belongs to the sub class.
But the no arg cons in the super class has not executed completely as inside it there is a call to a method in the subclass , which in turn prints the value of a instance variable.
Because the cons of the super class is not done executing yet,
no object of the sub class has been yet created because of the fact the construction is done from top to bottom in the classes
heirarchy.
so the process of initialising the instance variables to the
equated values does not take place,but at this time the instance
variables are already initialised to their default values.
Here because it is an int type it is initialised to zero.
That's why "Inside sub a=0" is printed.
Now once the no arg cons of the super class has completed executing ,the control transfers back to the default cons of the
sub class which basically does nothing other than returning the
address of the newly created object of the sub class after initialising the instance variables to the value which is assigned to it at the place where it is declared.
That is why when you call the function printone() it is able to print "Inside sub a=3"
So far for my understanding.Any Corrections will be gratefully
accepted.
regards,
Karthick
 
Ranch Hand
Posts: 104
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Thanks for the great explanation.
I was having this doubt since long.( Inside sub 0 )
regards,
Manish
 
Harpal Singh
Ranch Hand
Posts: 229
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Dear Manju,
Thanks a lot...I would have asked you this doubt in class on monday....anyways it is crystal clear now....Manju did I tell you that you are really good person.........see you on monday
Thanks again,
Harpal
 
Leverager of our synergies
Posts: 10065
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I just wonder what kind of school gives such advanced questions? I am impressed Manjunath, do you plan to certify all your students?
 
Manjunath BS
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Hi all,
Thanks for all the complements guys.I owe it to javaranch.
Harpal, I am happy that you have got the concept.
Tell you one thing though, it is really difficult to put the
concepts in words than saying it.
And Mapraputa , YES i really want to get everybody to get
certified . After all guess from which school i learnt java?
JAVARANCH!!!
regards,
Manjunath
 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Manjunath,
Correct me if I am wrong. Static methods cannot be overridden to be non-static.But in this code, method is static even in the sub class. So it can be overridden. Right?
 
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Suneeta
static methods can't be overridden.They can only be hidden.
And you are correct.static methods cant be overrideen by non static methods in the subclass

 
Mapraputa Is
Leverager of our synergies
Posts: 10065
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
To elaborate a little on what Anrup said:
1) if you "override" static method by non-static, you get compile time error.
2) if you "override" static method by another static method, it compiles clean. But now your method is "hidden", not overriden. The difference is: if you try to call your method with an instance of sub-class, defined as <code>
Superclass obj = new Subclass();
</code>
you will not get sub-class version of the method (as you may expect), superclass' method will be called.

[This message has been edited by Mapraputa Is (edited October 22, 2000).]
 
Ranch Hand
Posts: 149
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Dear Manjunath BS.

Correct me if its wrong. As i know the Static variables are intiliazed first.Hence when the object is created.The JVM intializes the static variables of the Super class and then sub class.hence the first result is

Inside Super b=4

Pls guid me if i am wrong.
Prasad

------------------
 
Harpal Singh
Ranch Hand
Posts: 229
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Prasad,
You are correct ,that is the reason you get the first result as 4........
Harpal
 
Ranch Hand
Posts: 70
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have got a bit confused about the flow of this program.Acc.to my understanding ,all the static variables are initialised and all the static methods are executed before any other execution takes place. As per the given explanation that does not seem to be true so could please clear up my concept??
Thanks in advance
Ira
 
Ranch Hand
Posts: 104
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ira,
You're right that static variables are initialised at class loading time, before the rest of the code executes. At this time, the static initializers are executed - these are little bits of code surrounded by static{}
e.g
public class StaticExample
{
static String classMember = "Static";
static{
System.out.println("StaticExample static initializer");
}
public static void classMethod(){
//Class Method
}
}
At class load time, the System.out.println will be executed and the static variable will be initialised. But the static classMethod won't run until you call it from somewhere. But you can call it using the class name
eg. StaticExample.classMethod()
without creating an instance of StaticExample - ie you don't need to do this:-
StaticExample example = new StaticExample();
example.classMethod();
The real point about static variables, methods, initializers is that you don't need to create an instance of the class to get to them. You've probably used lots of them without realising - Math.random()- a static method, BorderLayout.NORTH - a static variable, System.exit()- another static method - because they're static methods / variables, you can use them by referring to them using the class name rather than an instance of the class. And the most common static method is public static void main (String args[]) - that's your entry point into the program so no instances of classes exit when you call it - because it's static, you can call it without creating an instance of whichever class main is contained in.
Roberts et all, pages 84 onwards gives a good explanation of this and the limitations on static variables / methods.
Hope this helps,
Kathy
 
Ira Jain
Ranch Hand
Posts: 70
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Kathy for your nice explanation. It really cleared the concepts for me.
------------------
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic