Java “abstract’ meaning: What does it mean when a method or class is abstract?

An abstract class in Java cannot be directly instantiated; only its subclasses can be instantiated.

An abstract class may contain zero or more abstract methods. An abstract method is not defined in the abstract class. Instead its signature is declared, but it has no body. The implementation is left to the subclasses that are created from this abstract class.

Here's an example of an abstract class definition:

public abstract class MyFirstAbstractClass 
{
  // your code goes here ...
}

Here's an example of an abstract method declaration that might be found inside your abstract class:

public abstract String getName();

If you haven't seen that syntax before it may look pretty crazy. But again, you're just defining the method signature; the classes that are derived from your base abstract class must then implement this method however they see fit.

A few final notes: abstract methods can only be declared in abstract classes. An abstract class does not have to have an abstract method, but in the real world they usually do. And, as mentioned, whenever a subclass of an abstract class is created, it must define the behavior of the abstract methods that are defined in the abstract class.

Add new comment

Anonymous format

  • Allowed HTML tags: <em> <strong> <cite> <code> <ul type> <ol start type> <li> <pre>
  • Lines and paragraphs break automatically.