What exactly is the difference between an interface and abstract class?
Let's work on this question again:
The first thing to let you know is that 1/1 and 1*1 results in the same, but it does not mean that multiplication and division are same. Obviously, they hold some good relationship, but mind you both are different.
I will point out main differences, and the rest have already been explained:
Abstract classes are useful for modeling a class hierarchy. At first glance of any requirement, we are partially clear on what exactly is to be built, but we know what to build. And so your abstract classes are your base classes.
Interfaces are useful for letting other hierarchy or classes to know that what I am capable of doing. And when you say I am capable of something, you must have that capacity. Interfaces will mark it as compulsory for a class to implement the same functionalities.
The shortest way to sum it up is that an interface
is:
default
and static
methods; while it has definitions (method signatures + implementations) for default
and static
methods, it only has declarations (method signatures) for other methods.interface
s, and an interface
can inherit from multiple interface
s). All variables are implicitly constant, whether specified as public static final
or not. All members are implicitly public
, whether specified as such or not.Meanwhile, an abstract
class is:
abstract
methods. Can contain both declarations and definitions, with declarations marked as abstract
.protected
, private
, or private package (unspecified).Or, if we want to boil it all down to a single sentence: An interface
is what the implementing class has, but an abstract
class is what the subclass is.
In short the differences are the following:
Syntactical Differences Between Interface and Abstract Class:
In Interfaces now:
public static
- supported
public abstract
- supported
public default
- supported
private static
- supported
private abstract
- compile error
private default
- compile error
private
- supported
An explanation can be found here: http://www.developer.com/lang/php/article.php/3604111/PHP-5-OOP-Interfaces-Abstract-Classes-and-the-Adapter-Pattern.htm
An abstract class is a class that is only partially implemented by the programmer. It may contain one or more abstract methods. An abstract method is simply a function definition that serves to tell the programmer that the method must be implemented in a child class.
An interface is similar to an abstract class; indeed interfaces occupy the same namespace as classes and abstract classes. For that reason, you cannot define an interface with the same name as a class. An interface is a fully abstract class; none of its methods are implemented and instead of a class sub-classing from it, it is said to implement that interface.
Anyway I find this explanation of interfaces somewhat confusing. A more common definition is: An interface defines a contract that implementing classes must fulfill. An interface definition consists of signatures of public members, without any implementing code.
To give a simple but clear answer, it helps to set the context : you use both when you do not want to provide full implementations.
The main difference then is an interface has no implementation at all (only methods without a body) while abstract classes can have members and methods with a body as well, i.e. can be partially implemented.
In an interface all methods must be only definitions, not single one should be implemented.
But in an abstract class there must an abstract method with only definition, but other methods can be also in the abstract class with implementation...