World class Hard Drive Recovery and renowned raid recovery services

WestNIC provides reliable web hosting services

Site navigation below

This FAQ is part of the Code Style Help and FAQ section. Use the help request form below if your question is not answered here, but make sure you are asking the right question first.

Subscribe to this FAQ: RSS news feed

FAQ search

Understanding abstract classes

Q: When should I use abstract methods?

A: Abstract methods are usually declared where two or more subclasses are expected to fulfil a similar role in different ways. Often the subclasses are required to the fulfil an interface, so the abstract superclass might provide several of the interface methods, but leave the subclasses to implement their own variations of the abstract methods. Abstract classes can be thought of as part-complete templates that make it easier to write a series of subclasses.

For example, if you were developing an application for working with different types of documents, you might have a Document interface that each document must fulfil. You might then create an AbstractDocument that provides concrete openFile() and closeFile() methods, but declares an abstract displayDocument(JPanel) method. Then separate LetterDocument, StatementDocument or InvoiceDocument types would only have to implement their own version of displayDocument(JPanel) to fulfil the Document interface.

Q: Must a superclass be abstract or concrete?

A: A superclass may be abstract or concrete, provided the concrete class is not declared final. In both cases, you can add supplementary methods to those inherited from the superclass. To extend abstract classes you must fulfil any abstract methods that are declared by the superclass.

Q: 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.

Q: 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.

Q: Do I have to implement all abstract methods?

A: Yes, it is possible to extend an abstract class without implementing its abstract methods, but in this case the subclass must also be declared abstract. If the subclass is not declared abstract, the compiler will throw an exception. So ultimately it is necessary for any concrete subclass to implement the abstract methods.

Q: Can a static method be abstract?

A: The abstract keyword cannot be applied to static method declarations. The compiler will reject the class with the error "illegal combination of modifiers".

Q: 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.

Abstract classes and methods

Q: How can I call a concrete instance method on an abstract class?

A: It is only possible to call a concrete method of an abstract class when the method is instantiated through a concrete subclass. The method must also have a public or package visibility modifier that makes it accessible to the calling class. When you call the method on the subclass, it implicitly invokes superclass implementation, as in the example below.

Premium Content: Follow this link for subscription information More details available to subscribers:
How can I call a concrete instance method on an abstract class?

Q: Can you give an example of an abstract class?

A: Yes, this example of an abstract class hierarchy has a superclass AbstractShape that includes a concrete moveTo(int, int) method that will be inherited by any subclass. It also includes an abstract double getArea() method that must be implemented by any concrete subclass. The implementation of the getArea() method is different in the Circle and Square subclasses.

Premium Content: Follow this link for subscription information More details available to subscribers:
Can you give an example of an abstract class?

Help request

Use the form below to submit a help request or general enquiry about the Code Style Web site. Read our guidelines on asking the right questions first.

Information: Your email address will not be mis-used. If you include your address you may be sent a personal reply, you will not be added to any mailing list unless you request it. Read the site privacy statement for details.

Add this page to your chosen social bookmarking service

Style warning - please read

Home · CSS · Java · Javascript · HTML · Help · Log