1 min read

Abstract classes and methods in Java

Abstract classes and methods in Java
abstract class and methods

An abstract class, similar to an interface serves as a base to create subclasses. It cannot be instantiated, may or may not contain an abstract method.

package card;

import card.enumeration.Orientation;
import card.enumeration.CardFace;
import card.enumeration.CardType;

public abstract class Card {
  protected String serial;
  protected String qrCodeImageUrl;
  protected CardAttribute cardAttribute;
  public String getQrCodeImageUrl() {return this.qrCodeImageUrl;}
  public String getCardSerial() {return this.cardSerial;}
}

The snippet above is an abstract class containing fields and methods. Notice it does not need to have an abstract method.

Points to note about abstract classes and methods

  1. An abstract class is one declared with the keyword abstract
  2. An abstract class can be declared without an implementation.
  3. An abstract class may contain any number of methods, including zero.
  4. Methods in an abstract class can be concrete or abstract.
  5. An abstract class may or may not contain abstract methods
  6. An abstract class can be subclassed
  7. An abstract class can contain fields that are non static and final
  8. Public, protected and private concrete methods can be declared in an abstract class.
  9. All methods in an abstract class, declared as default are implictly public
  10. An abstract methods may not appear in a class that is not abstract.
  11. The first concrete sublass of an abstract class is required to implement all abstract methods that were not implemented in a superclass