-1

There is an existing class with this method

protected HttpResponse post (String route, String json,BasicNameValuePair...parameters) throws IOException

I am creating a child class in a different package, but want to call this method. How do I do that?

Also, the parent class of the child class is abstract, so I can't instantiate it.

William Roberts
  • 319
  • 2
  • 5
  • 21
  • 2
    did you try, in your child class, to do something like that? -> super.post(params)... protected means only child can access, doesn't matter the package location. If it was default, then it would matter. – Lucas Jan 28 '16 at 19:35
  • @Lucas actualy, unlike C++, in java protected means that it can be accessed in the same package. (And in subclasses of course). – Andrej Jan 28 '16 at 19:51
  • Default means it can only be accessed from the same package. Protected means it can only be accessed from the child. – Lucas Jan 28 '16 at 19:54
  • 1
    Possible duplicate of [Call protected method from a subclass of another instance of different packages](http://stackoverflow.com/questions/14404260/call-protected-method-from-a-subclass-of-another-instance-of-different-packages) – OYRM Jan 28 '16 at 20:10

3 Answers3

1
package package2;
import package1.Parent;
class Child extends Parent //assuming Parent is in package 1
{
   @Override
   protected HttpResponse post(...)
   {
       super.post(...)
       //Remaining stuff
   }
}
Jonah Graham
  • 7,890
  • 23
  • 55
SomeDude
  • 13,876
  • 5
  • 21
  • 44
-1

A commenter linked the answer here. I think I deleted that comment by mistake:

Call protected method from a subclass of another instance of different packages

Community
  • 1
  • 1
William Roberts
  • 319
  • 2
  • 5
  • 21
-1

William, you can call the method from any method of your child class provided the reference you use to invoke it is of the type of the child class or a subclass of it (the rationale, among other things, is explained in my answer to this question). As a simplified example, consider this Parent class

package parent;

public class Parent {

    protected void method() {
        System.out.println("parent.Parent.method() called");
    }
}

extended by a Child class

package child;

import grandchild.Grandchild;
import parent.Parent;

public class Child extends Parent {

    public void anyMethod(Child child, Grandchild grandchild) {
        this.method();
        child.method();
        grandchild.method();
    }
}

extended, in turn, by a Grandchild class.

package grandchild;

import child.Child;

public class Grandchild extends Child {
}

Given this class hierarchy, this code

Child child1 = new Child();
Child child2 = new Child();
Grandchild grandchild = new Grandchild();
child1.anyMethod(child2, grandchild);

would produce the following output.

parent.Parent.method() called
parent.Parent.method() called
parent.Parent.method() called

So, a Child object can access its own protected method() member as well as the method() member of another instance of Child or of an instance of a subclass of Child. It can't, however, call the method using a reference whose type is Parent (unless it uses super) or a different subclass of Parent (say, Brother).

I hope this answers your question and gives you an idea of what you can (or can't) do. If not, let me know.

Community
  • 1
  • 1
Alberto Doda
  • 281
  • 2
  • 6