当我需要在 Java 中使用抽象类和接口时?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3866264/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
When i need to use abstract class and interface in Java?
提问by Velmurugan
Possible Duplicate:
Abstract class and Interface class?
可能的重复:
抽象类和接口类?
When i need to use abstract class and interface in Java?
当我需要在 Java 中使用抽象类和接口时?
My doubt is
我的疑问是
- Which situation i need to use abstract class and which situation i need to use interface.
- interface satisfy the abstract class properties. so then why we need especially abstract class?
- i know, that abstract class contains abstract methods and non abstract methods, but we can use abstract class as a ordinary class, then the result will be same in the both classes. the ordinary class also inherited same as abstract class. So why we need abstract class.
- 哪种情况需要使用抽象类,哪种情况需要使用接口。
- 接口满足抽象类属性。那么为什么我们需要特别抽象的类呢?
- 我知道,抽象类包含抽象方法和非抽象方法,但是我们可以将抽象类用作普通类,那么两个类的结果将相同。普通类也继承了抽象类。那么为什么我们需要抽象类。
If anybody know the good example please reply me.
如果有人知道好的例子,请回复我。
Thanks.
谢谢。
回答by Eugene Kuleshov
Another important aspect of abstract class is that unlike interface, adding new methods to it won't break binary compatibility. So, from the API evolution point of view, especially when you can expect additions to the public API, abstract classes are more preferable.
抽象类的另一个重要方面是与接口不同,向其添加新方法不会破坏二进制兼容性。因此,从 API 演变的角度来看,尤其是当您可以期待公共 API 的添加时,抽象类更可取。
回答by Faisal Feroz
I would say that there are two types of inheritance. One I would say as Implementation Inheritance and other as Contract Inheritance.
我会说有两种类型的继承。我会说一种是实现继承,另一种是契约继承。
Abstract classes are used for having Implementation Inheritance. You can extend/change the behavior of your super/parent class.
抽象类用于具有实现继承。您可以扩展/更改超类/父类的行为。
Interfaces would go for Contract Inheritance. Where you are more interested in having the class implement some kind of a contract (service methods with arguments - more of a contract) and the behavior is different for different implementation, nothing generic that you can bundle up in an abstract class and extend the behavior.
接口将用于合同继承。您对让类实现某种契约(带参数的服务方法 - 更多的是契约)更感兴趣并且不同实现的行为不同的地方,没有任何通用的东西可以捆绑在抽象类中并扩展行为.
回答by Victor Ionescu
You need to use abstract classes if you want to apply the template pattern http://en.wikipedia.org/wiki/Template_method_pattern, usually in framework code. As a rule of thumb, if you're not intending to implement the template pattern, you're better off with interfaces, which permit loose coupling, the way spring framework does. Loose coupling leads to a design open to evolutions and a better testability, with techniques like mock objects (http://easymock.org)
如果要应用模板模式http://en.wikipedia.org/wiki/Template_method_pattern,则需要使用抽象类,通常在框架代码中。根据经验,如果您不打算实现模板模式,最好使用接口,它允许松散耦合,就像 spring 框架所做的那样。松散耦合导致设计对进化开放并具有更好的可测试性,使用模拟对象 (http://easymock.org) 等技术