Abstraction hides implementation details and exposes essential functionality.
It is achieved using abstract classes and interfaces.
Abstract class | Interface |
|---|---|
Uses | Uses |
Can have constructors | Cannot have constructors |
Can have instance variables | Fields are implicitly public static final |
Can have abstract and concrete methods | Can have abstract, default, static and private methods (depending on Java version) |
A class extends one class | A class can implement multiple interfaces |
What is an abstract method?
An abstract method is a method declared using the
abstractkeyword and without a body (no curly braces {} or implementation code), ending with a semicolon instead.
abstract class Animal {
abstract void sound();
}