Hello,
I would like to clarify a Java inheritance question that has appeared in an exam.
The question is:
"If we talk about inheritance, which of the following statements is true?"
A) A class can have as many subclasses as desired
B) A class can only inherit from one class at the same time
C) Constructors are not inherited, only the default one is
D) All of the above
The expected answer is D.
However, I think option C is not correct, or at least it is misleading.
My understanding is that constructors are not inherited in Java. A subclass constructor can invoke a superclass constructor using super(...), and if no constructor is declared in the subclass, the compiler generates a default constructor that implicitly calls super().
But I understand this as constructor invocation, not constructor inheritance.
For example:
class Parent {
Parent() {
System.out.println("Parent constructor");
}
}
class Child extends Parent {
// The compiler generates:
// Child() {
// super();
// }
}
In this case, I would say that Child does not inherit Parent(), but has its own default constructor which invokes Parent().
So my questions are:
1. Is it correct to say that "only the default constructor is inherited"?
2. According to the Java Language Specification, should option C be considered false or misleading?
Thank you.