Submitted by c-admin on Wed, 06/05/2019 - 02:27
Question:
Which one of the following class definitions is/are a legal definition of a class that cannot be instantiated?
class Automobile{
abstract void honk(); //(1)
}
abstract class Automobile{
void honk(); //(2)
}
abstract class Automobile{
void honk(){}; //(3)
}
abstract class Automobile{
abstract void honk(){} //(4)
}
abstract class Automobile{
abstract void honk(); //(5)
}
Select 2 options
A. 1
B. 2
C. 3
D. 4
E. 5
Answer and Explanation (Click to expand) Answer:
C - This is a valid abstract class although it doesn't have any abstract method.
E - This is a valid abstract class
Explanation:
Here are some points to remember:
A class is uninstantiable if the class is declared abstract.
If a method has been declared as abstract, it cannot provide an implementation i.e. a method body even if empty (and the class containing that method must be declared abstract).
If a method is not declared abstract, it must provide a method body (the class can be abstract but not necessarily so).
If any method in a class is declared abstract, then the whole class must be declared abstract.