• 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
  • Tim Cooke
  • Ron McLeod
  • paul wheaton
  • Jeanne Boyarsky
Sheriffs:
  • Paul Clapham
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
  • Himai Minh
Bartenders:

super()

 
Ranch Hand
Posts: 371
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi, I have a question about super(). Does it gets called implicitly always? For instance,
class ASubClass extends SuperClass{
ASubClass(String args){
//blah blah...
}
}
Was super() called in ASubClass(String)?

 
Ranch Hand
Posts: 117
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Cameron Park:
Hi, I have a question about super(). Does it gets called implicitly always? For instance,
class ASubClass extends SuperClass{
ASubClass(String args){
//blah blah...
}
}
Was super() called in ASubClass(String)?


Hi Cameron,
The answer is not always. The only time super() is implicitly called is when subclass has a default constructor (i.e. the one without argument). The default constructor is either provided by the programmer or 'implicitly' inserted by the compiler. Note that the compiler only does that if you do not provide any overloading constructor in the class.
Please note the last statement of the above paragraph because it could cause problem for you. Here is how:
class A() {
A(int a) {
}
}
class B extends A {
}
The above subclass is equivalent to:
class B extends A {
B() { super(); } // this is done by the compiler
}
Since A has an overloading constructor, there is no default constructor. However B does not have any constructor, therefore java compiler will provide one. That will implicitly call super(). But as you can see, there is no default constructor in A -> hence you will encounter error.
Hope that helps,
Lam

[This message has been edited by Lam Thai (edited April 20, 2001).]
 
Ranch Hand
Posts: 400
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
ALWAYS...ALWAYS...ALWAYS...!!!
Sorry Lam...I disagree...!!!,
your state :
"......The answer is not always. The only time super() is implicitly called is when subclass has a default constructor (i.e. the one without argument)...."
In my opinion..., even if subclass has an overloading constructor, this subclass overloading constructor will also IMPLICITLY call Base class constructor using super(), so the answer should be "ALWAYS".
consider this :
class A{
A() { System.out.println("A called"); }
}
public class B extends A{
B(int x) {
//this will IMPLICITLY call super()
System.out.println("B called");
}
public static void main(String[] args) {
B b = new B(123);
}
}

hope that helps
stevie
 
Lam Thai
Ranch Hand
Posts: 117
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Stevie Kaligis:
ALWAYS...ALWAYS...ALWAYS...!!!
Sorry Lam...I disagree...!!!,
your state :
"......The answer is not always. The only time super() is implicitly called is when subclass has a default constructor (i.e. the one without argument)...."
In my opinion..., even if subclass has an overloading constructor, this subclass overloading constructor will also IMPLICITLY call Base class constructor using super(), so the answer should be "ALWAYS".
consider this :
class A{
A() { System.out.println("A called"); }
}
public class B extends A{
B(int x) {
//this will IMPLICITLY call super()
System.out.println("B called");
}
public static void main(String[] args) {
B b = new B(123);
}
}

hope that helps
stevie



Hi Stevie,
I stand corrected. But here is the case where default super constructor, super(), is not called:
class A{
A() { System.out.println("A called"); }
A(int x) { System.out.println("A(int x) called"); }
}
public class B extends A{
B(int x) {
super(x);
//this will IMPLICITLY call super()
System.out.println("B called");
}
public static void main(String[] args) {
B b = new B(123);
}
}
This will not implicitly call super(). So I guess we must state that default super() will not be inserted by the compiler if the subclass constructor explicitly invokes immediately its other overloaded parent's super, if one exists.
Regards,
Lam
 
Enthuware Software Support
Posts: 4907
60
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Or if the subclass constructor calls another subclass constructor using 'this' : this(...); (Of course, on the first line)
-Paul.
------------------
Get Certified, Guaranteed!
(Now Revised for the new Pattern)
www.enthuware.com/jqplus

Try out the world's only WebCompiler!
www.jdiscuss.com
 
Cameron Park
Ranch Hand
Posts: 371
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Unless, super(param, etc.) is called. super() will always be called right? So explicitly calling super() is redundant?
 
Lam Thai
Ranch Hand
Posts: 117
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Cameron Park:
Unless, super(param, etc.) is called. super() will always be called right? So explicitly calling super() is redundant?


Right!
Lam
 
Ranch Hand
Posts: 62
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I may add a thing here Super will always be called if there is a default constructor in Immediate top class and there is no explicit super(parameters) call in the derived class.If there is no default constructor in yr super class relying on an implicit call to a constructor super() that doesnt exist will result in a compile time error!!!
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic