java中的继承函数

时间:2014-11-19 11:49:28

标签: java class inheritance

重复Disabling inherited method on derived class

这是我的班级代码:

class Parent{
    public bool sum(int a){return true;}

    public int mul(int a){return a*a;}

}

以及另外两个扩展父类的类:

class derived extend parent{
    //here this child can see two methods of parent class and
    //I want just see sum method in here and don't see mul method
}
class derivedb extend parent{
//here this child can see two methods of parent class and
//I want here just see mul method.
}

1 个答案:

答案 0 :(得分:2)

您可以通过在父类中将mul作为私有方法来实现。因此,mul方法仅在Parent

内具有可见性
class Parent{
    public bool sum(int a){return true;}

    private int mul(int a){return a*a;}

}
相关问题