aspose file tools
The moose likes Beginning Java and the fly likes call to super() and this() - k&b p139 Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Reply Bookmark "call to super() and this() - k&b p139" Watch "call to super() and this() - k&b p139" New topic
Author

call to super() and this() - k&b p139

Kiran Gavate
Greenhorn

Joined: Jan 18, 2007
Posts: 25
Hi ranchers,

In k&b p139 I have been reading that a constructor can never have a call to super() and a call to this() because either needs to be the first statement in a constructor.

Also it has been stated that -

'That also means the compiler will not put a call to super() in any constructor that has a call to this()'

But I am not able to verify this. e.g. if I run the following code -



I get the following output -

In Super constructor...
In SuperTest no-args constructor...
In Super constructor...
In SuperTest no-args constructor...
In SuperTest(String s) constructor...

It looks like the compiler Is putting a call to super() even when the constructor SuperTest(String s) has a call to this().

Could somebody please explain if I am doing something wrong here?


Kiran
Jesper de Jong
Java Cowboy
Bartender

Joined: Aug 16, 2005
Posts: 12907
    
    3

There's nothing wrong, this is perfectly normal behaviour.

1. SuperTest(String s) calls this(), which means it calls the constructor SuperTest()

2. SuperTest() implicitly calls super() (which translates to SuperSup())

You can think of it like this: An object of type SuperTest consists of a SuperSup object plus something extra (the extra stuff in SuperTest, which extends SuperSup). So if you create a SuperTest object, the SuperSup object that is the base part of the SuperTest object has to created too. So at some point, a constructor of SuperSup has to be called.


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
Kiran Gavate
Greenhorn

Joined: Jan 18, 2007
Posts: 25
Thanks Jesper,

It means that the super constructor will ALWAYS be called no matter what.
whether inserted by the compiler explicitly or through a call to this().
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 16684
    
  19

Originally posted by Kiran Gavate:

It means that the super constructor will ALWAYS be called no matter what.
whether inserted by the compiler explicitly or through a call to this().


An instance of the SuperTest class IS-A SuperSub object, so at some point, the part of the object that is being inherited has to be contructed too.

Henry
[ January 26, 2007: Message edited by: Henry Wong ]

Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
 
I agree. Here's the link: http://zeroturnaround.com/jrebel
 
subject: call to super() and this() - k&b p139
 
Similar Threads
Constructor Problem
Redefining a static method in subclass
constructor question
Question on Constructor
Overloading or Overriding ?