aspose file tools
The moose likes Beginning Java and the fly likes Base class constructor Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Reply Bookmark "Base class constructor" Watch "Base class constructor" New topic
Author

Base class constructor

Tmmet Johnson
Ranch Hand

Joined: Nov 03, 2004
Posts: 56
Hi ,
What will happen if I have a derived class default constructor which does not call the base class constructor as below?
Will this cause any issues?
Thanks,

public class Base{
private String st = null;
public Base (){}
public Base(String str){
st = str;
}}

public class Derived extends Base{
public Derived(){}

}
}
Petrus Pelser
Ranch Hand

Joined: Feb 20, 2006
Posts: 132
No. The first line in a constructor must either be a call to super() which calls the superclass' constructor, or a call to this() which calls an overloaded constructor. If there are no call to this() or super(), the compiler will insert an implicit call to super() as the first line of your constructor. In your case, if you create an instance of the derived class, the default constructor of your base class will be called.
Petrus Pelser
Ranch Hand

Joined: Feb 20, 2006
Posts: 132
Another thing: In this case, if your base class did not have a no-args constructor, you will recieve a compiler error as the implicit call to super() with no arguments would be invalid.
sven studde
Ranch Hand

Joined: Sep 26, 2006
Posts: 148
cross posted
 
I agree. Here's the link: http://ej-technologies/jprofiler - if it wasn't for jprofiler, we would need to run our stuff on 16 servers instead of 3.
 
subject: Base class constructor
 
Similar Threads
A simple Q but still have doubt
Constructor doubt
URGENT!!!
need explanation
about constructors