Calling an Overridden Method from a Parent Class Ctor

Is it possible to call an overridden method in an instance of child class ES 2015?

It's JavaScript, so you can. There's no classes anyway, just prototypal inheritance with some syntactic sugar over it.

Which one do you want to call: Car's emitsCarbon specifically? That'll be

Car.prototype.emitsCarbon.call(car1);

Your object's class' direct ancestor class' emitsCarbon in general?

car1.__proto__.__proto__.emitsCarbon.call(car1);

(You're not supposed to directly access __proto__, but there's nothing actually stopping you besides the general good practice and guidelines).

Also, hybrids do emit carbon. Just a side note.

Calling an overridden method from a constructor

I think what is happening here is that the super constructor is calling the child's show() method because this method was overriden in Child.

That is correct

but why is the value of x 0

because it's not initialized yet (x of Child)

and why is it able to access this method before the super constructor has completed?

That's exactly why in a constructor you should never call a method, which can be overridden (non-final public and protected).

Edit:

The strange thing here is that everything has default/ package-private visibility. This can have some strange effects. See: http://www.cooljeff.co.uk/2009/05/03/the-subtleties-of-overriding-package-private-methods/

I recommend to avoid overriding methods with default visibility if possible (you can prevent this by declaring them final).

Call an overridden method in base class constructor in typescript

The order of execution is:

  1. A's constructor
  2. B's constructor

The assignment occurs in B's constructor after A's constructor—_super—has been called:

function B() {
_super.apply(this, arguments); // MyvirtualMethod called in here
this.testString = "Test String"; // testString assigned here
}

So the following happens:

var b = new B();     // undefined
b.MyvirtualMethod(); // "Test String"

You will need to change your code to deal with this. For example, by calling this.MyvirtualMethod() in B's constructor, by creating a factory method to create the object and then execute the function, or by passing the string into A's constructor and working that out somehow... there's lots of possibilities.

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.

Calling an overridden method from base constructor

It's not simple to provide a language agnostic answer to this, because "what happens" depends on the language itself. In Java, for instance, the overridden (virtual) method will be called, but this can present its own problems, and is thus not recommended.

You need to consult the documentation or language specification you're interested in, and then read around to see if anyone's published an opinion on why you should or shouldn't do it, and what might go wrong, such as the Scott Meyers piece that AraK links to in his/her comment.

Calling child's overridden method from parent class

If you're coming from java, you need to remember that every non-primitive variable or field in java is implicitly a pointer, so to make equivalent C++ code, you need to make all interclass references into pointers.

In addition, every method in java is implicitly virtual, so if you want to override them, you need an explicit virtual in C++.

So you end up with something like:

#include <iostream>

class System {
public:

virtual void start() {
std::cout << "Booting System..." << std::endl;
}
};

class MyOS: public System {
public:

// Override
void start() override {
std::cout << "Booting MyOS..." << std::endl;
}
};

class Phone {
public:

Phone(System *system) {
this->system = system;
}

void start() {
system->start();
}

private:

System *system;
};

int main() {
MyOS os;
Phone myPhone(&os);
myPhone.start();

return 0;
}

Of course, using raw pointers is a recipe for memory leaks and corruption, as C++ does not have built in garbage collection. So you generally want to use "smart" pointers instead -- either std::unique_ptr or std::shared_ptr

Overridden Function within Child Class Constructor (JAVA)

The superclass constructor is executed first. So when the overridden method is called, the child constructor hasn't been executed yet, so id field in the subclass still has its default value.

That's why calling overridable methods from a constructor is a bad practice, flagged by tools such as PMD: the invariants of the objects are not fulfilled when such a method is called.

Overridden method call in parent method

Any class in an inheritance hierarchy of Parent, be it a Child, a Parent, or some Grandchild deriving from them, has exactly one implementation of the method called isMeh. When Child overrides isMeh of Parent with its own implementation, it replaces the implementation; as far as the Child is concerned, the implementation in the Parent no longer applies*.

When you instantiate Child and call its parentMethod, the method has access to only one method isMeh - that is, the implementation provided by the Child. That is why you get the behavior that you describe.

This behavior allows for very nice patterns: for example, you can write a "partial implementation" of a method in the parent by relying on "plug-in" functionality in the child class provided through overrides. This technique is known as Template Method Design Pattern.

* although Child retains an ability to call Parent's isMeh explicitly.



Related Topics



Leave a reply



Submit