Reputation: 4633
I know the following doesn't work, but can you help me understand why?
class A {
A(int x) {
System.out.println("apel constructor A");
}
}
class B extends A {
B() {
System.out.println("apel constructor B");
}
}
public class C {
public static void main(String args[]) {
B b = new B();
}
}
Upvotes: 4
Views: 879
Reputation: 27
I solve your problem two ways:-
class B extends A{ B(int x){super(x); System.out.println("hello second constructor");} public static void main(String args[]){B b =new B(10);}} 2.
class B extends A{ B(){super(any int value); System.out.println("hello second constructor");} public static void main(String args[]){B b =new B(10);}}
Upvotes: 1
Reputation: 7625
Every time you extend a class the constructor from the class you extend will be called (super constructor). If you declare a constructor it will override the implicit constructor and if that constructor has parameters the class from which you extend it won't be able to call the implicit constructor so you will have to explicitly call the constructor.
Example:
B() {
super(0);//integer value
System.out.println("apel constructor B"); }
}
Upvotes: 2
Reputation: 88
Whenever the constructor of a sub class is called in Java, it automatically calls the constructor of its super class first. Always the constructor of Super class is executed first and then the constructor of Sub class is executed. And in this case, the Super class Constructor requires an integer argument when called, which is not being provided by the code. you could try this:
class B extends A
{
B()
{
super(5);
System.out.println("apel constructor B");
}
}
Upvotes: 1
Reputation: 32468
You have to call the super class constructor in the sub class Constructor.
B is sub class, and it inherits super class A's behaviors. So, when you create an instance of B, B's constructor should call the super class Constructor.
B() {
super(1); // or super()
System.out.println("apel constructor B");
}
Upvotes: 2
Reputation: 161
When you call class 'B's constructor you should state that you wish the parents constructor to also be called e.g.
class B extends A{
B(){
super(0);
}
B(int i){
super(i);
}
}
either of those constructors should be fine
Upvotes: 2
Reputation: 1317
It doesn't compile because you haven't defined a default constructor for A and B constructor is calling for it.
Upvotes: 2
Reputation: 1500015
Every constructor (other than in Object
) must chain to another constructor as the very first thing it does. That's either using this(...)
or super(...)
.
If you don't specify anything, the constructor implicitly adds super()
to chain to a parameterless constructor in the superclass. Sooner or later, you need to go through a constructor for every level of the inheritance hierarchy. This ensures that the state of the object is valid from every perspective, basically.
In your case, you don't have a parameterless constructor in A
, hence why B
fails to compile. To fix this, you'd either need to add a parameterless constructor to A
, or explicitly chain to the parameterised A
constructor in B
:
public B() {
super(0); // Or whatever value you want to provide
}
See JLS section 8.8.7 for more details.
Upvotes: 4
Reputation: 48404
Your class A
uses an explicit argument-taking constructor, so the default no-args constructor is not present implicitly.
For your class B
to successfully extend A
you either need:
A
super(some int)
as first line of B
's constructorConsider that the constructor of a child class implicitly calls super()
.
Upvotes: 4
Reputation: 8708
This doesn't work because you must explicitly call one of the constructors of the super class A
, if class A
doesn't declare a default constructor.
Upvotes: 2