• Post Reply Bookmark Topic Watch Topic
  • New Topic

Help figuring out code  RSS feed

 
Davey Lopez
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello I was wondering If someone could tell me what the following lines of code do "System.out.println(s.x);" and "System.out.println(t.x);".
I am much more used to something like System.out.println(S.papa3());
I haven't encountered this in my book yet, so if someone could explain to me what they do. Or better yet, provided me with a link to an explanation I would greatly appreciate it.


I added a bunch of checkpoints to tell me where the code is going. I guess my question is really why it initially goes to Papa3() that requires no parameters?

When I did a test run I got this
Method 1
here dummy
0
Method 1
This is x 2
0

The "here dummy" and "this is x 2" That I understand comes from the =new Son3() and =new Son3(2)

-- thanks

 
Joe Harry
Ranch Hand
Posts: 10128
3
Eclipse IDE Mac PPC Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The concept is called constructor chaining. If you take a look into the compiled code, you will see that there is always an implicit call to the super class constructor from the child class that that is why you see method 1 printed when you instantiate Son.
 
Davey Lopez
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Question I thought you had to type super() to actually use a method from the base class
 
Jitender Raghuvanshi
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You have two constructors in your superclass.
Your object s is referencing the superclass as well.
So, when you are creating an object of your base class its first invoking the Constructor of the superclass.

The Superclass constructor initializes the x to 0 and prints the statement "Method 1".
Then control goes back to base class printing the statement "here dummy".
Afterwards, control comes back to main and s.x accesses the variable x of superclass and prints it.

So basically s.x and t.x are accessing the fields of your superclass.
Please do tell me if i am wrong... I am a beginner too. :-(
 
Jitender Raghuvanshi
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Davey Lopez wrote:Question I thought you had to type super() to actually use a method from the base class

Objects of base class can access the superclass as well. super() is useful when you are transferring data to the superclass constructor from its baseclass (or simply invoking the superclass constructor in the baseclass) .
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Davey Lopez wrote:Hello I was wondering If someone could tell me what the following lines of code do "System.out.println(s.x);" and "System.out.println(t.x);".
I am much more used to something like System.out.println(S.papa3());
I haven't encountered this in my book yet, so if someone could explain to me what they do.

The dot ('.') is actually an operator in Java, and it means "defined in" (or words to that effect), so when you write:
S.papa3()
you're saying "the method papa3() defined in S" (which can be either a class or an object; if the latter, then papa3() must be static).

And you can use it for fields as well, so:
t.x
simply says "the field x defined in t".

Just FYI, fields and methods are collectively known as "members".

HIH

Winston
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!