How to call abstract method from abstract class called by inherit class

Amadeus

I want to call a method of an abstract class from abstract class called by inherit class.

Abstract class:

public abstract class Abstract {
    protected void updateMotionY(float deltaTime) {
        System.out.println("Abstrcat updateMotionY");
    }
    public void update(float deltaTime) {
        this.updateMotionY(deltaTime);
    }
}

Inherit class:

public class Obj extends Abstract {
    @Override
    protected void updateMotionY(float deltaTime) {
        System.out.println("updateMotionY");
        super.updateMotionY(deltaTime);
    }
    @Override
    public void update(float deltaTime) {
        super.update(deltaTime);
    }
}

Main method class:

public static void main(String[] args) {
    (new Obj()).update(10.0f);
}

Whenever I try to call new Obj().update() method in main class, it prints "updateMotionY" and "Abstrcat updateMotionY". I want to get only "Abstrcat updateMotionY".

Can anyone tell me how to resolve this problem?

Michael

(new Obj()).update(10.0f) calls Obj::update which calls Abstract::update which calls this.updateMotionY. Because this is an instance of Obj, this calls Obj::updateMotionY.

This prints "updateMotionY".

This then calls super.updateMotionY(deltaTime) which is Abstract::updateMotionY.

This prints "Abstrcat updateMotionY".

That's the end of the call hierarchy and everything unwinds.


Fundamentally your confusion seems to stem from the fact that this.updateMotionY(deltaTime); in the Abstract class resolves to updateMotionY in the Obj class. That's basically the whole point of polymorphism.


One thing you could do is to add a private method (so that it cant be overridden) which contains the actual implementation, and defer to it:

public abstract class Abstract {
    private void motionY(float dt)
    {
        System.out.println("Abstrcat updateMotionY");
    }

    protected void updateMotionY(float deltaTime) {
        motionY(deltaTime);
    }
    public void update(float deltaTime) {
        motionY(deltaTime);
    }
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How to call non abstract method in a abstract class?

From Dev

How to make an Abstract Class inherit from another Abstract Class in Python?

From Dev

How to call a method in an abstract class

From Dev

How to call abstract class method

From Java

Inherit annotations from abstract class?

From Dev

How to inherit abstract class with PyCharm

From Dev

Unable to call abstract class method from interface

From Dev

Call a method from abstract class c#

From Dev

How to call an abstract method from a Class parameter in Kotlin?

From Dev

How to test an object class inherit from a certain abstract class

From Dev

How to call a method in an abstract class properly

From Java

Making an abstract class inherit from another abstract class in python

From Dev

Error "__init__ method from base class is not called" for an abstract class

From Dev

Node.js: how to 'inherit' from abstract class?

From Dev

How to register classes that inherit from an abstract class in python

From Dev

How to inherit from an abstract class properly in C++?

From Dev

Inherit INotifyPropertyChanged for Static Event from abstract Class

From Java

How to ensure a certain methods gets called in abstract super-class from method in sub-class (Java)

From Java

static method from abstract class

From Dev

Referencing method from abstract class

From Dev

How to call the constructor of an Abstract class from another constructor inside the same class (method overloading)

From Java

How do I call a childs class method from a parent abstract class

From Dev

Why can we call abstract method from an abstract class before sub-class initialization

From Dev

Abstract method in a non abstract class

From Dev

Abstract Method In Abstract Class In java

From Dev

Call again abstract inherited method from another class

From Dev

Call no-virtual subclass method from a pointer base abstract class

From Dev

How to instantiate class from static method of abstract class

From Dev

How to create an instance of concrete class from static method of abstract class

Related Related

  1. 1

    How to call non abstract method in a abstract class?

  2. 2

    How to make an Abstract Class inherit from another Abstract Class in Python?

  3. 3

    How to call a method in an abstract class

  4. 4

    How to call abstract class method

  5. 5

    Inherit annotations from abstract class?

  6. 6

    How to inherit abstract class with PyCharm

  7. 7

    Unable to call abstract class method from interface

  8. 8

    Call a method from abstract class c#

  9. 9

    How to call an abstract method from a Class parameter in Kotlin?

  10. 10

    How to test an object class inherit from a certain abstract class

  11. 11

    How to call a method in an abstract class properly

  12. 12

    Making an abstract class inherit from another abstract class in python

  13. 13

    Error "__init__ method from base class is not called" for an abstract class

  14. 14

    Node.js: how to 'inherit' from abstract class?

  15. 15

    How to register classes that inherit from an abstract class in python

  16. 16

    How to inherit from an abstract class properly in C++?

  17. 17

    Inherit INotifyPropertyChanged for Static Event from abstract Class

  18. 18

    How to ensure a certain methods gets called in abstract super-class from method in sub-class (Java)

  19. 19

    static method from abstract class

  20. 20

    Referencing method from abstract class

  21. 21

    How to call the constructor of an Abstract class from another constructor inside the same class (method overloading)

  22. 22

    How do I call a childs class method from a parent abstract class

  23. 23

    Why can we call abstract method from an abstract class before sub-class initialization

  24. 24

    Abstract method in a non abstract class

  25. 25

    Abstract Method In Abstract Class In java

  26. 26

    Call again abstract inherited method from another class

  27. 27

    Call no-virtual subclass method from a pointer base abstract class

  28. 28

    How to instantiate class from static method of abstract class

  29. 29

    How to create an instance of concrete class from static method of abstract class

HotTag

Archive