Khan
Khan

Reputation: 139

How to access protected method of abstract class?

Is there any way to access protected method of abstract class?

In selenium webdriver i am unable to access protected method of class "SingleBrowserLocator"

http://selenium.googlecode.com/git/docs/api/java/index.html

Upvotes: 3

Views: 6146

Answers (2)

Stephen C
Stephen C

Reputation: 718778

Like this:

public abstract class Foo {
    protected void method() { ... }
}

public class Bar extends Foo {
    public void method() {
        super.method();
    }
}

If you can't create a subclass (named as above, or anonymous), then reflection (or something that uses it) is your best option.

Upvotes: 1

Daniel Gabado
Daniel Gabado

Reputation: 284

There are 3 ways:

  • Create a new class that extends that abstract class SingleBrowserLocator (you will have to implement the abstract methods in it);
  • Search for a non abstract subclass of SingleBrowserLocator that makes that method public or has other public methods that calls the protected one;
  • Search for another class in the same package of a non abstract subclass of SingleBrowserLocator that provides access to that method;

If the method is useful to you and made protected in an abstract class, probably the better or only correct choice is that you will have to implement a new subclass of it (the first choice above).

Upvotes: 1

Related Questions