What exactly is the difference between an interface and abstract class?
相关问题
- how to define constructor for Python's new Nam
- Keeping track of variable instances
- Object.create() bug?
- std::vector of objects / pointers / smart pointers
- Name for a method that has only side effects
相关文章
- List可以存储接口类型的数据吗?
- 接口B继承接口A,但是又不添加新的方法。这样有什么意义吗?
- NameError: name 'self' is not defined, eve
- Implementation Strategies for Object Orientation
- Alternatives to abstract classes in Ruby?
- Check if the Type of an Object is inherited from a
- When to use Interfaces in PHP
- Can Eclipse auto-generate an interface of a 3rd pa
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.
Some important differences:
In the form of a table:
As stated by Joe from javapapers:
When you want to provide polymorphic behaviour in an inheritance hierarchy, use abstract classes.
When you want polymorphic behaviour for classes which are completely unrelated, use an interface.
I am constructing a building of 300 floors
The building's blueprint interface
Building constructed up to 200 floors - partially completed---abstract
Building construction completed-concrete
Interface
Abstract
Taken from DurgaJobs Website
The main point is that:
I don't want to highlight the differences, which have been already said in many answers ( regarding public static final modifiers for variables in interface & support for protected, private methods in abstract classes)
In simple terms, I would like to say:
interface: To implement a contract by multiple unrelated objects
abstract class: To implement the same or different behaviour among multiple related objects
From the Oracle documentation
Consider using abstract classes if :
Consider using interfaces if :
Serializable
interface.abstract class establishes "is a" relation with concrete classes. interface provides "has a" capability for classes.
If you are looking for
Java
as programming language, here are a few more updates:Java 8 has reduced the gap between
interface
andabstract
classes to some extent by providing adefault
method feature. An interface does not have an implementation for a method is no longer valid now.Refer to this documentation page for more details.
Have a look at this SE question for code examples to understand better.
How should I have explained the difference between an Interface and an Abstract class?