Sometimes, we require just method declaration in super-classes. This can be achieve by specifying the abstract type modifier. These methods are sometimes referred to as subclasser responsibility because they have no implementation specified in the super-class. Thus, a subclass must override them to provide method definition. To declare an abstract method, use this general form:
abstract type method-name(parameter-list);
As you can see, no method body is present. Any concrete class(i.e. class without abstract keyword) that extends an abstract class must override all the abstract methods of the class.
Important rules for abstract methods:
- Any class that contains one or more abstract methods must also be declared abstract
- The following are various illegal combinations of other modifiers for methods with respect to abstract modifier:
- abstract native
- abstract synchronized
- abstract static
- abstract private
- abstract strictfp
Consider the following Java program, that illustrate the use of abstract keyword with classes and methods.
B's implementation of m2. This is a concrete method.
Note: Although abstract classes cannot be used to instantiate objects, they can be used to create object references, because Java’s approach to run-time polymorphism is implemented through the use of super-class references. Thus, it must be possible to create a reference to an abstract class so that it can be used to point to a subclass object.
- Why can't static methods be abstract in Java?
- Java.util.BitSet class methods in Java with Examples | Set 2
- DoubleBuffer put() methods in Java with Examples | Set 1
- ByteBuffer put() methods in Java with Examples | Set -1
- FloatBuffer put() methods in Java with Examples
- DoubleBuffer get() methods in Java with Examples
- FloatBuffer get() methods in Java with Examples
- StringTokenizer methods in Java with Examples | Set 2
- Buffer hasArray() methods in Java with Examples
- ShortBuffer clear() methods in Java with Examples
- FloatBuffer flip() methods in Java with Examples
- Buffer mark() methods in Java with Examples
- Buffer position() methods in Java with Examples
- Buffer clear() methods in Java with Examples
- Buffer array() methods in Java with Examples
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.