Constructors are not inherited, you must create a new, identically prototyped constructor in the subclass that maps to its matching constructor in the superclass.
Here is an example of how this works:
class Foo {
Foo(String str) { }
}
class Bar extends Foo {
Bar(String str) {
// Here I am explicitly calling the superclass
// constructor - since constructors are not inherited
// you must chain them like this.
super(str);
}
}
Answer from Andrew Hare on Stack OverflowConstructors are not inherited, you must create a new, identically prototyped constructor in the subclass that maps to its matching constructor in the superclass.
Here is an example of how this works:
class Foo {
Foo(String str) { }
}
class Bar extends Foo {
Bar(String str) {
// Here I am explicitly calling the superclass
// constructor - since constructors are not inherited
// you must chain them like this.
super(str);
}
}
Superclass constructor CAN'T be inherited in extended class. Although it can be invoked in extended class constructor's with super() as the first statement.
I am doing a lesson right now about inheritance with super/sub class with constructors. The lesson says there are 2 ways have child constructor inherit and parent one. The first is with super keyword and this works...
The parent class is below...
public class Shape {
int sides;
public Shape(int numSides) {
sides = numSides;
System.out.println("this obj has "+ sides +" sides.");
}and the child class using "super"
public class Triangle extends Shape {
public Triangle() {
super(3);
}
public static void main(String[] args) {
Triangle tr = new Triangle();which outputs when runs...
"this obj has 3 sides"
Now, the other way they said can do is bypass the parent constructor and reference the parent field using this. like below (parent class is unchanged)
child class using "this" keyword
public class Triangle extends Shape {
public Triangle() {
this.sides = 3;
}
public static void main(String[] args) {
Triangle tr = new Triangle();which i tried but get the error ..
"Implicit super constructor Shape() is undefined. Must explicitly invoke anotherconstructor".
it says the super() constructor is secretly called in this case by the compiler, so why is this error happening? What in this concept am i missing?
Thank you
There is an implicit call to super() with no arguments for all classes that have a parent - which is every user defined class in Java - so calling it explicitly is usually not required. However, you may use the call to super() with arguments if the parent's constructor takes parameters, and you wish to specify them. Moreover, if the parent's constructor takes parameters, and it has no default parameter-less constructor, you will need to call super() with argument(s).
An example, where the explicit call to super() gives you some extra control over the title of the frame:
class MyFrame extends JFrame
{
public MyFrame() {
super("My Window Title");
...
}
}
A call to your parent class's empty constructor super() is done automatically when you don't do it yourself. That's the reason you've never had to do it in your code. It was done for you.
When your superclass doesn't have a no-arg constructor, the compiler will require you to call super with the appropriate arguments. The compiler will make sure that you instantiate the class correctly. So this is not something you have to worry about too much.
Whether you call super() in your constructor or not, it doesn't affect your ability to call the methods of your parent class.
As a side note, some say that it's generally best to make that call manually for reasons of clarity.
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 no-args constructor is private.
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
btw, IMHO it's better form, to do this:
public class Person
...
public Person() {
this("", 0); // No-args constructor calls the arg'd version with default values
}
public Person(String nm, int ag) {
name = nm;
age = ag;
}
...
}
This means everything goes through the one constructor. If you have to add anything into your constructor, you have only one place to do change.
The only reason I can think of for the code above to not work when in separated files is if the following happened:
- You have another
Personclass - That "other"
Personclass has a different constructor - Your file with
BaseballPlayerclass imported that one instead
Conclusion - check your import statements (when the classes are in separate files).
By the way, removing public from the BaseballPlayer when they are in the same file is different then what you are trying to achieve - BaseballPlayer will be packag-private access, instead of public.
EDIT:
ah, since I am not familiar with NetBeans:
- perhaps the "original"
Personclass is not automatically compiled?
I am currently learning Java and when going through the Java tutorials on using the super keyword, I got confused.
It is stated:
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.
What does this actually mean? Suppose I have a superclass A and subclass B. There is a relationship Object <: A <: B.
class A {
Public A(int x) {}
}
class B extends A {
Public B(int x) {}
}So in the above case, I did not explicitly invoke a superclass constructor and did not use the super keyword at all. Why would this give me an error if I just call the constructor of B normally new B(int)? I am pretty sure I am misinterpreting the statement in the tutorial here. Can someone please explain it in greater clarity?