This week's book giveaway is in the OO, Patterns, UML and Refactoring forum. We're giving away four copies of Refactoring for Software Design Smells: Managing Technical Debt and have Girish Suryanarayana, Ganesh Samarthyam & Tushar Sharma on-line! See this thread for details.
Java programs are starts it execution from the main method.
You override the method print of SuperClass in SubClass and also you call the method print in main method before the round method perform action.
In the print method you have print the value of i and
i is the global variable in your code
.So it automatically initialise with 0.So only it prints 0 and then 4.
even if we do not use global variables in the method and just try to print out something something it still gives double output
what can be the fact?
here is the code
experts please answer why this is occuring
I am such a fool!
I think I got the solution
I will tell what I am thinking but please tell me whether it is right or not !
here is the code
and the output
Super class constructor running now
counter = 1
Super class constructor completes here
counter = 2
According to me following things happen
1. Starting of main method
2. on the object creation line, the subclass constructor runs and the default values are assigned to the variables of the subclass and also the superclass constructor runs due to the implicit call to the superclass no-arg constructor
3.in the super class constructor we are calling the method print hence the method gets executed and the default values are printed
4. Now as the superclass constructor has completed, the control returns back to the subclass and explicit values are assigned to the variables in the subclass
5. Now again we are calling the method using the object hence the explicit values are printed