当前位置: 动力学知识库 > 问答 > 编程问答 >

Coding style when using abstract classes in Java

问题描述:

When inheriting from a base class in a scenario when not all methods will be implemented, is it better to put empty methods in the base class so that sub-classes that don't require that method can ignore it totally, while other classes must override the method if they want to implement it... e.g:

Base class:

public void myMethod() {

}

Sub-class that doesn't implement:

<nothing!>

Or is it better to leave the base class cleaner and just put an abstract method in and force the sub-class to flesh out a blank method if it doesn't implement that method?

Base class:

public abstract void myMethod();

Sub-class that doesn't implement:

public void myMethod() {

}

网友答案:

It's up to you and it really depends on the situation.

You can use abstract methods when you have an abstract class and you want classes which extend it to implement that method (because the abstract parent class uses the abstract method - it may be something like print()). It's similar to interface's methods but it's usually used in different scenarios. But I would use interface in most cases...

I would use abstract method only in case that myMethod() does a different thing in each class that extends the abstract parent... Otherwise, if myMethod() does usually the same thing and one or two classes need to override it, I will use the first solution.

Also look at the template method pattern. I don't know which case is yours so I can't answer this question in an exact way...

网友答案:

It is your design decision . If you want to force your developers to override myMethod and developed the logic. You should go for abstract class.

网友答案:

Both are not the most elegant solutions, though both can get the job done. Use the design pattern strategy design pattern http://www.newthinktank.com/2012/08/strategy-design-pattern-tutorial/

网友答案:

Coding style is up to you, depends on your requirement and everything has it own pros and cons.

In case of abstract class, it is not necessary to put only abstract method. I would recommend you, better to use Interface.

网友答案:

Whenever it is possible you should

Prefer interfaces to abstract classes

Because interfaces do not permit to contain method implementations, there is the so called Abstract*Interface*, which is a combination of both technics: In that case the Interface defines the type, while the abstract class provides a skeletal implementation.
An example are the Collection Framework which provides skeletal implemantations: AbstractCollection, AbstractList, AbstractSet and AbstractMap.

Further info see Josh Bloch, Effective Java 2nd Edition, Item 18

网友答案:

I think it comes down to whether there is a meaningful default implementation for myMethod(). If there is, put it in the base class and subclasses only override if they need something different.

If there is no meaningful default, and in practice every non-abstract subclass should either implement the method itself or inherit an implementation from an intermediate class, it is a very bad idea to provide a fake implementation in the base class. It converts an error the compiler could have detected to one that can only be found by testing.

One option to consider in some cases is providing a default implementation that throws UnsupportedOperationException.

网友答案:

The differenct in an abstract class is that you can but must not override that function.

So public void nothing() {} can be overridden and public abstract void nothing2(); must be overriden.

分享给朋友:
您可能感兴趣的文章:
随机阅读: