2

We have found a GWT bug in the Tree class (fixed by Google but in a newer release). The function that needs a fix is a private function. I am wondering if there is any way at all to override the private function. It can be hacky if need be, but if there is a way, please do let me know.

I can probably try to figure out how to change GWT code and re-compile as another option, but it would be really much more easier if I could derive a class ("MyTree") and override this functionality somehow.

ashish d
  • 35
  • 4
  • 3
    If it's private, it was meant not to be overridden. The short answer is: don't try do this. Find another way to achieve your desired functionality. – Hovercraft Full Of Eels Nov 08 '12 at 04:46
  • As far as I understand, there is no way to override it to be applied to the base class as well. If you have source code you can make it protected or public in the base class. – Nikolay Kuznetsov Nov 08 '12 at 04:47
  • These two links might be useful to read http://stackoverflow.com/questions/1346208/confusing-override-a-private-method http://stackoverflow.com/questions/2000137/overriding-private-methods-in-java – Nikolay Kuznetsov Nov 08 '12 at 04:48
  • Possible duplicate of [Can I override a private method in Java?](http://stackoverflow.com/questions/10623913/can-i-override-a-private-method-in-java) – Philipp Wendler Apr 29 '17 at 11:46

6 Answers6

5

Private functions cannot be overriden. It's basically like they don't exist to subtypes.

They also cannot be overriden in the sense of a super class dispatching to a subclasses override.

A concrete example might prove useful:

public class Test
{

    public static void main(String[] args)
    {
        (new B()).privateGo();
        (new B()).protectedGo();
        (new C()).privateGo();
    }

}


class A
{
    public void privateGo()
    {
        _privateGo();
    }

    public void protectedGo()
    {
        _protectedGo();
    }

    private void _privateGo()
    {
        System.out.println("A._privateGo");
    }

    protected void _protectedGo()
    {
        System.out.println("A._protectedGo");
    }

}

class B extends A
{

    private void _privateGo()
    {
        System.out.println("B._privateGo");
    }

    protected void _protectedGo()
    {
        System.out.println("B._protectedGo");
    }

}

class C extends A
{
    public void privateGo()
    {
        _privateGo();
    }
    private void _privateGo()
    {
        System.out.println("C._privateGo");
    }
}

The output will be:

A._privateGo
B._protectedGo
C._privateGo

Protected methods will be dispatched to subclass implementations. Private methods will not. In this sense, it's like private methods do not exist to sub types.

You will have to either update to a fixed version of GWT or edit the source. Note how C's privateGo will dispatch to C's _privateGo. This could be used as a hacky work around, but the problem would be that you'd have to make sure you always had an instance of the subclass and never the superclass as the superclass still wouldn't dispatch to C._privateGo (you're much, much better off editing the source or grabbing a new version).

Corbin
  • 33,060
  • 6
  • 68
  • 78
2

Don't know whether this is help full toy you

if it is a private method there may be a public method which is calling that private method. You may try overriding that public method in your class.

someone
  • 6,577
  • 7
  • 37
  • 60
1

Here is the hacking way:

A private method is meant to be use in some of the public methods in the class. Extend the class, override that public method. Leave everything same but call your own private method from the overridden method.

Yogendra Singh
  • 33,927
  • 6
  • 63
  • 73
  • Other classes in the package might call the original class, though. So to make it work, you also have to extend & override these other classes as well. And then other classes might call these classes too. After you are done you basically rewrote the whole package. Fun times when a new version of the package comes out. – Guillaume F. Apr 05 '20 at 09:56
0

Private methods are not inherited and cannot be overridden in any way. Following threads might help you...

1 . Overriding private methods in Java

2 . Confusing "override a private method"

3 . Can I override a private method in Java?

Community
  • 1
  • 1
Snake Eye
  • 535
  • 3
  • 13
0

The source for the GWT classes comes with the bundle. Why not edit the source to fix the problem?

Bohemian
  • 412,405
  • 93
  • 575
  • 722
0

- One of the most important thing to remember about private methods is that, they are NOT INHERITED.

- So one can say that a Sub-Class is totally blind about the existence of the Private member in the Super-Class. So its quite obvious that it can't be Overridden.

Kumar Vivek Mitra
  • 33,294
  • 6
  • 48
  • 75