为了账号安全,请及时绑定邮箱和手机立即绑定

在接口中重写默认方法,并在接口中执行默认方法

在接口中重写默认方法,并在接口中执行默认方法

catspeake 2021-05-03 16:34:09
我想通过创建具体实现类的对象来执行接口中默认方法的定义主体,该对象也已覆盖了该方法。无论是直接创建具体实现类的对象还是通过动态绑定/多态性,实现类中定义/覆盖的主体都只会被执行。请看下面的代码interface Banking {    void openAc();    default void loan()    {    System.out.println("inside interface Banking -- loan()");    }}class Cust1 implements Banking{    public void openAc()    {        System.out.println("customer 1 opened an account");    }    public void loan()    {        // super.loan(); unable to execute the default method of interface         //when overridden in implementation class         System.out.println("Customer1 takes loan");    }}//Cust1 concrete implementation classclass IntfProg8 {    public static void main(String[] args)     {        System.out.println("Object type=Banking \t\t\t Reference type=Cust1");        Banking banking = new Cust1();        banking.openAc();        banking.loan();    } }我想知道如何在Banking界面内的控制台中打印以下内容-loan()当默认方法被覆盖时
查看完整描述

2 回答

  • 2 回答
  • 0 关注
  • 223 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信