Java: Keyword “abstract”
The “abstract” Keyword
- The keyword
abstract
can be used on classe declaration. For exampleabstract MyClassX (…);
. - The keyword
abstract
can be used on method declaration. For exampleabstract int f (…);
. - When a class is declared
abstract
, it cannot be instantiated. - When a method is declared
abstract
, it cannot have definition. - Only abstract classes can have abstract methods. Abstract class does not necessarily require its methods to be all abstract.
Purpose of Abstract Classes
The purpose of abstract class is to serve purely as a parent of classes. (that is, solely for the purpose of extension (inheritance) by other classes.)
The difference between abstract class and “normal” class is that, the abstract class's methods cannot have any definition.
Syntax and Code Example
The abstract
keyword can be used on classes and methods. A class
declared with the abstract
keyword cannot be instantiated, and that is the
only thing the abstract
keyword does. Example of declaring a abstract class:
// declare a abstract class abstract CalendarSystem (String name);
When a class is declared abstract, then, its methods may also be declared abstract.
When a method is declared abstract, the method can not have a definition. This is the only effect the abstract keyword has on method. Here's a example of a abstract method:
// declare a abstract method abstract int get_person_id (String name);
Play with “abstract” Keyword
The following is a code template to experiment with. Begin by commenting out lines involving H2. Experiment with just defining a abstract class. See what is allowed, what is not. Then, define H2 that extends a abstract class. Get a feel of what needs to be there or not. Or, what kind of complaints compiler makes. Finally, create a object of a class that is inherited from a abstract class.
// a abstract class abstract class H { int x; /* abstract */ int y; // variables cannot be abstract /* abstract */ H () {x=1;} // constructor cannot be abstract void triple (int n) {x=x*3;}; // a normal method static int triple2 (int n) {return n*3;}; // a static method in abstract class is ok. abstract void triple3 (); // abstract method. Note: no definition. // static abstract void triple3 (int n); // abstract static cannot be combined int returnMe () {return x;} } // H1 extends (inherits) H. When a class extends a abstract class, // it is said to “implement” the abstract class. class H1 extends H { void triple3 () {x=x*3+1;} // must be defined, else compiler makes a complaint. //Also, all return type and parameter must agree with the parent class. } public class Abbst { public static void main(String[] args) { // H xx = new H(); // abstract class cannot be instantiated H1 jj = new H1(); // abstract class cannot be instantiated jj.triple3(); System.out.println(jj.returnMe()); } }
Examples of Abstract Class
For example, the java.lang.Number
is a abstract class.
java.lang.Number
's abstract methods includes
- byteValue()
- doubleValue()
- floatValue()
- intValue()
- longValue()
- shortValue()
The following classes:
{Byte, Double, Float, Integer, Long, Short}
are children of
java.lang.Number
,
and implement those methods, each differently.
It does not make sense to have a default implementation in java.lang.Number
for the methods, because each is really specific to the type.
Abstract Class Summary
As a parent class, what's the difference between abstract class and normal class?
- In a normal class, methods need to have definitions. If your class is to be a parent of other classes, it sometimes does not make sense to give a default definition for methods. For example: if you have a
GraphicObject
as parent, andRectangle
andCircle
as children. It does not make sense, to haveDrawIt
predefined inGraphicObject
. - When a class is declared abstract, it needs not to have definitions for its methods. Therefore, it is ideal, for serving as pure parent of classes.
Viewed alternatively, Java's class hierarchy system has a problem, in some cases, because of its required inherence of method definitions. A programer's workaround is to give a dummy definition in the parent class. (such as do nothing). Abstract class is the official solution at the language level.
What is the difference between abstract class and interface?
- Interface is a way to let classes have consistent set of methods.
- Abstract class is a way to group classes into one family. Abstract class and any children is part of the class hierarchy.
- Interfaces are not part of the class hierachy. (interfaces can have relationship with other interfaces)
- A class may be a child of only 1 abstract class. But a class can implement from multiple interfaces.
Their only similarity, is that both require the child class to have the methods implemented.