-2

In the following example, the call FirstChildclass first; first.someMethod(); will do the same as SecondChildclass second; second.someMethod();. Or am I wrong?

class Superclass {
   public void someMethod() {
      //do something
   }
}

class FirstChildclass extends Superclass {

   @Override
   public void someMethod() {
      super.someMethod();
   }

   public void someOtherMethod() {
      //do something else
   }
}

class SecondChildclass extends Superclass {

   public void someOtherMethod() {
      //do something else
   }
}

Is there a reason why one will implement it like in FirstChildclass? Because I have seen many implementations like in FistChildclass and am wondering why anyone would do it.

emi-le
  • 756
  • 9
  • 26

2 Answers2

3

If you ask about the difference between:

@Override
public returnType someName() {
    return super.someName();
}

and not overriding it. There is no difference as well as there is no sense in doing it.

You should call super.someName() just in case you want to extend the original method.

Watch out! If you have a constructor you want to use in a child class, but you don't need any additional behavior, you'll call super(arguments).

xenteros
  • 15,586
  • 12
  • 56
  • 91
2

This is about your design. Override means you wanna change the already existing implementation which is inherited from the parent. If you are not doing anything different then you don;t want to do the

@Override
public void someMethod() {
      super.someMethod();
}

That is useless.

@Override
public void someMethod() {
   // do some other logic
   super.someMethod();
   // do some other logic
}

This is perfectly ok :) Because you are doing some tasks other than the parent's implementation.