Java: protected methods not accessed in different class of different package when I used extends keyword to achieve multiple inheritance

前端 未结 1 1701
甜味超标
甜味超标 2021-01-26 13:33

I Have following two classes: Test.java

package com.test.app;

public class Test {

    public int a=10;
    protected void testFunc() {
        // TODO Auto-gen         


        
1条回答
  •  旧巷少年郎
    2021-01-26 13:55

    The Test#testFunc() method is only accessible for sub-classes (like Main) and for classes in the same package (com.test.app).

    This is why the statement

    main.testFunc();
    

    compiles fine (because Main is a sub-class of Test and it's allowed to call testFunc()).


    This statement, however

    test.testFunc();
    

    doesn't compile, because the package where the Main class is located is not com.test.app, but com.test.main.

    More info:

    • Difference between public, default, protected, and private

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