Publishers of technology books, eBooks, and videos for creative people

Home > Articles

  • Print
  • + Share This
This chapter is from the book

Abstract Methods

An abstract method establishes an interface but cannot contain execution details. Any subclasses must have their own implementation for a declared abstract method.

To declare a method as abstract, do not use the curly braces in your abstract class (Example 4.19). The containing class must also be marked as abstract.

EXAMPLE 4.19

abstract class Being extends Object
{
  Being()
  {
    print('-- Init Being--');
  }

  //this is an example of an abstract method - no execution statement
  void exist();
}

class Human extends Being
{
  void exist(){
    print('I am I'); //this is the implementation in the subclass
  }
}

main()
{
  Being woman = new Human();

  //woman is a variable with an abstract class type with a concrete instance
  woman.exist();
}

When you implement a concrete class that descends from a superclass with an abstract method, if the required method is not yet defined the interpreter will alert you that 'Missing concrete implementation of exist'. This alerts all subclasses that there is an expectation that they implement the abstract method.

  • + Share This
  • 🔖 Save To Your Account