Java - Abstraction and Interface
In this note, you will quickly know about the Abstract Classes and Interfaces.
Abstraction
The abstract
keyword is a non-access modifier, used for classes and methods:
- Abstract class: a restricted class that cannot be used to create objects (to access it, it must be inherited from another class).
- cannot instantiate an abstract class using the
new
operator - constructor can be defined, which are invoked in the constructors of its subclasses
- An abstract class can contain regular methods and abstract methods.
- A subclass can be abstract even if its superclass is concrete.
- cannot instantiate an abstract class using the
- Abstract method: can only be used in an abstract class, and it does not have a body (no
{}
). The body is provided by the subclass (inherited from).- non-abstract subclass extended from an abstract class must have all the abstract methods implemented (even if they are not used in the subclass)
- subclass can override a abstract method from its abstract superclass
- If a subclass of an abstract superclass does not implement all the abstract methods, the subclass must be defined abstract as well.
A concrete class is a class that has an implementation for all of its methods.
Example - cannot instantiate an abstract class using the new
operator:
1 | abstract class GeometricObject { |
Example - Abstract Class and Abstract Method:
1 | // Abstract class |
Interface
An interface
is a completely “abstract class” that contains only constants and abstract methods.
In many ways, an interface is similar to an abstract class, but the intent of an interface is to specify behavior for objects.
- Like abstract classes, interfaces cannot be used to create objects
- cannot instantiate an abstract class using the
new
operator
- cannot instantiate an abstract class using the
- Interface has no constructors
- To access the interface methods, the interface must be “implemented” with the
implements
keyword- the idea is like inherited but you use
implements
keyword instead ofextends
- the idea is like inherited but you use
- You can use an interface as a data type for a variable, as the result of casting
In an interface:
- All data fields are
public static final
- All methods are
public abstract
If you omitted the modifiers in Interfaces, the compiler will automatically run as above modifiers.
In rare occasions, a class may implement two interfaces with conflict information (e.g., two same constants with different values or two methods with same signature but different return type).
This type of errors will be detected by the compiler.
Example - Interface Declaration:
1 | // interface |
Example: Access the interface methods using implements
keyword:
1 | // Interface |
Java does not support “multiple inheritance” (a class can only inherit from one superclass). However, it can be achieved with interfaces, because the class can implement multiple interfaces.
Example- Multiple Implement:
1 | interface FirstInterface { |
Interfaces vs. Abstract Classes
-
All classes share a single root, the Object class, but there is no single root for interfaces.
-
If a class extends an interface, this interface plays the same role as a superclass.
-
interface can be used as a data type and cast a variable of an interface type to its subclass, and vice versa
When to Use Interface or Class?
Abstract classes and interfaces can both be used to model common features.
When to Use Class inheritance
- a strong is-a relationship can be modeled using inheritance.
- i.e. x is a y
Example:
Teacher is a person.
So the Teacher
class extends the Person
(abstract) class.
When to Use Interface
- A weak is-a relationship can be modeled using interfaces.
- i.e. x is kind of y
Example1:
ArrayList
, LinkedList
, Vector
, Stack
are both kind of Lists.
So the ArrayList
, LinkedList
, Vector
, Stack
classes implements the List
interface.
Example2:
Pig
, Cat
, Dog
are both kind of Animals.
So the Pig
, Cat
, Dog
classes implements the Animal
interface.