java
  1. java-abstract-class

Java Abstract class

In Java, an abstract class is a class that cannot be instantiated and is used as a template for other classes. Abstract classes are designed to be extended by other classes, which they provide a common template and set of methods for. In this article, we'll take a look at Java abstract classes and how they can be used in your programs.

Syntax

The syntax to create an abstract class in Java is as follows:

abstract class MyClass {
  // variables and methods
}

An abstract class is created using the abstract keyword. The class may contain any number of variables, constructors, and methods.

Example

Here is an example of an abstract class in Java:

abstract class Animal {
  public abstract void makeSound();
}

class Dog extends Animal {
  public void makeSound() {
    System.out.println("Bark bark!");
  }
}

class Cat extends Animal {
  public void makeSound() {
    System.out.println("Meow!");
  }
}

public class Main {
  public static void main(String[] args) {
    Dog myDog = new Dog();
    myDog.makeSound();

    Cat myCat = new Cat();
    myCat.makeSound();
  }
}

In this example, we have an abstract class Animal with an abstract method makeSound(). We then have two subclasses, Dog and Cat, which extend the Animal class and implement the makeSound() method. Finally, in the main method, we create instances of Dog and Cat and call their makeSound() methods.

Output

The output of the above example will be:

Bark bark!
Meow!

Explanation

In this example, we create an abstract class Animal with an abstract method makeSound(). We then have two subclasses, Dog and Cat, which extend the Animal class and implement the makeSound() method.

The makeSound() method is marked as abstract in the Animal class, which means that it must be implemented by any class that extends Animal. This allows us to define a set of methods that must be implemented by any class that is based on the Animal template.

We then create instances of Dog and Cat and call their makeSound() methods, which print out the appropriate sound for each animal.

Use

Abstract classes in Java are used as a template for other classes. They provide a common set of methods and variables that can be used by any class that extends them.

Abstract classes are used when you want to provide a common interface that can be used by many different classes, while still allowing those classes to be customized in their own way. They can be used to define a set of methods that must be implemented by any subclass, or to define a set of constants or variables that are shared between all subclasses.

Important Points

  • An abstract class cannot be instantiated.
  • An abstract class can contain any number of variables, constructors, and methods.
  • An abstract method is a method without a body, which must be implemented by any class that extends the abstract class.
  • An abstract class can be extended by any number of subclasses.

Summary

Java abstract classes provide a useful way to define a template for other classes in your program. They can define a set of methods that must be implemented by any class that extends them, or they can provide a set of variables that are shared among all subclasses. By using abstract classes, you can create a common interface for many different types of objects, while still allowing those objects to be customized in their own way.

Published on: