Reputation: 5044
If I have a class
class Foo{
String name;
public Foo(String s){
name=s;
}
public void setName(String s){
name=s;
}
public String getName(){
return name;
}
}
and then
class FooBar extends Foo {
public FooBar(String S){
super(s);
}
}
and my main method is
public static void main(String[] args){
FooBar item1 = new FooBar("Jim");
}
Will the super() call in class FooBar then call the constructor for Foo() and properly set the name to s? Is this what a plain super() call does? Call the constructor of the class being extended?
Upvotes: 0
Views: 54
Reputation: 5148
Yes, that is exactly what it does. It calls the constructor for the super / base class.
Upvotes: 2
Reputation: 18460
super
always refers to your parent class. The sequence of code execution will be as follows:
new FooBar("Jim");
-> super(s);
-> Foo(String s)
which will set name=s
You do not have a default constructor so there is no super()
(i.e. without arguments) since you cannot create an instance of FooBar
without a String
argument
Upvotes: 1