Question Related to Abstract Class

Q1: Must abstract classes contain at least one abstract method?
A: No, abstract classes are not required to have any abstract methods, they can simply be marked abstract for general design reasons. An abstract class may contain a full set of functional, integrated methods but have no practical use in its basic form, for example. In other words, they may require extension with additional methods to fulfil a range of different purposes. If the purpose is not specified by abstract method signatures, the range of potential applications for subclasses can be very broad.

Q2: How can an abstract method be inherited?
A: A subclass inherits all non-private fields and methods of its superclass whether the methods are abstract or have concrete implementations. If a subclass does not implement an abstract method, the subclass must be declared abstract itself. That means that an abstract method can be inherited through numerous abstract classes without any concrete implementation.
A common use of abstract methods is the template design pattern, where the common behaviour of a class hierarchy is defined in a set of concrete methods in an abstract superclass. Those core methods include calls to abstract template methods which must be implemented in concrete subclasses. This makes it relatively easy to implement subclasses and produce type-specific behaviour in each.
 
Q3: Why can't an abstract method be declared private?
A: The private and abstract method modifiers do not make sense in combination and the compiler should normally fail with a warning in this case. An abstract method must be overridden by any subclass, but subclasses do not have access to their superclass' private fields, so a private abstract method could never be fulfilled.

Q4: Why can't abstract methods be declared static?
A: The rule against static abstract methods is fundamentally a Java language design decision, which is not explained in the Java Language Specification. The abstract inheritance and implementation scheme is concerned with forming a structured collaboration of classes with deferred implementation of its abstract instance methods. This scheme can be verified at compile time to ensure it is safe at runtime.
Static methods must be concrete because they are attached to a specific host class and must be available to execute in a static context, when no object instance exists. It is not possible to assign an implementation to a static method at runtime, there is no mechanism to do that in Java.
It is also worth noting that static methods cannot be overridden by a subclass in Java. Static methods with the same signature effectively hide the superclass method. The concept of implementing a static method in a subclass would fail for the same reason, the concrete implementation would be attached to the “wrong” host.

People who read this post also read :



2 comments:

Nice post. All the questions that you have posted above are really good. They cover up all the facts related to this concept. The answer provided with respect to each question is complete and helped me to understand about abstract class in a better way. Thanks.
sap pp

Such a nice post. KDSS Technologies provides the best software development services to its clients along with other IT services.That is what makes it one of the best software development company in Delhi NCR | software development company in Delhi

Post a Comment

Share

Twitter Delicious Facebook Digg Stumbleupon Favorites More