Is it good practice to override non-abstract methods?

I have a situation where I need to change the method of a superclass to have the logic of a subclass, but the logic of the methods is the same for all other subclasses.

I have two options:

1) make the method abstract and repeat the same code for everyone except my respective subclass.

2) Override a non-abstract method in the appropriate subclass where I want to change the logic.

Is it good practice in Java to override a non-abstract method? and what will be the difference conceptually b / w, redefining non-abstract and abstract methods.

+7
java override inheritance oop abstract-methods
source share
4 answers

To a certain extent, it is a matter of style.

This is common practice , but there are people who tell you that any method should not have more than one implementation. These people claim that several implementations in the inheritance hierarchy lead to complex code debugging, because you have to be extremely careful to determine which version of such a method is actually called.

And when such methods are used mainly in other ways, you can easily lose the big picture — it suddenly becomes difficult to foresee what some code is doing — due to heavy overriding in some subclasses.

Key value for understanding: the "single" @Override for some foo() method in class X is great and common, good practice. But overriding the same foo() again in subclasses of X - this can quickly lead to all the problems.

In other words: the re-introduction of non-abstract methods must be done carefully. If this makes it difficult to understand the code, then look for other solutions. For example: a base class having a fixed (finite) method that does things - and this method calls other abstract methods to do its job. Example:

 public abstract class Base { public final int doSomething() { String tmp = foo(); int result = bar(tmp); return result * result; } public abstract String foo(); public abstract int bar(String str); 

As it is written: here you can see that the implementation is "fixed" because doSomething() is final, but the required "ingredients" can (should) be redefined in each subclass.

+6
source share

Is it good practice in Java to override a non-abstract method?

Yes. (But make sure that you do not violate the Liskov Substitution Principle (referring to an existing SO post), which states that Child classes should not violate the definitions of the type of the parent class. For example, the walk () method of the override from the Animal class should execute (have behavior) walking, he should not perform a flight or anything else.)

I also recommend going through the SOLID Principle to understand the design principle.

and what will be the difference conceptually b / w redefinition of not abstract and abstract methods.

There is no difference AFAIK. But, to be clear, the abstract method does not contain any implementation, and you MUST override where you can override it as a non-abstract method.

+5
source share

Override a non-abstract method in the appropriate subclass where I want to change the logic - Yes

For example: If you consider toString () and hashCode () in the Object class, both methods are abstract methods, but in most cases it is recommended to override these methods in our subclasses.

Because, if I want to fulfill my logic, and not superclassical logic, I need to redefine and use it, it is recommended to redefine it in such situations.

+1
source share

Redefining a non-abstract method is fine and is usually practiced until you violate the Liskov subscription principle :

The Liskov principle imposes some standard requirements on signatures

  • Contravariance of method arguments in a subtype.
  • The covariance of return types in a subtype.
  • No new exceptions should be selected by subtype methods, unless those exceptions are themselves subtypes of exceptions created by supertype methods.

In addition to the signature requirements, the subtype must meet a number of behavioral conditions.

  • Preconditions cannot be strengthened in a subtype.
  • Postconditions cannot be relaxed in a subtype.
  • Supertype invariants must be stored in the subtype.
  • The restriction of history ("rule of history"). Objects are considered modifiable only through their methods (encapsulation). Because subtypes can introduce methods that are not in the supertype, introducing these methods can allow state changes in the subtype that are not valid in the supertype. A story restriction prohibits this.

As long as the method still honors the contract after overriding, there should be no confusion as to what the new implementation does. The new implementation method must still adhere to the same contract as the implementation before it has been redefined.

+1
source share

All Articles