0

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.

4
  • 12
    You need to declare myObj as either B or myInterface, A doesn't have the method. Commented Mar 30, 2020 at 18:05
  • @daniu does this mean it's not possible to code in a polymorphic way when you use interfaces then? I know it's possible to program this way and the program will find the correct methods. For example, repl.it/repls/SaneIndianredMachinecodeinstruction Commented Mar 30, 2020 at 18:23
  • @user85421 It looks like this may be a case of the teacher believing and showing things as being able to work one way when in reality they do not. According to him, it should still be able to find the class/interfaces methods of the subclass. Upon further testing, it does not seem to work that way. Commented Mar 30, 2020 at 18:45
  • @user85421Well, I'll go with changing it to B bObj = new B(); and stay with that standard from here on out. I thought it was a bit weird to go with A bObj, but... a student's gotta follow the teacher first. Thanks! Commented Mar 30, 2020 at 18:54

3 Answers 3

2

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you, this is about how I thought it worked, but the instructions I was receiving were exactly the opposite. It sounds like even though you could do something with polymorphism, there might not be too much reason to if you really need the additional methods defined in the subclass... which makes sense.
1

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.

Comments

0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.