An abstract class is a contract in the form of a class. This type of classes are designed to be inherited and can not be instantiated.
An abstract class is suitable when you need a class/contract that will be inherited/followed by a set of other classes, and at the same time having objects as instances of this class/contract does really help.
For a better understanding, let's have an example.
Imagine that we have a class Tool
and others classes that inherit from it. Since a Tool
is not really practical, we will transform the Tool
class to an abstract class, so that we still can inherit from it without instantiating it. In other words, a screwdriver is a practical tool, but the word tool in itself is a concept and not a practical thing.
To declare an abstract class, all you need to do is to add the abstract keyword in front of the class declaration.
Note that an abstract class can have some method declarations.
abstract class Tool{
double price;
double name;
List<String> useCases;
abstract void use();
double getPrice(){return price};
double getName(){return name};
double getUseCases(){return usecases};
}
class ScrewDriver extends Tool{
public ScrewDriver(){
price = 6.99;
name = "screwdriver";
usecases = new ArrayList<>();
usecases.add("Turning screws", "Opening jars XD");
}
void use(){
System.out.println("Screwdriver is turning the screw...!");
}
}
An interface is just like an abstract class, in a sense that it is also a mechanism to define a contract that should be followed by all the classes that implement it. But there are some key differences that we will discuss later.
Keep in mind that every class that implements an interface should declare all the methods that are in the interface.
To declare an interface, all you need is to use the interface keyword.
interface User{
void setFullName(String name);
void setAge(int age);
void setEmail(String email);
String getFullName();
String getLastName();
String getFirstName();
String getEmailServer();
}
class Customer implements User{
String fullName, email;
int age;
public Customer(){
...
}
void setFullName(String name){
this.fullName = name;
}
void setAge(int age){
this.age = age;
}
void setEmail(String email){
this.email = email;
}
String getFullName(){
return this.fullName;
}
String getLastName(){
return this.fullName.split(" ")[1];
}
String getFirstName(){
return this.fullName.split(" ")[0];
}
String getEmailServer(){
return this.email.split("@")[1];
}
}
- You can implement multiple interfaces but you can only extend one abstract class.
- In an abstract class you can declare some functions, but you can not do so in an interface.
- In an interface every attribute is forced to be static & final, in an abstract class you can have attributes that will be overwritten in a subclass.
- An interface can be used as a type, but an abstract class can not be used to do so.
- An interface can be used as a functional interface, but an abstract class can not.
- Every method in an interface is considered as abstract, but in an abstract class you can define some methods.