In Java, I can’t create instances of abstract classes. So why doesn’t eclipse scream about the following code?
public abstract class FooType {
private final int myvar;
public FooType() {
myvar = 1;
}
}
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
The code is fine, the final variable is initialized in the constructor of
FooType.You cannot instantiate
FooTypebecause of it being abstract. But if you create a non abstract subclass ofFooType, the constructor will be called.If you do not have an explicit call to
super(...)in a constructor, the Java Compiler will add it automatically. Therefore it is ensured that a constructor of every class in the inheritance chain is called.