How do I call a base class's own method from another method when it's overridden?

Saieden
public class Parent{

    private Object oBase;

    public Object getObject(){

        // [some logic] 
        return oBase;
    }

    public String getObjectValue(){

        return getObject().getValue();
    }



public class Child extends Parent {

    private Object oChild;

    public Object getObject(){

        return oChild;
    }


    public Object getObjectValue(){

        return getObject().getValue();
    }

    public String getParentObjectValue(){

        return super.getObjectValue();
    }
}

In the above template, I need a way to make sure that the getObject() in Parent.getObjectValue() calls Parent.getObject() and not Child.getObject(), so that the child class can have getters to oBase and oChild.

I've tried using ((Parent)this).getObject().getValue() in Parent.getObjectValue(), but it still polymorphs to the child definition. Is there way to force static binding in java for a specific call?

I'm trying to avoid duplicating the [some logic] from Parent.getObject() in Parent.getObjectValue(), but I guess I'll have to if there's really no way around it.

aquaraga

You can either make the getObject() method private, or change the method name so that polymorphism will not kick on. Overall, you'll have to rethink your design.

Other options are to extract the [some logic] to a third method and invoke this from both getObject() and getObjectValue(). Please keep in mind the Command Query separation principle when you use this design.

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 mock a base class's method when it was overridden?

From Dev

If a function (that's outside of the class) needs to use a method from a class, how do I call the method?

From Dev

In Java, how do I call the method of a overridden class from an instance of the overriding class

From Dev

How do I call a method from another class in Java?

From Dev

How do I call actionPerformed method from another class in java

From Dev

How to call or refer to an instance attribute from another's class method

From Java

Why do I get a BootstrapMethodError when trying to call a super class's protected method using method reference from an inner class?

From Dev

Why do I get a BootstrapMethodError when trying to call a super class's protected method using method reference from an inner class?

From Dev

How do I call a base class method from within the same overloaded derived class method in python?

From Dev

Can't call base class's method when another overload present in derived class

From Dev

How do I override a method when the compiler says it's "not found in base class"?

From Dev

In Typescript how do I call a class method from another method in the same class that is called as an event handler

From Dev

How to break overridden method if it's base method fails?

From Dev

Why do I have to call overridden to_s method explicitly and not implicitly as I would expect with puts

From Dev

How to call an Object's Method from another class without creating a sub-class and/or inheriting class?

From Dev

iOS: What is happening when I call a overridden method from other class

From Dev

How do you call a method in one class, from another class?

From Dev

How do you call a method in one class, from another class?

From Dev

How can I run testNG through a call from another method, let's say a main method?

From Dev

How do I call a derived class method from the base class constructor in C++?

From Dev

How can I make a method takes the value(s) from another method from within the same class in Python

From Java

How to call Base Class's __init__ method from the child class?

From Dev

How do I call a classes method from another class without initialising the first class?

From Dev

How do I call a method on a specific base class in Python?

From Dev

Perl: How to make sure overridden method is called when accessed from within the base class

From Dev

Calling an overridden method from the base class in Ada

From Java

Call an overridden method from super class in typescript

From Dev

Another class's method call Lua

From Dev

How do you call a method inherited from another class?

Related Related

  1. 1

    How to mock a base class's method when it was overridden?

  2. 2

    If a function (that's outside of the class) needs to use a method from a class, how do I call the method?

  3. 3

    In Java, how do I call the method of a overridden class from an instance of the overriding class

  4. 4

    How do I call a method from another class in Java?

  5. 5

    How do I call actionPerformed method from another class in java

  6. 6

    How to call or refer to an instance attribute from another's class method

  7. 7

    Why do I get a BootstrapMethodError when trying to call a super class's protected method using method reference from an inner class?

  8. 8

    Why do I get a BootstrapMethodError when trying to call a super class's protected method using method reference from an inner class?

  9. 9

    How do I call a base class method from within the same overloaded derived class method in python?

  10. 10

    Can't call base class's method when another overload present in derived class

  11. 11

    How do I override a method when the compiler says it's "not found in base class"?

  12. 12

    In Typescript how do I call a class method from another method in the same class that is called as an event handler

  13. 13

    How to break overridden method if it's base method fails?

  14. 14

    Why do I have to call overridden to_s method explicitly and not implicitly as I would expect with puts

  15. 15

    How to call an Object's Method from another class without creating a sub-class and/or inheriting class?

  16. 16

    iOS: What is happening when I call a overridden method from other class

  17. 17

    How do you call a method in one class, from another class?

  18. 18

    How do you call a method in one class, from another class?

  19. 19

    How can I run testNG through a call from another method, let's say a main method?

  20. 20

    How do I call a derived class method from the base class constructor in C++?

  21. 21

    How can I make a method takes the value(s) from another method from within the same class in Python

  22. 22

    How to call Base Class's __init__ method from the child class?

  23. 23

    How do I call a classes method from another class without initialising the first class?

  24. 24

    How do I call a method on a specific base class in Python?

  25. 25

    Perl: How to make sure overridden method is called when accessed from within the base class

  26. 26

    Calling an overridden method from the base class in Ada

  27. 27

    Call an overridden method from super class in typescript

  28. 28

    Another class's method call Lua

  29. 29

    How do you call a method inherited from another class?

HotTag

Archive