Access modifier for methods in Java - Default

1

I just did a test and I found a suspicious question, I would like your opinion.

  

In the Java programming language, when the method of a class
  does not have an explicitly declared access modifier   means that this method can be accessed

     

(A) for all classes of the same package it was declared in.

     

(B) by any other class, other than that to which   belongs.

     

(C) by the class to which it belongs, exclusively.

     

(D) by the class in which it was declared and its subclasses, and by members   of other classes in the same package.

I think that answer D is correct, even in a test that I did the method behaved normally and with no problem at compile time and at runtime:

class Teste {
    String teste() {
        return "ola";
    }
}

class Teste2 extends Teste {

}

class Principal {
    public static void main(String[] args) {
        Teste2 t = new Teste2();
        System.out.println(t.teste());
    }
}

The preliminary feedback reports that the correct answer is (A).

    
asked by anonymous 06.03.2017 / 00:55

1 answer

1

One answer just to stay logged in:

The answer is the letter A. If you do not have access modifier specified, then access is package.

If a subclass is in another package, it will not have access / visibility to the method or property of the parent class with packet-like access modifier.

    
10.03.2017 / 18:57