java调用抽象类的方法,如何在Java中调用抽象类方法

在Java中,不能直接实例化抽象类。要调用`Call`类的`getEarliestConnection()`方法,需创建其子类如`MyCall`并覆写抽象方法。当你尝试调用`getCallFailedString()`时,传入的`Call`对象未初始化导致NullPointerException。解决方案是创建`MyCall`实例,并将其传递给方法。
摘要由CSDN通过智能技术生成

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

public abstract class Call {

public Connection getEarliestConnection() {

Connection earliest = null;

...

return earliest;

}

}

I want to call the above method, and the calling class is:

public class MyActivity extends Activity {

Connection c = new Connection();

private void getCallFailedString(Call cal)

{

c = cal.getEarliestConnection();

if (c == null) {

System.out.println("** no connection**");

} else {

System.out.println("** connection");

}

}

}

Whenever I try to run the above class, it throws a NullPointerException on the line c = cal.getEarliestConn

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值