All methods in an abstract class must be declared as abstract methods

Similarly, in object-oriented programming, you may want to model an abstract concept without being able to create an instance of it. For example, the Number class represents the abstract concept of numbers. It makes sense to model numbers, but it doesn't make sense to create a generic number object. Instead, the Number class makes sense only as a superclass to such classes as Integer and Float, both of which implement specific kinds of numbers. A class such as Number, which represents an abstract concept and should not be instantiated, is called an abstract class. An abstract class can only be subclassed.

To declare that your class is an abstract class, use the keyword abstract before the class keyword in your class declaration:

abstract class Number {
    ...
}
If you attempt to instantiate an abstract class, the compiler displays an error message.

Abstract Methods

An abstract class can contain abstract methods — methods with no implementation. In this way, an abstract class can define a complete programming interface for its subclasses but allows its subclasses to fill in the implementation details of those methods. In practice, abstract classes usually provide a complete or partial implementation of at least one method. If an abstract class contains only abstract method declarations, it should be implemented as an interface instead. Interfaces are covered in the section Interfaces and Packages.

Let's look at an example of when you might want to create an abstract class with an abstract method in it. In an object-oriented drawing application, you can draw circles, rectangles, lines, Bezier curves, and so on. These graphic objects all have certain states (position, bounding box) and behaviors (move, resize, draw) in common. You can take advantage of these similarities and declare them all to inherit from the same parent object — for example,

abstract class GraphicObject {
    int x, y;
    ...
    void moveTo(int newX, int newY) {
        ...
    }
    abstract void draw();
}
0, as shown in the next figure.

All methods in an abstract class must be declared as abstract methods
Graphic objects are substantially different in many ways: drawing a circle is quite different from drawing a rectangle. The graphic objects cannot share these types of states or behavior. On the other hand, all
abstract class GraphicObject {
    int x, y;
    ...
    void moveTo(int newX, int newY) {
        ...
    }
    abstract void draw();
}
0s must know how to draw themselves; they just differ in how they are drawn. This is a perfect situation for an abstract superclass.

First, you would declare an abstract class,

abstract class GraphicObject {
    int x, y;
    ...
    void moveTo(int newX, int newY) {
        ...
    }
    abstract void draw();
}
0, to provide member variables and methods that were wholly shared by all subclasses, such as the current position and the
abstract class GraphicObject {
    int x, y;
    ...
    void moveTo(int newX, int newY) {
        ...
    }
    abstract void draw();
}
3 method.
abstract class GraphicObject {
    int x, y;
    ...
    void moveTo(int newX, int newY) {
        ...
    }
    abstract void draw();
}
0 also declares abstract methods for methods, such as
abstract class GraphicObject {
    int x, y;
    ...
    void moveTo(int newX, int newY) {
        ...
    }
    abstract void draw();
}
5, that need to be implemented by all subclasses but that are implemented in entirely different ways (no default implementation in the superclass makes sense). The
abstract class GraphicObject {
    int x, y;
    ...
    void moveTo(int newX, int newY) {
        ...
    }
    abstract void draw();
}
0 class would look something like this:

The abstract class in Java cannot be instantiated (we cannot create objects of abstract classes). We use the

abstract class Language {

  // abstract method
  abstract void method1();

  // regular method
  void method2() {
    System.out.println("This is regular method");
  }
}
3 keyword to declare an abstract class. For example,

// create an abstract class
abstract class Language {
  // fields and methods
}
...

// try to create an object Language
// throws an error
Language obj = new Language(); 

An abstract class can have both the regular methods and abstract methods. For example,

abstract class Language {

  // abstract method
  abstract void method1();

  // regular method
  void method2() {
    System.out.println("This is regular method");
  }
}

To know about the non-abstract methods, visit Java methods. Here, we will learn about abstract methods.


Java Abstract Method

A method that doesn't have its body is known as an abstract method. We use the same

abstract class Language {

  // abstract method
  abstract void method1();

  // regular method
  void method2() {
    System.out.println("This is regular method");
  }
}
3 keyword to create abstract methods. For example,

abstract void display();

Here,

abstract class Language {

  // abstract method
  abstract void method1();

  // regular method
  void method2() {
    System.out.println("This is regular method");
  }
}
5 is an abstract method. The body of
abstract class Language {

  // abstract method
  abstract void method1();

  // regular method
  void method2() {
    System.out.println("This is regular method");
  }
}
5 is replaced by
abstract class Language {

  // abstract method
  abstract void method1();

  // regular method
  void method2() {
    System.out.println("This is regular method");
  }
}
7.

If a class contains an abstract method, then the class should be declared abstract. Otherwise, it will generate an error. For example,

// error
// class should be abstract
class Language {

  // abstract method
  abstract void method1();
}

Example: Java Abstract Class and Method

Though abstract classes cannot be instantiated, we can create subclasses from it. We can then access members of the abstract class using the object of the subclass. For example,

abstract class Language {

  // method of abstract class
  public void display() {
    System.out.println("This is Java Programming");
  }
}

class Main extends Language {

