2

I have an Interface MyInterface with two methods doSomething() and doSomethingElse().

I have an Abstract public class A that implements this interface. I Have a class C that extends class A and therefore implements the interface's methods as well. On the main method I assign the object of class C to a MyInterface reference like: MyInterface myiface = new C();

The problem is that if I add some methods in either class (abstract or concrete) I cannot call the method through the reference myiface. I have to downcast it first. Shouldn't I be able to polymorphically access the class as it is shown in the book Java SE8 for programmers of Deitel (Chapter 10 Polymorphism and Interface) ?

Carcigenicate
  • 43,494
  • 9
  • 68
  • 117
D3bian0s
  • 51
  • 3
  • No. The variable is of type MyInterface, so the compiler can't possibly assume there are other methods than the one declared in MyInterface callable on this variable. If you need to access methods of A, then make the variable of type A. If you need to access methods of C, then make it of type C. – JB Nizet Sep 02 '17 at 08:19
  • 1
    Code is worth 1024 words. All of the verbiage above could be more cleanly and clearly expressed by just showing us the minimal code for the interface and classes involved. – T.J. Crowder Sep 02 '17 at 08:20
  • *"as it is shown in the book Java SE8 for programmers of Deitel (Chapter 10 Polymorphism and Interface"* We can't help you with that bit without your quoting the parts you think mean you have access to new features of `C` from `MyInterface`. – T.J. Crowder Sep 02 '17 at 08:28
  • The reason I didn't post any code is because I am just testing something and the names of the methods and classes are exactly as I mention here. I didn't simplified anything for the sake of the question. – D3bian0s Sep 05 '17 at 18:38

2 Answers2

1

Think of it like this...imagine your MyInterface is named 'Animal' and you have two classes implementing it, named 'Dog' and 'Cow'. just because they are both animals you wouldn't expect the Dog to suddenly give milk, right? In this analogy giveMilk() is a method only implemented by the Cow class.

do you get it?

Mario Köhler
  • 192
  • 1
  • 10
1

Shouldn't I be able to polymorphically access the class...

You could — if the type of your variable were of type C. But it isn't, it's of type MyInterface, and that completely defines what features of the instance you have access to. Yes, the instance has more features, but code written to the interface doesn't know that.

Adding features to C has little to do with polymorphism. In this case, the polymorphism (there are a couple of different kinds of it) is that A and C could implement MyInterface's features in different ways, but your code using instances via a MyInterface reference neither knows nor cares that the instances are As or Cs; all it knows, all it should know, is that they claim to be faithful implementations of MyInterface.

Let's take a concrete example:

public void doSomethingWith(List list) {
    // ...
}

When something calls doSomethingWith, it passes in an instance that fulfills the List interface. That's all that that method knows about the instance (absent things like instanceof and getClass). The instance could be an ArrayList, a LinkedList, a Stack, etc.; but the code using it doesn't know or care about that. All it knows is that it's a List. Even if the method is passed an ArrayList, it can't use (say) ensureCapacity without first downcasting the reference (which will fail if the instance isn't an ArrayList), because not all List implementations have ensureCapacity.

blacktide
  • 10,654
  • 8
  • 33
  • 53
T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875
  • I now understand why I was confused with the book's paradigm. In the book the author has an interface called Payable and an abstract class named Employee which implements this interface. There is also a concrete class named SalariedEmployee. Both classes override the toString() method and you can call it directly from the object, regardless if it is a reference to the Interface. So I assumed that this would be the case for other methods as well, but it is not. Thank you T.J Crowder, I consider your response an answer to the question. – D3bian0s Sep 05 '17 at 18:31