what is the advantage of interface over abstract c

2020-05-19 04:16发布

Abstract class have both concrete and abstract function . interface only have abstract functions.

Overriding is possible in both. Is it

What is the real time advantage of one over the other ?

标签: java oop
9条回答
Ridiculous、
2楼-- · 2020-05-19 04:21

An class may implement several interfaces, whereas it may only extend one class (abstract or concrete), because Java does not support multiple inheritance.

查看更多
Melony?
3楼-- · 2020-05-19 04:22

The main advantages of interface over abstract class is to overcome the occurrence of diamond problem and achieve multiple inheritance. In java there is no solution provided for diamond problem using classes.For this reason multiple inheritance is block using classes in java. So to achieve multiple inheritance we use interface .

查看更多
够拽才男人
4楼-- · 2020-05-19 04:27

Interfaces are for when you want to say "I don't care how you do it, but here's what you need to get done."

Abstract classes are for when you want to say "I know what you should do, and I know how you should do it in some/many of the cases."

Abstract classes have some serious drawbacks. For example:

class House {

}

class Boat {

}

class HouseBoat extends /* Uh oh!! */ {
    // don't get me started on Farmer's Insurance "Autoboathome" which is also a helicopter
}

You can get through via an interface:

interface Liveable {

}

interface Floatable {

}

class HouseBoat implements Liveable, Floatable {

}

Now, abstract classes are also very useful. For example, consider the AbstractCollection class. It defines the default behavior for very common methods to all Collections, like isEmpty() and contains(Object). You can override these behaviors if you want to, but... is the behavior for determining if a collection is empty really likely to change? Typically it's going to be size == 0. (But it can make a big difference! Sometimes size is expensive to calculate, but determining whether something is empty or not is as easy as looking at the first element.)

And since it won't change often, is it really worth the developer's time to implement that method every... single... time... for every method in that "solved" category? Not to mention when you need to make a change to it, you're going to have code duplication and missed bugs if you had to re-implement it everywhere.

查看更多
狗以群分
5楼-- · 2020-05-19 04:29

The facts are-

  1. Java doesn't support multiple inheritance
  2. Multiple interfaces can be implemented
  3. Few methods in an abstract class may be implemented

These facts can be used to tilt the advantage in favor of interfaces or abstract classes.

If there are more than one behavior that a class must share with other classes, interfaces win. If a method definition has to be shared/ overridden with other classes, abstract classes win.

查看更多
你好瞎i
6楼-- · 2020-05-19 04:29

-Method without any implementation is abstract method,whenever a class contains one or more abstract method,then it must be declared as a abstract class

-Interface is fully abstract which cannot have constructor,instance and static blocks,and it contains only two types of members 1.public abstract method 2.public-static-final variable

*Both cannot be instantiated but reference can be created.

*Which one suits better depends on the application -Interfaces are useful because Java classes will not support multiple inheritance but interfaces do.

-Abstract classes are useful when you need concrete behavior from the base class.

查看更多
我只想做你的唯一
7楼-- · 2020-05-19 04:33

In OOP (mostly independent of a concrete language) abstract classes are a re-use mechanism for the class hierarchy for behaviour and structure which isn't complete on its own. Interfaces are mechanism for specification of requirements on a module (e.g. class) independently of the concrete implementation. All other differences are technical details, important is different usage.

查看更多
登录 后发表回答