A class containing even one abstract method has to be declared abstract. (has to)
But a class not having any abstract method may be declared as abstract. (you may) So effectively you could declare any class abstract, not having any abstract method. (The designer does not want that this class be instantiated.)
Originally posted by wei liu:
Question 191)
Read the following piece of code carefully
public abstract class AbstractClass
{
public AbstractClass()
{
System.out.println("this is an abstract class constructor!");
}
public void aMethod()
{
System.out.println("This is in the method in the abstract class");
}
}
Attempting to compile and run the code will cause
1 Compiler error - abstract classes cannot have constructors.
2 Compiler error - the method AbstractClass does not have a valid return type.
3 Compiler error - the class cannot be declared as abstract as it does not have any unimplemented methods.
4 No compiler error - the class is practically not an abstract class and can be instantiated.
5 No compiler error - the class cannot be instantiated directly. It has to be extended to an non-abstract class. The constructors of the extended class will call the constructor of the abstract class (implicitly or explicitly).
6 No compiler error - the class cannot be instantiated directly. It has to be extended to an non-abstract class. The constructors of the abstract class will never be called.
the ans is 5th. why? the abstract class should include abstract method,should not it?