In: Computer Science
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 |
All of the above reasons are to use an abstarct class over a concrete class or interface
Explanation:-
--Abstract class os a class declared abstract
-- It may or may not contain abstract methods
-- Abstract classes cannot be instantiated but it can be subclassed
-- If a class contan abstract methods , the class itself must be declared abstract
Eg:- public abstract class Person {
// variable declaration
// declare non abstract methods
abstract void Home Address ();
}
-- Abstract classes are similar to the interfaces. You cannot instantiate them. They may contain a mix of methods declared with or without implementation
-- In abstract class you can declare fields that are static and final define public, protected, private concrete methods. With interface all fields are automatically public, static and final and all methods that you declare or define are public
-- You can implement any number of interfaces in the abstract classes