File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes super() ? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "super() ?" Watch "super() ?" New topic
Author

super() ?

Stu Higgs
Ranch Hand

Joined: Jan 01, 2006
Posts: 74
I just finished reading about inheritance in A Programmer's Guide to Certification and in one of the Sun self paced CD courses. From what I understand from the reading is that when you extend a class, by default the super classes default constructor will be called before anything happens at all in the subclass, and you may use super to call an explicit constructor in the super class. My question is why do I see a lot of code where super() is placed in the sub class constructor if the super class default constructor is going to be called by default? Am I missing something?
Nicholas Carrier
Ranch Hand

Joined: Apr 14, 2005
Posts: 78
Ok, I'm guessing at what you are looking at here because I don't have an example.

All the constructors in an object's inheritence tree must be run when you make a new object.

However, you may want to use a constructor that has an argument.

For example if an instance variable in the super class is marked private and the constructor has a setter builit into it to set that instance variable, but you don't have that instance variable in the inherited class you can do something like this.



public class Car {

private String name;

public Car(String aName) {

name = aName;

}

}

public class SportsCar extends Car {

public SportsCar(String brand) {

super(brand); // it send the name up the statck to the Car constructor
// other code like setSexiness();

}

}


I believe that's how it works, and if it isn't I'm sure someone will correct me soon enough

Basically even though the SportsCar inherits say a getBrand() method from the Car part of it self, it doesn't have a brand isntance variable. Instead, it relies on the Car part of it self for that information. Were it not for the super() constructor call you wouldn't be able to call a super constructor that takes arguments.

Hope that helps.
[ January 30, 2006: Message edited by: Nicholas Carrier ]

Teaching yourself anything is always the cheapest way, but it definitely takes a lot of time and effort.<br /> <br />Thank you javaranch <a href="http://"http://faq.javaranch.com/view?HowToAskQuestionsOnJavaRanch"" target="_blank" rel="nofollow">Learn How to Ask Your Question</a> and be nice
Stu Higgs
Ranch Hand

Joined: Jan 01, 2006
Posts: 74
That is exactly what I was talking about in my post. What you described is also my understanding of the use of super()... to call an explicit constructor in the super class from the sub class constructor.

What I was getting at though is that I see a lot of examples where a class is extended and a no param super() declaration(I think its a declaration) is coded into the subclass default constructor where it does not appear to be doing anything except calling the super class no arg default constructor which is going to be called anyway whether we place super() in the subclass default constructor or not.

My question was whether this is redundant to do, and is it serving any purpose at all when placed in the subclass constructor with no params to signify a call to anything other than the super classes default constructor. Is there some other reason people would code super() into the subclass default constructor, or is it possible that the examples I am looking at were written by someone who doesn't understand how it works? I'm self taught in Java, so sometimes it can get pretty ugly trying to figure things out, so when i read something and think that i understand it, and then see something that doesn't seem to make much sense makes me wonder if I am missing the boat. Here is an example for clarity.

public abstract class Animal {

abstract void talk();

}


public class Dog extends Animal {

public Dog(){//explicit declaration of a default constructor
//Why have super()(see below) here when Animal's default constructor is going to be called by default unless we call an explicit Animal constructor from here?
super();
}

void talk(){

System.out.println("Woof, woof!");
}

}

I see this a lot, and I was wondering what it is doing there because it doesn't jive with what I have read and thought I understood. Stuff like that makes me question whether I really understand it or not.
[ January 30, 2006: Message edited by: Stu Higgs ]
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

Originally posted by Stu Higgs:
... I see this a lot, and I was wondering what it is doing there because it doesn't jive with what I have read and thought I understood. Stuff like that makes me question whether I really understand it or not...

Stu, it sounds like your understanding is correct. The only reason I can think of for including an explicit call to the no-args super constructor is clarity (although that seems a dubious reason :roll: ).

(Or am I missing something too?)


"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
sscce.org
Nicholas Carrier
Ranch Hand

Joined: Apr 14, 2005
Posts: 78
same here.
Layne Lund
Ranch Hand

Joined: Dec 06, 2001
Posts: 3061
I agree with marc. The only reason I can think of for this practice is to make the call explicit (READ: obvious).

Layne


Java API Documentation
The Java Tutorial
Stu Higgs
Ranch Hand

Joined: Jan 01, 2006
Posts: 74
Thanks everyone. Nice to know I am understanding this, well, maybe...
 
 
subject: super() ?
 
Similar Threads
SCJP certification
flow of execution
constructor invocation
can we have two constructors being called for the same object ???