views:

251

answers:

3

isnt this one automatically put by the compiler if i don´t put it in a subclass's constructor?

that means i dont even should care about it? in some articles they put it out.

and if i got 1 constructor with arguments, will this be THE constructor, or does it takes a constructor without argument list?

+4  A: 

If the super class constructor has no arguments Java automatically calls it for you. If it has arguments you'll get an error.

src: http://java.sun.com/docs/books/tutorial/java/IandI/super.html

lemon
+13  A: 

Firstly some terminology:

  • No-args constructor: a constructor with no parameters;
  • Accessible no-args constructor: a no-args constructor in the superclass visible to the subclass. That means it is either public or protected or, if both classes are in the same package, package access; and
  • Default constructor: the public no-args constructor added by the compiler when there is no explicit constructor in the class.

So all classes have at least one constructor.

Subclasses constructors may specify as the first thing they do which constructor in the superclass to invoke before executing the code in the subclass's constructor.

If the subclass constructor does not specify which superclass constructor to invoke then the compiler will automatically call the accessible no-args constructor in the superclass.

If the superclass has no no-arg constructor or it isn't accessible then not specifying the superclass constructor to be called (in the subclass constructor) is a compiler error so it must be specified.

For example:

public class Base { }
public class Derived extends Base { }

This is fine because if you add no constructor explicitly Java puts in a public default constructor for you.

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

Also fine.

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

The above is a compilation error as Base has no default constructor.

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

This is also an error because Base's default constructor is private.

cletus
ok, and regarding my second question, which is the default constructor that gets called if i have one with no args and one with args?
weng
"Default constructor" always means "constructor taking no arguments".
Michael Madsen
Actually technically "default constructor" means "the no-arg constructor provided by the compiler if you don't define any constructor at all in the class". It's more correct to refer to these as "no-arg constructors".
matt b
A: 

If your method overrides one of its superclass's methods, you can invoke the overridden method through the use of the keyword super.

The syntax for calling a superclass constructor is

super();  
--or--
super(parameter list);

With super(), the superclass no-argument constructor is called. With super(parameter list), the superclass constructor with a matching parameter list is called.

Note: If a constructor does not explicitly invoke a superclass constructor, the Java compiler automatically inserts a call to the no-argument constructor of the superclass. If the super class does not have a no-argument constructor, you will get a compile-time error. Object does have such a constructor, so if Object is the only superclass, there is no problem.

giri
so with other words, whenever i create a constructor with arguments i MUST construct a constructor without arguments of i will get compilation errors?
weng
@noname - not correct. You will ONLY get compilation errors if something tries to (explicitly or implicitly) use the no-args constructor. If nothing needs to use it, you won't get compilation errors.
Stephen C