如何限制子类访问超类方法?

时间:2014-07-15 09:35:49

标签: java class oop

例如:

我有课

package test.inheritance.checkMultiple;

public class Company {



protected void run (){
    System.out.println("Time to Run");
}

}

public class Department extends Company {



    public void testDepartment()
    {
        Department d =new Department();
        d.run();
    }

}


public class Employee extends Department{


    public void checkValidEmployee(){
        Employee e =new Employee();
        e.run();
        e.testDepartment();

    }

    public static void main (String[] artgs)
    {
        Employee e =new Employee();
        e.checkValidEmployee();
        System.out.println("Valid Emplyee");
    }

}

1)我不希望员工类有权访问run方法。我该怎么办?

2)如果我想给员工类提供run方法的访问权限,而不是部门类,那我该怎么办?

3 个答案:

答案 0 :(得分:1)

你不应该这样,因为每个儿童班都必须是一个有效的孩子班级 - 见Liskovs substitution principle

所以使用@michael Laffargue建议的构图而不是继承

我不知道你想要实现什么,但它应该看起来更像下面的内容,公司是Department的成员变量,而Department又是Employee的成员变量。

import java.util.Arrays;
import java.util.List;

public class Company
{
    private final String name;

    public Company(String name)
    {
        this.name = name;
    }

    public String getName()
    {
        return this.name;
    }

    public void run()
    {
        System.out.println("Time to Run");
    }
}

public class Department
{
    private static final List<String> VALID_DEPARTMENTS = Arrays.asList("IT");
    private final String name;
    private final Company company;

    public Department(String name, Company company)
    {
        this.name = name;
        this.company = company;
    }

    public String getName()
    {
        return this.name;
    }

    public Company getCompany()
    {
        return this.company;
    }

    public void checkValid()
    {
        if (!VALID_DEPARTMENTS.contains(this.name))
            throw new AssertionError();
    }

}

public class Employee
{
    private final Department department;
    private final String name;

    public Employee(String name, Department department)
    {
        this.department = department;
        this.name = name;
    }

    public void checkValid()
    {
        this.department.getCompany().run();
        this.department.checkValid();
    }

    public Department getDepartment()
    {
        return this.department;
    }

    public String getName()
    {
        return this.name;
    }
}

public class Main
{
    public static void main(String[] artgs)
    {
        final Company company = new Company("Company");
        final Department department = new Department("IT", company);
        Employee e = new Employee("Peter", department);
        e.checkValid();
        System.out.println("Valid Emplyee");
    }
}                                                                                                                                                                                                                                                                                                                                                                                   

答案 1 :(得分:0)

只需将子类访问的方法声明为private。当您对某些方法使用private访问说明符时,该子方法不会继承该特定方法class.You可以看到下表,知道访问说明符的工作原理

enter image description here

还要研究此链接http://docs.oracle.com/javase/tutorial/java/javaOO/accesscontrol.html

答案 2 :(得分:0)

您可以将方法声明为私有,并且子类无法看到它。

此外,您可以使用默认修饰符(包)并将子项放在不同的包中。