• 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

The method output

 
Ranch Hand
Posts: 446
1
Eclipse IDE MySQL Database Tomcat Server
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I don't know why the "0" gets printed here
please can anyone explain why is it?

The output is
0
4
 
Ranch Hand
Posts: 138
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi prasad,

Java programs are starts it execution from the main method.

and
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.
Cheers Munees
 
Prasad Kharkar
Ranch Hand
Posts: 446
1
Eclipse IDE MySQL Database Tomcat Server
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
so does that mean that the print method gets called twice?
 
Prasad Kharkar
Ranch Hand
Posts: 446
1
Eclipse IDE MySQL Database Tomcat Server
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
here I have modified some code and found that the method does not gets called twice
I used a counter there
and found something interesting
here is code


the output is

counter = 1
0
0
counter = 1
4
0



isn't it interesting?
let us discuss
 
Prasad Kharkar
Ranch Hand
Posts: 446
1
Eclipse IDE MySQL Database Tomcat Server
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
and also
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

and output is

C:\Users\Prasad\Desktop>java SubClass
something something
something something

 
Prasad Kharkar
Ranch Hand
Posts: 446
1
Eclipse IDE MySQL Database Tomcat Server
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hahahaha
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


C:\Users\Prasad\Desktop>java SubClass
Super class constructor running now
counter = 1
something something
0
0
Super class constructor completes here
counter = 2
something something
4
0


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

Is it right now?

 
Muneeswaran Balasubramanian
Ranch Hand
Posts: 138
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ya,You are right prasad.
Cheers Munees
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic