ephreal
ephreal

Reputation: 51

Java implemented interface methods not accessible from subclass

Description of issue:

When trying to run methods in a subclass that implements an interface, the compile will error out with the error

Main.java:5: error: cannot find symbol

This occurs regardless of package locations, code location (ie: all code in one file vs. split out into separate *.java files), or methods implemented.

Example of the issue:

I have 2 classes and an interface, A, B, and myInterface. Class A is abstract while B inherits from A and implements myInterface. The problem is, when I run my code, I cannot call any methods from the interface I implemented.

interface myInterface {
  public void printSomething();
}

abstract class A {
  int aVal;

  A() {
    this.aVal = 0;
  }

  public int getVal() {
    return aVal;
  }
}

class B extends A implements myInterface {
  B() {
    super();
  }
}

public void printSomething() {
  System.out.println("Something");
}

class Main {
  public static void main(String[] args) {
    A bObj = new B();

    bObj.printSomething();
    // Throws
    // Main.java:5: error: cannot find symbol
    //       bObj.printSomething();
    //           ^
    // symbol:   method printSomething()
    // location: variable bObj of type A
    // 1 error
  }
}

For easier running and viewing, I have the same code as above saved here as well.

https://repl.it/repls/IdealMobileUserinterface

Why is this happening, and how would I go about resolving the issue? I've stumped myself and my teacher trying to figure this one out.

Upvotes: 0

Views: 1532

Answers (3)

daniu
daniu

Reputation: 14999

Let me try to answer this canonically.

Here's your interfaces:

interface iA {
    void fromA();
}
interface iB {
    void fromB();
}

and a class hierarchy.

class cX implements iA {
    public void fromX() {}
    public void fromA() {} // to implement iA
}
class cY extends cX implements iB {
    public void fromY() {}
    public void fromB() {} // to implement iB
}

Note that cY also implicitly implements iA - it's handed down from inheriting from cX.

Now let's look at what's valid.

cY cy = new cY();
cy.fromA();  // valid - cY extends cX implements iA
cy.fromB();  // valid - cY implements iB
cy.fromX();  // valid - cY extends cX
cy.fromY();  // valid - cy is a cY

cX cx = new cY(); // valid - cY extends cX
cx.fromA();  // valid - cx implements iA
cx.fromB();  // INVALID - cX does not implement iB. The compiler doesn't know cx is actually a cY instance.
cx.fromX();  // valid - cY extends cX
cx.fromY();  // INVALID - cX is not a cY

iA ia = new cY(); // valid cY extends cX implements iA
ia.fromA();  // valid - fromA() is an iA method
ia.fromB();  // INVALID - iA is not a cY
ia.fromX();  // INVALID - iA does not have that method. Again, compiler doesn't know ia is a cY instance.

Since you ask about "polymorphism" in the comments: this is the polymorphism part:

iA v = new cY();  // can assign a cY instance to an iA variable
v = new cX();     // can assign a cX instance to an iA variable

This is also the reason you can't now call v.fromX() or v.fromY(); v can have been assigned either, so only the common denominator is available - namely the iA methods.

Upvotes: 2

learner
learner

Reputation: 11

Since you don't have the method printSomething() in class A and you are using Reference of Class A to hold the child object B, so by inheritance principle you cannot access child specific methods using parent references.

Upvotes: 0

loganrussell48
loganrussell48

Reputation: 1864

I believe the issue is that you're putting everything into a class A reference, which doesn't implement myInterface, and so doesn't necessarily have a printSomething method to call.

You have an object of type A. And you're trying to call printSomething on it, but types of A don't necessarily have that method. Only things that implement myInterface have that method. So, you need to specifiy that A has that method.

Or create a generic method that only works on types of A that implement that interface.

Upvotes: 1

Related Questions