Which of the following situations described is a good situation to use an abstract class, and where it would be better to use than either a concrete class or an interface?
When some methods in the class are abstract and some are concrete, and some variables in the class need to be private, while other variables need to be public |
||
When the inheriting class is closely related to the abstract class, such as with an "is-a" relationship |
||
When the objects instantiated from the abstract class hierarchy need to be grouped together |
||
When the class will have both concrete and abstract methods |
||
All of the above are reasons to use an abstract class over a concrete class or an interface |
Choice(2) is correct, i.e., when the inheriting class is closely related to the abstract class, such as with an "is-a" relationship.
Expalantion :
Class declared as abstract can't be instantiated directly, that is, instantiating abstract class using new keyword is not supported. A method inside the abstract class should not be defined and can only be declared. A good way of accessing abstract class is to create a concrete class(class which inherits the base, that is, abstract class)and further this class should be used to define the abstract methods present in abstract class. So, 'is-a' relationshiop which provides code reusability feature by providing inheritance functionality will be implemented.
Get Answers For Free
Most questions answered within 1 hours.