Metoda chroniona - niewidzialna w głównej klasie.

0

Witam. Uczę się z książki Thinking in Java (wydanie 3) i robiłem zadania odnośnie modyfikatorów dostępów.

Napotkałem problem w miejscu, w którym według książki być go nie powinno.

Oto testowy kod:

import components.*;

public class Base extends Extensions {

	public static void main (String[] args){
		
		Extensions ex = new Extensions();
		ex.Sugar();
	}
}
package components;

public class Extensions {

	protected void Sugar() { System.out.println("sugar");}
}
 

Są to dwa różne pakiety z czego główna klasa "Baza" dziedziczy po klasie "Extensions". Miała ona uzyskać dostęp do chronionej metody Sugar() wedle opisanych zasad, że owe (chronione) składniki będą dostępne jedynie dla klas dziedziczących je.

No i w tym miejscu mam problem bo wyskakuje komunikat, że metoda nie jest widzialna. Gdzie jest błąd? Czy to moje złe rozumowanie?
Pozdrawiam.

1

A protected member or constructor of an object may be accessed from outside the package in which it is declared only by code that is responsible for the implementation of that object.
//
Let C be the class in which a protected member m is declared. Access is permitted only within the body of a subclass S of C. In addition, if Id denotes an instance field or instance method, then:
If the access is by a qualified name Q.Id, where Q is an ExpressionName, then the access is permitted if and only if the type of the expression Q is S or a subclass of S. [...]

1 użytkowników online, w tym zalogowanych: 0, gości: 1