04 OOPS - Abstract Classes & Interfaces
04 OOPS - Abstract Classes & Interfaces
Interfaces
How to create an interface?
Why use an interface?
Abstract Classes
Why use an abstract class?
How to create an abstract class?
Reading List
Interfaces
In Java, an interface is an abstract type that contains a collection of methods and
constant variables.It can be thought of as a blueprint of behavior. It is one of the
core concepts in Java and is used to achieve abstraction, polymorphism and multiple
inheritances. An interface is a reference type in Java. It is similar to a class, but
it cannot be instantiated. It can contain only constants, method signatures, default
methods, static methods, and nested types. Method bodies exist only for default
methods and static methods.
@Override
public String getName() {
return name;
}
@Override
public String getEmail() {
return email;
}
}
1. Behavioral Functionality
// same as before
@Override
public int compareTo(Player otherPlayer) {
return Integer.compare(getRanking(), otherPlayer.getRanking());
}
}
2. Multiple Inheritances
Java classes support singular inheritance. However, by using interfaces, we’re also
able to implement multiple inheritances. For instance, in the example below, we notice
that the Car class implements the Fly and Transform interfaces. By doing so, it
inherits the methods fly and transform:
@Override
public void fly() {
System.out.println("I can Fly!!");
}
@Override
public void transform() {
System.out.println("I can Transform!!");
}
}
3. Polymorphism
Polymorphism is the ability for an object to take different forms during runtime. To
be more specific it’s the execution of the override method that is related to a
specific object type at runtime.
In Java, we can achieve polymorphism using interfaces. For example, the Shape
interface can take different forms — it can be a Circle or a Square.
@Override
public String name() {
return "Circle";
}
}
@Override
public String name() {
return "Square";
}
}
Finally, it’s time to see polymorphism in action using our Shape interface and its
implementations. Let’s instantiate some Shape objects, add them to a List, and,
finally, print their names in a loop:
shapes.add(circleShape);
shapes.add(squareShape);
Traditional interfaces in Java 7 and below don’t offer backward compatibility. What
this means is that if you have legacy code written in Java 7 or earlier, and you
decide to add an abstract method to an existing interface, then all the classes that
implement that interface must override the new abstract method. Otherwise, the code
will break.
Java 8 solved this problem by introducing the default method that is optional and can
be implemented at the interface level.
Summmary
Abstract Classes
There are many cases when implementing a contract where we want to postpone some parts
of the implementation to be completed later. We can easily accomplish this in Java
through abstract classes. Before diving into when to use an abstract class, let’s look
at their most relevant characteristics:
We define an abstract class with the abstract modifier preceding the class
keyword
An abstract class can be subclassed, but it can’t be instantiated
If a class defines one or more abstract methods, then the class itself must be
declared abstract
An abstract class can declare both abstract and concrete methods
A subclass derived from an abstract class must either implement all the base
class’s abstract methods or be abstract itself To better understand these
concepts, we’ll create a simple example.
Now let's create a class that extends the Person abstract class:
@Override
public String getName() {
return name;
}
@Override
public String getEmail() {
return email;
}
}
Why use an abstract class?
Now, let’s analyze a few typical scenarios where we should prefer abstract classes
over interfaces and concrete classes:
Sample Code Let’s have our base abstract class define the abstract API of a board
game:
Reading List
Comparators & Comparable Interface
Interface Inheritance
Functional Interfaces in Java 8
Sample Java Collections Interface - Queue Interface
Duck Typing
OOP in Python