When to use abstract class or interface? [closed]

2019-01-01 02:16发布

Why are abstract or interface classes created, or when should we use abstract or interface classes?

6条回答
路过你的时光
2楼-- · 2019-01-01 02:46

See Interface is basically a "Contract". When you are defining an interface you are defining a Contract. Where abstract classes are extended, interfaces are Implemented.

Let's consider an example.

public interface Friend {
void hello();
}

Now you have defined a contract which says that any class which wants to implement Friend needs to provide a definition for method hello().

Here is an implementation:

public class myFriend implements Friend {
public void hello()
println("Done");
}

Now myFriend has fulfilled the contract. Now the question is: Where should interfaces be used?

Interfaces help you define a behavior which must be implemented. Say you have a class A which defines some functionality. You want the other classes to use this class functionality only if they define particular behavior (methods). You enforce this restriction in term of interface.

查看更多
泪湿衣
3楼-- · 2019-01-01 02:56

Interface is used when you only want to declare which methods and members a class MUST have. Anyone implementing the interface will have to declare and implement the methods listed by the interface.

If you also want to have a default implementation, use abstract class. Any class extending the abstract class will have to implement only its abstract methods and members, and will have some default implementation of the other methods of the abstract class, which you may override or not.

--EDIT - forgot to mention, Earwicker reminded me

Finally, you can implement as many interfaces as you want, but only extend one class (being it abstract or not). Keep that in mind before choosing.

查看更多
萌妹纸的霸气范
4楼-- · 2019-01-01 02:57

SamuelCarrijo seems to have answered this question well.

In addition for Java, some frameworks require an interface to work with. I'm thinking of (say) dynamic proxies, or some client/server proxying frameworks. This is because they use introspection on the object to determine methods implemented by the interfaces implemented by the object. So occasionally you have to implement an interface for an object where, perhaps, you wouldn't normally bother.

Note this reason for interfaces is specific to Java.

查看更多
深知你不懂我心
5楼-- · 2019-01-01 03:02

The key difference is that you can implement multiple interfaces in a class, but only extend a single abstract class. This is because an abstract class can also define fields that store data, whereas an interface cannot.

查看更多
姐姐魅力值爆表
6楼-- · 2019-01-01 03:04

Abstract classes are used when you are building an inheritance hierarchy. However, most inheritance hierarchies should not be too "deep" (i.e. too many levels of inheritance). Many object oriented design books will favor interfaces over inheritance (one book I read once quoted a developer as saying that "inheritance is the single coolest [object oriented] feature you won't implement"), as this allows classes to be assigned behaviors "by contract", where the contract is the interface.

It is worth noting samuelcarrijo's answer - if you want to have a default implementation of a method, you would have to use an abstract class that has a concrete implementation of the method to give it a default implementation. This default implementation can be overridden in child classes.

Hope this helps!

查看更多
无色无味的生活
7楼-- · 2019-01-01 03:06

An abstract class is a class, that has atleast one abstract method or you can also make all your methods as abstract. Obviously it cannot be instantiated. You have to inherit from an abstract class and implement the abstract methods in the inheriting class (i.e, the class extending the abstract class).

Interfaces are not classes at all (so don't call them interface class). Interfaces define the signature of methods without any implementation. Also interfaces have no member-fields. If you implement an interface in a class, you have to provide implementations for all the methods provided by the interface.

It makes sense to define a generalized API for some stuff, that can have completely different implementations. Abstract classes are more useful for classes that do mainly the same, but have some subtle differences. You can combine both approaches.

A good example is the collections framework of the Java class-library. You have the interface List, that defines how Lists have to behave. Some implementations are for instance ArrayList and LinkedList. As they behave similar, the stuff that works the same for both is implemented in the abstract class AbstactList, both inherit this.

查看更多
登录 后发表回答