This article helps you understand what is abstract keyword in Java and how to use it in the Java programming language.

You know, the abstract keyword is used to declare a class or a method as abstract. An abstract class can have abstract methods which have to be implemented by its sub classes. An abstract method does not have concrete implementation or body, and must ends with a semicolon. The following example declares a class Car as abstract:

public abstract class Car {
    public abstract void drive();
}
The method drive() is also declared as abstract.

 

1. Some rules for abstract class and abstract method:

An abstract class:

 

An abstract method:

2. Java abstract class and method example

A class that extends the abstract Car class and implements its abstract method:

class PoliceCar extends Car {
    public void drive() {
        // drive faster than normal car
    }
} 
An abstract class has both abstract and non-abstract methods:

abstract class Airplane {
    public abstract void takeOff();
    public void landing() {
        // landing smoothly
    }
}


See all keywords in Java.

 

Related Topics:

 

Other Recommended Tutorials:


About the Author:

is certified Java programmer (SCJP and SCWCD). He started programming with Java in the time of Java 1.4 and has been falling in love with Java since then. Make friend with him on Facebook and watch his Java videos you YouTube.