In Java, How to Call a Base Class's Method from the Overriding Method in a Derived Class

In Java, how do I call a base class's method from the overriding method in a derived class?

The keyword you're looking for is super. See this guide, for instance.

How to call the overridden method of a derived class (child class)?

JVM invokes your child overridden method by default and the overridden method can decide whether to call the super method or not, that's how inheritance works. But for your overridden method to get called you have to make sure you have overridden it!

In your code you have not declared your onNewMessage method open in the BaseActivity. Hence the compiler assumes the method as final and does not allow overriding it, so replace

fun onNewMessage(msg : String) {
Log.e(TAG, "New Message Parent Method invoked: msg: $msg")
}

with

open fun onNewMessage(msg : String) {
Log.e(TAG, "New Message Parent Method invoked: msg: $msg")
}

So it can be overridden using the same signature.

Overriding a base class method in a derived class

First of all one must be made clear: calling an overridable method from a constructor is a well-known antipattern. It will almost certainly break your code because the subclass method will be invoked before the subclass constructor is done and so will observe an uninitialized object. Thus I should better refrain from giving you detailed advice on Java technicalities involved in achieving this antipattern.

The only safe way to acomplish your requirement is to let the construction finish and only afterwards call an initialize-kind of method. If you want to ensure initialize is always invoked, make the constructors non-public and provide a factory method instead.

Unfortunately, Java requires quite a bit of work on your part to make this work properly.

Calling base class overridden function from base class method

Unfortunately, no

As i'm sure you're aware, but I'll state explicitly for completeness - there are only the 2 keywords to control the method invocation:

  • this - this.method() - looks for method starting from the invoking instance's class (the instance's "top" virtual table - implied default)
  • super - super.method() - looks for method starting from the parent class of the class in which the invoking method is defined (the invoking class' parent's virtual table - not strictly true, but simpler to think of this way - thanks @maaartinus)

I can imagine another keyword (e.g. current?) do what you describe:

  • current - current.method() - looks for method starting from the class in which the invoking method is defined

but Java doesn't have such a keyword (yet?).

Why isn't my derived class method calling the overriden method from base class despite using the super keyword?

1st: You are calling super.introduce() which returns a string but you are not doing anything with that string. You need to assign it to a variable and add it to your return statement for it to be visible.

2nd: I recommend you change the introduce() method to toString() since that way you can get the string by just writing:

System.out.println(superhero);

Here is what you need to do to return the "Hey! I'm " + name + " and I'm " + age + " years old." part as well:

@Override
public String toString(){
return super.introduce() + "\n" + "I am also known as" + alterEgo + "!";
}

Personally i prefer implementing my toString() methods like this.

@Override
public String toString(){
String string = super.toString();
string = string + "\n";
string = string + "I am also known as";
string = string + alterEgo;
string = string + "!";
return string;
}

How do I call an overridden parent class method from a child class?

call super

class A {
int foo () { return 2; }
}

class B extends A {

boolean someCondition;

public B(boolean b) { someCondition = b; }

int foo () {
if(someCondition) return super.foo();
return 3;
}
}

How to call a base class method without changing all derived classes

public class Base {
public final void execute() {
doExecute();
someMethod();
}

protected abstract void doExecute();

public void someMethod() {
}
}

This solution prevents the super code smell.

State of Derived class object when Base class constructor calls overridden method in Java

  • The Derived object has been created - it's just that the constructor hasn't been run yet. The type of an object never changes in Java after the instant it is created, which happens before all constructors run.

  • var is assigned the default value of 0 as part of the process of creating an object, before constructors are run. Basically, the type reference gets set and the rest of the memory representing the object gets wiped to zero (conceptually, anyway - it may already have been wiped to zero before, as part of garbage collection)

  • This behaviour at least leads to consistency, but it can be a pain. In terms of consistency, suppose you had a read-only subclass of a mutable base class. The base class may have an isMutable() property which was effectively defaulted to true - but the subclass overrode it to always return false. It would be odd for the object to be mutable before the subclass constructor ran, but immutable afterwards. On the other hand, it's definitely strange in situations where you end up running code in a class before the constructor for that class has run :(

A few guidelines:

  • Try not to do much work in a constructor. One way of avoiding this is to do work in a static method, and then make the final part of the static method a constructor call which simply sets fields. Of course, this means you won't get the benefits of polymorphism while you're doing the work - but doing so in a constructor call would be dangerous anyway.

  • Try very hard to avoid calls to non-final methods during a constructor - it's very likely to cause confusion. Document any method calls you really have to make very clearly, so that anyone overriding them knows that they will be called before initialization has finished.

  • If you have to call a method during construction, it's usually not then appropriate to call it afterwards. If that's the case, document it and attempt to indicate it in the name.

  • Try not to overuse inheritance in the first place - this is only going to become an issue when you've got a subclass deriving from a superclass other than Object :) Designing for inheritance is tricky.



Related Topics



Leave a reply



Submit