  public static void main(String[] args) {
    
    // create an object of Main
    Main obj = new Main();

    // access method of abstract class
    // using object of Main class
    obj.display();
  }
}

Output

This is Java programming

In the above example, we have created an abstract class named Language. The class contains a regular method

abstract class Language {

  // abstract method
  abstract void method1();

  // regular method
  void method2() {
    System.out.println("This is regular method");
  }
}
5.

We have created the Main class that inherits the abstract class. Notice the statement,

obj.display();

Here, obj is the object of the child class Main. We are calling the method of the abstract class using the object obj.


Implementing Abstract Methods

If the abstract class includes any abstract method, then all the child classes inherited from the abstract superclass must provide the implementation of the abstract method. For example,

abstract class Animal {
  abstract void makeSound();

  public void eat() {
    System.out.println("I can eat.");
  }
}

class Dog extends Animal {

  // provide implementation of abstract method
  public void makeSound() {
    System.out.println("Bark bark");
  }
}

class Main {
  public static void main(String[] args) {

    // create an object of Dog class
    Dog d1 = new Dog();

    d1.makeSound();
    d1.eat();
  }
}

Output

Bark bark
I can eat.

In the above example, we have created an abstract class Animal. The class contains an abstract method

abstract class Language {

  // abstract method
  abstract void method1();

  // regular method
  void method2() {
    System.out.println("This is regular method");
  }
}
9 and a non-abstract method
abstract void display();
0.

We have inherited a subclass Dog from the superclass Animal. Here, the subclass Dog provides the implementation for the abstract method

abstract class Language {

  // abstract method
  abstract void method1();

  // regular method
  void method2() {
    System.out.println("This is regular method");
  }
}
9.

We then used the object d1 of the Dog class to call methods

abstract class Language {

  // abstract method
  abstract void method1();

  // regular method
  void method2() {
    System.out.println("This is regular method");
  }
}
9 and
abstract void display();
0.

Note: If the Dog class doesn't provide the implementation of the abstract method

abstract class Language {

  // abstract method
  abstract void method1();

  // regular method
  void method2() {
    System.out.println("This is regular method");
  }
}
9, Dog should also be declared as abstract. This is because the subclass Dog inherits
abstract class Language {

  // abstract method
  abstract void method1();

  // regular method
  void method2() {
    System.out.println("This is regular method");
  }
}
9 from Animal.


Accesses Constructor of Abstract Classes

An abstract class can have constructors like the regular class. And, we can access the constructor of an abstract class from the subclass using the

abstract void display();
6 keyword. For example,

abstract class Animal {
   Animal() {
      ….
   }
}

class Dog extends Animal {
   Dog() {
      super();
      ...
   }
}

Here, we have used the

abstract void display();
7 inside the constructor of Dog to access the constructor of the Animal.

Note that the

abstract void display();
6 should always be the first statement of the subclass constructor. Visit Java super keyword to learn more.


Java Abstraction

The major use of abstract classes and methods is to achieve abstraction in Java.

Abstraction is an important concept of object-oriented programming that allows us to hide unnecessary details and only show the needed information.

This allows us to manage complexity by omitting or hiding details with a simpler, higher-level idea.

A practical example of abstraction can be motorbike brakes. We know what brake does. When we apply the brake, the motorbike will stop. However, the working of the brake is kept hidden from us.

The major advantage of hiding the working of the brake is that now the manufacturer can implement brake differently for different motorbikes, however, what brake does will be the same.

Let's take an example that helps us to better understand Java abstraction.

Example 3: Java Abstraction

abstract class Language {

  // abstract method
  abstract void method1();

  // regular method
  void method2() {
    System.out.println("This is regular method");
  }
}
0

Output:

abstract class Language {

  // abstract method
  abstract void method1();

  // regular method
  void method2() {
    System.out.println("This is regular method");
  }
}
1

In the above example, we have created an abstract super class MotorBike. The superclass MotorBike has an abstract method

abstract void display();
9.

The

abstract void display();
9 method cannot be implemented inside MotorBike. It is because every bike has different implementation of brakes. So, all the subclasses of MotorBike would have different implementation of
abstract void display();
9.

So, the implementation of

abstract void display();
9 in MotorBike is kept hidden.

Here, MountainBike makes its own implementation of

abstract void display();
9 and SportsBike makes its own implementation of
abstract void display();
9.

Note: We can also use interfaces to achieve abstraction in Java. To learn more, visit Java Interface.

Are all methods in an abstract class abstract?

Not all methods in an abstract class have to be abstract methods. An abstract class can have a mixture of abstract and non-abstract methods. Subclasses of an abstract class must implement (override) all abstract methods of its abstract superclass.

Is it necessary to define all methods of abstract class?

It's not necessary for an abstract class to have abstract method. We can mark a class as abstract even if it doesn't declare any abstract methods. If abstract class doesn't have any method implementation, its better to use interface because java doesn't support multiple class inheritance.

Can we declare abstract class without abstract method?

Yes, we can declare an abstract class with no abstract methods in Java. An abstract class means that hiding the implementation and showing the function definition to the user.

Can we declare method as a abstract?

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.