How to call abstract class method in java

后端 未结 3 1197
暖寄归人
暖寄归人 2021-01-12 11:14

I want to call a method of an abstract class in my own class. The abstract class is:

public abstract class Call {

    public Connection getEarliestConnectio         


        
相关标签:
3条回答
  • 2021-01-12 11:44

    Firstly, Call an abstract class, therefore you cannot instantiate it directly. You must create a subclass, say MyCall extends Call which overrides any abstract methods in Call.

    Getting a NullPointerException means that whatever you are passing in as an argument to getCallFailedString() hasn't been initialized. So after you create your subclass of Call, you'd have to instantiate it and then pass this in to your method, so something like:

    class MyCall extends Call 
    { 
         //override any abstract methods here... 
    }
    

    Wherever you are calling getCallFailedString() would then require something above it like:

    Call cal = new MyCall();
    Activity activity = new MyActivity();
    activity.getCallFailedString(cal);
    
    0 讨论(0)
  • 2021-01-12 11:48

    Make sure your object "cal" is initialized and not null. Also, you won't be able to instantiate a Call object(as its an abstarct class). Instead, declare class Call as an interface and implement its method getEarliestConnection(), in your class.

    0 讨论(0)
  • 2021-01-12 11:59

    Looks like the Call cal is null before it is passed into the function getCallFailedString. Make sure you extend Call and instantiate the extended class and pass it into getCallFailedString.

    0 讨论(0)
提交回复
热议问题