How to Call the Overridden Method of a Superclass

How to call the overridden method of a superclass?

You cannot do what you want. The way polymorphism works is by doing what you are seeing.

Basically a cat always knows it is a cat and will always behave like a cat regardless of if you treat is as a Cat, Felis, Felinae, Felidae, Feliformia, Carnivora, Theria, Mammalia, Vertebrata, Chordata, Eumetazoa, Animalia, Animal, Object, or anything else :-)

How to call the superclass implementation of an overridden method from inside the superclass in Java?

You could move said method body to a private method and let the default method (the one which may be overridden by the subclass) delegate to the former. See this example

public abstract class SuperClass {

private Object field1;

protected SuperClass(Object obj){
// call the safe implementation
setField1Safe(obj);
}

public void setField1(Object obj){
// just delegates
setField1Safe(obj);
}

private void setField1Safe(Object obj){
// perform some check on obj and
// then set field1 such that field1==obj
}
}

public class SubClass extends SuperClass{
public SubClass(Object obj){
super(obj);
}

@Override
public void setField1(Object obj){
super.setField1(obj);
// do some work that is necessary only when
// field1 is set through SubClass.setField1()
}
}

That way the sub class can still override setField1 but if you really depend on the implementation then you can call the private setField1Safe method.

how do you call a overridden super-class method directly from subclass

... but it calls the subclass method

That's the WHOLE POINT of overriding ... so that when you call the method on an instance the subclass you get the subclasses override of the method!

On the other hand, the subclass itself can call the overridden version of the method in its superclass:

class A {
public void foo() {
System.out.println("super");
}
}

class B extends A {
public void foo() {
super.foo(); // call the overridden method
System.out.println("sub");
}
}

... which will print

super
sub

if you call foo on a B instance.


are you guys really sure that there is no other method to get around this problem ?

  1. Yes
  2. I don't see why you think it is a "problem".

Java: Calling a super method which calls an overridden method

The keyword super doesn't "stick". Every method call is handled individually, so even if you got to SuperClass.method1() by calling super, that doesn't influence any other method call that you might make in the future.

That means there is no direct way to call SuperClass.method2() from SuperClass.method1() without going though SubClass.method2() unless you're working with an actual instance of SuperClass.

You can't even achieve the desired effect using Reflection (see the documentation of java.lang.reflect.Method.invoke(Object, Object...)).

[EDIT] There still seems to be some confusion. Let me try a different explanation.

When you invoke foo(), you actually invoke this.foo(). Java simply lets you omit the this. In the example in the question, the type of this is SubClass.

So when Java executes the code in SuperClass.method1(), it eventually arrives at this.method2();

Using super doesn't change the instance pointed to by this. So the call goes to SubClass.method2() since this is of type SubClass.

Maybe it's easier to understand when you imagine that Java passes this as a hidden first parameter:

public class SuperClass
{
public void method1(SuperClass this)
{
System.out.println("superclass method1");
this.method2(this); // <--- this == mSubClass
}

public void method2(SuperClass this)
{
System.out.println("superclass method2");
}

}

public class SubClass extends SuperClass
{
@Override
public void method1(SubClass this)
{
System.out.println("subclass method1");
super.method1(this);
}

@Override
public void method2(SubClass this)
{
System.out.println("subclass method2");
}
}

public class Demo
{
public static void main(String[] args)
{
SubClass mSubClass = new SubClass();
mSubClass.method1(mSubClass);
}
}

If you follow the call stack, you can see that this never changes, it's always the instance created in main().

Swift calling subclass's overridden method from superclass

Make sure that the object's class is SubclassViewController. Otherwise, it will not have any knowledge of the method which is overriden by subclass

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.

Call overridden method of super class

i think this will fulfil your requirement!! try this
when you call obj2.printHello(); execution will move to Overridings printHello() and in that method i ve used super.printHello(); this means first invoke super class's printHello() and compiler prints Print Hello then execution again come to Overridings printHello() and print NO NO PRINT HI

class OverrideSuper {
public void printHello() {
System.out.println("Print Hello");
}
}
public class Overriding extends OverrideSuper {
public void printHello() {
super.printHello();
System.out.println("NO NO PRINT HI");
}

public static void main(String[] args) {
// OverrideSuper obj1 = new OverrideSuper();
Overriding obj2 = new Overriding();
obj2.printHello();// this calls printHello() of class Overriding.
// I want to call printHello() of OverrideSuper using obj2. How do I do
// that???
}
}

output

Print Hello
NO NO PRINT HI

Calling overridden methods from a vector of superclass pointers

You are describing polymorphism (or a lack thereof in your current implementation).

To make your draw function polymorphic, you must declare it virtual. See below for an example:

class Polygon {
public:
virtual ~Polygon() {}
virtual void draw() = 0;
};

class Rectangle : public Polygon
{
public:
void draw() override { std::cout << "Rectangle::draw()\n"; }
};

class Circle : public Polygon
{
public:
void draw() override { std::cout << "Circle::draw()\n"; }
};

Note three extra things in the above:

  1. I also declared the destructor virtual. This allows for proper destruction of an object through its base class pointer.
  2. I declared the base draw method as pure-virtual (the = 0 part). This means the Polygon class is abstract and cannot itself be instantiated. You may not want that, but to me it seems there's no use for a draw method on a base class anyway. Define one if you want. Up to you.
  3. The override specifier is optional, but recommended (language feature introduced by C++11). It instructs the compiler that you're intentionally overriding a virtual method, and so if no such method exists to be overridden then it will generate a compiler error.


Related Topics



Leave a reply



Submit