File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Please explain me the flow of this simple prog....... Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Please explain me the flow of this simple prog......." Watch "Please explain me the flow of this simple prog......." New topic
Author

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

Harpal Singh
Ranch Hand

Joined: Oct 10, 2000
Posts: 229
--------------------------------------------------------------------------------

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).]
Manjunath BS
Greenhorn

Joined: Oct 21, 2000
Posts: 3

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
Manish Singhal
Ranch Hand

Joined: Sep 21, 2000
Posts: 104

Thanks for the great explanation.
I was having this doubt since long.( Inside sub 0 )
regards,
Manish
Harpal Singh
Ranch Hand

Joined: Oct 10, 2000
Posts: 229
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
Mapraputa Is
Leverager of our synergies
Sheriff

Joined: Aug 26, 2000
Posts: 10065
I just wonder what kind of school gives such advanced questions? I am impressed Manjunath, do you plan to certify all your students?


Uncontrolled vocabularies
"I try my best to make *all* my posts nice, even when I feel upset" -- Philippe Maquet
Manjunath BS
Greenhorn

Joined: Oct 21, 2000
Posts: 3

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
suneeta prattipati
Greenhorn

Joined: Oct 16, 2000
Posts: 8
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?
anrup kris
Greenhorn

Joined: Oct 22, 2000
Posts: 17
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
Sheriff

Joined: Aug 26, 2000
Posts: 10065
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).]
Prasad Ballari
Ranch Hand

Joined: Sep 23, 2000
Posts: 149
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

Joined: Oct 10, 2000
Posts: 229
Prasad,
You are correct ,that is the reason you get the first result as 4........
Harpal
Ira Jain
Ranch Hand

Joined: Sep 06, 2000
Posts: 70
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
Kathy Rogers
Ranch Hand

Joined: Aug 04, 2000
Posts: 103
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

Joined: Sep 06, 2000
Posts: 70
Thanks Kathy for your nice explanation. It really cleared the concepts for me.
------------------
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Please explain me the flow of this simple prog.......
 
Similar Threads
Again overriding
doubt in inheritance...
Dynamic Binding of methods and data
Polymorphism and Instance Initialisation
Instance Initialisation/Strange behavour