Unable to call abstract class method from interface

j1nrg

I'm having a bit of a hard time grasping the concept of programming to an interface and using abstract classes at the same time.

I have a set of "Retriever" classes which retrieve different types of data from the Internet. These retriever classes retrieve strings of JSON responses

public class AccountRetriever extends DataRetriever implements AccountInformation{
     List<String> getAccountInformation{
      ....
   }
}

public class BalanceRetriever extends DataRetriever implements BalanceInformation{
     List<String> getBalanceInformation{
      ....
   }
}

Both AccountInformation and BalanceInformation are interfaces with the methods shown in them. I am also extending from an abstract class "DataRetriever" which has a method called

public String getResponseAsString();

After instantiating these classes in my main using interfaces:

AccountInformation ai = new AccountRetriever(apiRequest);
List<String> aiList = ai.getAccountInformation();   <----this works
ai.getResponseAsString();      <----this doesn't work. Why??

BalanceInformation bi = new BalanceRetriever(apiRequest);
List<String> biList = bi.getBalanceInformation();   <----this works
bi.getResponseAsString();      <----this doesn't work. Why??

The getResponseAsString() method exists in the abstract class each of these retrievers extend from so why am I unable to access that method? The only methods that I can access are the ones in the interface. What am I doing wrong here?

slipperyseal

These methods aren't visbale because while you construct a AccountRetriever, your ai variable is only of type AccountInformation, the interface which only has getAccountInformation() not getResponseAsString()

AccountInformation ai = new AccountRetriever(apiRequest);
List<String> aiList = ai.getAccountInformation();   <----this works
ai.getResponseAsString();      <----this doesn't work. Why??

But if you change the variable type to DataRetriever then getAccountInformation() will be visible but getAccountInformation() will not.

DataRetriever ai = new AccountRetriever(apiRequest);
List<String> aiList = ai.getAccountInformation();   <----this wont work
ai.getResponseAsString();      <---- this will work

You will need to change the variable type to AccountRetriever which both extends DataRetriever and implements AccountInformation

AccountRetriever ai = new AccountRetriever(apiRequest);
List<String> aiList = ai.getAccountInformation();   <----this will work
ai.getResponseAsString();      <---- this will work

Or, change your design so that both AccountInformation and BalanceInformation both extends a common interface which defines getResponseAsString(). And remove the abstract class all together.

public interface Information {
    public String getResponseAsString();
}

public interface AccountInformation extends Information {
    public List<String> getAccountInformation();
}

public interface BalanceInformation extends Information {
    public List<String> getBalanceInformation();
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Java

How to call abstract method from abstract class called by inherit class

From Dev

Call a method from abstract class c#

From Dev

Kotlin concrete class extending from abstract class and interface, with the interface using a method implemented in the abstract class

From Dev

Unable to call Interface methods from another class

From Java

Same method in Interface and Abstract class

From Dev

Typing method of a abstract class with an interface

From Dev

call an instance method from an interface class

From Dev

Is the abstract method in the interface Inter is same with the abstract method in the abstract class Test?

From Dev

How to call non abstract method in a abstract class?

From Dev

How to call a method in an abstract class

From Dev

How to call abstract class method

From Dev

Unable to call method from extended class

From Dev

Unable to clone an object from within an abstract class method in Scala

From Dev

Call again abstract inherited method from another class

From Dev

Call no-virtual subclass method from a pointer base abstract class

From Dev

How to call an abstract method from a Class parameter in Kotlin?

From Dev

Why can we call abstract method from an abstract class before sub-class initialization

From Java

Java : call child class method from parent class using interface

From Dev

Unable to type class method that extends from a abstract class generic method (TypeScript)

From Dev

Unable to call method from another method of the same class in JAVA

From Java

Call method from another method in abstract class with same name in real class

From Dev

How to call a method of super class from parent class. How to call the same method from interface?

From Dev

Unable to call a method that is declared in one class from another singleton class

From Dev

Java Overriding abstract class method with an interface as param

From Dev

Does abstract class hide or override the method of the interface?

From Java

static method from abstract class

From Dev

Referencing method from abstract class

From Dev

Call interface method implemented in an activity from a java class

From Java

Difference between abstract class with all method abstract and interface?

Related Related

  1. 1

    How to call abstract method from abstract class called by inherit class

  2. 2

    Call a method from abstract class c#

  3. 3

    Kotlin concrete class extending from abstract class and interface, with the interface using a method implemented in the abstract class

  4. 4

    Unable to call Interface methods from another class

  5. 5

    Same method in Interface and Abstract class

  6. 6

    Typing method of a abstract class with an interface

  7. 7

    call an instance method from an interface class

  8. 8

    Is the abstract method in the interface Inter is same with the abstract method in the abstract class Test?

  9. 9

    How to call non abstract method in a abstract class?

  10. 10

    How to call a method in an abstract class

  11. 11

    How to call abstract class method

  12. 12

    Unable to call method from extended class

  13. 13

    Unable to clone an object from within an abstract class method in Scala

  14. 14

    Call again abstract inherited method from another class

  15. 15

    Call no-virtual subclass method from a pointer base abstract class

  16. 16

    How to call an abstract method from a Class parameter in Kotlin?

  17. 17

    Why can we call abstract method from an abstract class before sub-class initialization

  18. 18

    Java : call child class method from parent class using interface

  19. 19

    Unable to type class method that extends from a abstract class generic method (TypeScript)

  20. 20

    Unable to call method from another method of the same class in JAVA

  21. 21

    Call method from another method in abstract class with same name in real class

  22. 22

    How to call a method of super class from parent class. How to call the same method from interface?

  23. 23

    Unable to call a method that is declared in one class from another singleton class

  24. 24

    Java Overriding abstract class method with an interface as param

  25. 25

    Does abstract class hide or override the method of the interface?

  26. 26

    static method from abstract class

  27. 27

    Referencing method from abstract class

  28. 28

    Call interface method implemented in an activity from a java class

  29. 29

    Difference between abstract class with all method abstract and interface?

HotTag

Archive