Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it good practice to override non abstract methods?

I have a situation where I need to modify the super class method to have a subclass specific logic, but the methods logic is same for all other subclasses.

I have two options:

1) make the method abstract and for each except my concerned subclass repeat the same code.

2) Override the non-abstract method in the concerned subclass where i want to have alter logic.

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

like image 297
Learner Avatar asked Jan 06 '18 06:01

Learner


People also ask

Can you override a non-abstract method?

Subclasses of an abstract class must implement (override) all abstract methods of its abstract superclass. The non-abstract methods of the superclass are just inherited as they are. They can also be overridden, if needed.

Can we override non-abstract methods of abstract class?

To implement features of an abstract class, we inherit subclasses from it and create objects of the subclass. A subclass must override all abstract methods of an abstract class. However, if the subclass is declared abstract, it's not mandatory to override abstract methods.

Can we override abstract methods?

An abstract method is a method that is declared, but contains no implementation. you can override both abstract and normal methods inside an abstract class. only methods declared as final cannot be overridden.

Is it mandatory to override abstract method in derived class?

It is necessary to override all the methods which are declared as abstract . you cannot skip the non-concrete methods. If you really want to do then make your class as abstract.


1 Answers

To a certain degree this is a matter of style.

It is a common practice - but there are also people that tell you that any method should not have more than one implementation. These people claim that multiple implementations within an inheritance hierarchy lead to code that is hard to debug - because you have to be extremely careful to determine which version of such a method is actually called.

And when such methods are used heavily by other methods you can easily loose the big picture - all of a sudden it becomes complicated to anticipate what some code is doing - because of heavy overriding in some subclasses.

The key thing to understand: a "single" @Override for some method foo() in class X is fine, and common, good practice. But overriding the same foo() again in subclassses of X - that can quickly lead to all kinds of problems.

In other words: re-implementing non-abstract methods should be done carefully. If it makes your code harder to understand then look for other solutions. Like: the base class having a fixed (final) method doing things - and that 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 written: here you can see that the implementation is "fixed" because doSomething() is final - but the required "ingredients" can (must) be overridden in each subclass.

like image 94
GhostCat Avatar answered Oct 05 '22 00:10

GhostCat