Skip to main content

Abstraction

Java supports abstraction in several different ways:

  • Java allows us to create a class hierarchy, where the top of the hierarchy, the base class, is usually an abstract concept, whether it's an abstract class or not
  • Java lets us create abstract classes
  • Java gives us a way to create interfaces

Abstract methodโ€‹

  • An abstract method is unimplemented
  • We can think of an abstract method as a contract, that promises that all subtypes will provide the promised functionality, with the agreed upon name and arguments

Concrete methodโ€‹

  • A concrete method has a method body, usually with at least one statement
  • A concrete method is said to implement an abstract method, if it overrides one

Method Modifiersโ€‹

ModifierPurpose
abstractWhen we declare a method abstract, a method body is always omitted. An abstract method can only be declared on an abstract class or an interface
staticSometimes called a class method, rather than an instance method, because it's called directly on the Class instance
finalCannot be overridden by subclasses
defaultThis modifier is only applicable to an interface. They allow us to add new methods to an interface that are automatically available in the implementations
nativeThis is another method with no body. The method body will be implemented in platform-dependent code, typically written in another programming language
synchronizedIndicate that a method can be accessed by only one thread at a time

Abstract classโ€‹

Whenโ€‹

  • We want to share code among several closely related classes
  • We expect classes that extend our abstract class to have many common methods or field or require access modifiers other than public
  • We want to declare non-static or non-final fields, so this enables us to define methods that can access and modify state of an object
  • We have a requirement for our base class to provide a default implementation of certain methods but other methods should be open to being overridden by child classes

Summary: An abstract class provides a common definition as a base class that multiple derived classes can share

Interfaceโ€‹

Whenโ€‹

  • We expect that unrelated classes will implement our interface
  • We want to specify the behavior of a particular data type, but we are not concerned about who implements its behavior
  • We want to separate different behavior

Summary: The interface decouples the "what" from the "how" and is used to make different types behave in similar ways

Abstract class vs Interfaceโ€‹

Abstract ClassInterface
An instance can be created from itโŒโŒ
Has a constructorโœ…โŒ
Records and enums can extend or implementโŒโœ…
Inherits from java.lang.Objectโœ…โŒ
Can have both abstract and concrete methodsโœ…โœ… (as of JDK 8)
Abstract methods must include abstract modifierโœ…โŒ
Supports default modifier for it's methodsโŒโœ… (as of JDK 8)
Can have instance fieldsโœ…โŒ
Can have static fieldsโœ…โœ… (implicitly public static final)

A class can use extends and implements in same declarationโ€‹

public class Bird extends Animal implements FlightEnabled, Trackable {

}

final modifierโ€‹

  • for a method: a final means it can't be overridden by a subclass
  • for an object's field: can't be reassigned or given a different value, after its initialization
  • for a static field: class field that can't be reassigned, or given a different value, after the class's initialization process
  • for a class: can't be overridden, meaning no class can use it, in the extends clause
  • for a variable, in a block of code: once it's assigned a value, any remaining code in the block can't change it
  • for a final method parameter: we can't assign a different value to that parameter in the method code block

Constantsโ€‹

A constant variable is a final variable of primitive type, or type String, that is initialized with a constant expression