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?).
Calling an overridden method from the base class
The A::foo
will call B::bar
if you have an instance of B
. It does not matter if the instance is referenced through a pointer or a reference to a base class: regardless of this, B
's version is called; this is what makes polymorphic calls possible. The behavior is not compiler-specific: virtual functions behave this way according to the standard.
Calling an overridden function from a base class?
You should not call a virtual method from a constructor because the object has not yet been fully constructed. Essentially, the derived class doesn't exist yet, so its methods cannot be called.
Calling an overridden function from a list of its base class?
You need to use a list of Base*
(that is, pointers-to-Base), or preferably, std::unique_ptr<Base>
or std::shared_ptr<Base>
.
The reason for this is because of the C++ object model and copying. Derived classes must be at least as large as their base class (they can be the same size depending on the derived class being empty or not). Since C++ utilizes copying (or possibly moving in C++11) when adding items to a vector
, it must allocate enough space for n Base
objects. Since a vector
is almost always a wrapper around a simple array
, trying to add a (possibly differently sized) Derived
object into an array
of Base
objects is undefined behaviour.
Calling an overridden method from the base class in Ada
In Ada, dispatching only happens when the object is of a class-wide type. The relevant manual section is ARM 3.9.2.
In Parents.Prints
, the controlling operand Self
is of type Parent
, and is “statically tagged”, so there is no dispatching.
One approach is to use “redispatching”, which looks like this:
procedure Prints(Self: in out Parent) is
begin
Put_Line("Parents.Prints: calling prints...");
Parent'Class (Self).Print1;
Parent'Class (Self).Print2;
end;
in which the view conversion Parent'Class (Self)
means that the object on which .Print1
is invoked is "dynamically tagged" and the call dispatches.
As you have it, Prints
is able to be overridden in derived types. This isn’t always (or even usually?) what you want. If not, it’d be appropriate to change its parameter to be class-wide:
procedure Prints(Self: in out Parent'Class);
(and in the body, of course!) and then everything works as you’d expected.
[A side note: I’ve now learned that the object.operation notation works with class-wide objects!]
How do overridden method calls from base-class methods work?
Here's the example you requested. This prints chocolate
.
class Base:
def foo(self):
print("foo")
def bar(self):
self.foo()
class Derived(Base):
def foo(self):
print("chocolate")
d = Derived()
d.bar() # prints "chocolate"
The string chocolate
is printed instead of foo
because Derived
overrides the foo()
function. Even though bar()
is defined in Base
, it ends up calling the Derived
implementation of foo()
instead of the Base
implementation.
Calling the overridden method from the base class in C#
Which method is called is determined via polymorphism on the type that is passed into the AnotherObject constructor:
AnotherObject a = new AnotherObject(new A()); // invokes A.MyMethod()
AnotherObject b = new AnotherObject(new B()); // invokes B.MyMethod()
AnotherObject c = new AnotherObject(new BaseClass()); //invokes BaseClass.MyMethod()
calling an overridden method from base class?
Yes, you're missing this:
b.bar() # echoes B.foo
B
has no bar
method of its own, just the one inherited from A
. A
's bar
calls self.foo
, but in an instance of B
ends up calling B
's foo
, and not A
's foo
.
Let's look at your quote again:
a method of a base class that calls
another method defined in the same
base class, may in fact end up calling
a method of a derived class that
overrides it
To translate:
bar
(method ofA
, the base class)
callsself.foo
, but may in fact end up
calling a method of the derived class
that overrides it (B.foo
that
overridesA.foo
)
Call an overridden method in base class constructor in typescript
The order of execution is:
A
's constructorB
'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.
Related Topics
How Are Servlet Url Mappings in Web.Xml Used
Java - Regular Expression Finding Comments in Code
Why Doesn't Java.Util.Collection Implement the New Stream Interface
Adding New Paths for Native Libraries at Runtime in Java
Functional Interface That Takes Nothing and Returns Nothing
Production Settings File for Log4J
How to Get the Array Class for a Given Class in Java
How to Implement Task Prioritization Using an Executorservice in Java 5
Managing Dll Dependencies with Maven
Why Should I Use Concurrent Characteristic in Parallel Stream with Collect
Detect If Java Application Was Run as a Windows Admin
Understanding Scanner's Nextline(), Next(), and Nextint() Methods
Parsing String to Local Date Doesn't Use Desired Century
Which Is More Effective: If (Null == Variable) or If (Variable == Null)