开发者

Using abstract classes and Interfaces

开发者 https://www.devze.com 2022-12-21 07:12 出处:网络
If I have three classes class A class B extends A class C 开发者_开发问答extends A Would using abstract classes or interfaces give me any type of errors or affect the way I implement the program?

If I have three classes

class A

class B extends A

class C 开发者_开发问答extends A

Would using abstract classes or interfaces give me any type of errors or affect the way I implement the program?


Unless you need to share implementation, favor interfaces.

If you need to share implementation, think carefully if an abstract class is the right way to accomplish this.

Inheritance is a powerful tool, but can easily create an unmaintainable mess (I have been guilty here).

But to answer your question - no, there is nothing inherent in this setup that would cause an "error".


You should generally prefer interfaces whenever you can. Java's lack of multiple inheritance quickly becomes a limiting factor if you use classes for that.


Yes. Abstract classes trade a shorter way of reusing implementation for the flexibility to provide multiple interfaces to the same object.

Unless you're playing code-golf, choosing the flexibility of interfaces is usually the better option.

0

精彩评论

暂无评论...
验证码 换一张
取 消