Hello, I came up with a question: there is a class "man" - public, its successor is "student". when it works without a modifier, and when I put private for example, modifier writes private not allowes here, what's the problem? Here is the code:

package l2; public class Man { protected int age; protected double weight; protected String name, gender; protected Man (int age, double weight, String name, String gender) { this.age = age; this.weight = weight; this.name = name; this.gender = gender; } protected int ageChanged (int n_a){ age = n_a; return age; } protected double weightChanged (double n_w){ weight = n_w; return weight = n_w; } protected String nameChanged (String n_n){ name = n_n; return name; } public static void main(String[] args) { Student p1 = new Student (18, 67.3, "Anton", "male", 2016); Man p2 = new Man (45, 67.3, "Jack", "male"); System.out.println("Возраст p1: "+p1.age+"; год обучения p1: "+p1.y_of_s+";"); System.out.println("Возраст p2: "+p2.age+";"); System.out.println("Возраст p1 (новій): "+p2.ageChanged (19)+"; год обучения p1 (новій): "+p1.y_of_sChanged (1)+";"); System.out.println("Возраст p2 (новій): "+p2.ageChanged (46)+"."); } } class Student extends Man { protected int y_of_s; protected Student (int age, double weight, String name, String gender, int y_of_s){ super (age, weight, name, gender); this.y_of_s = y_of_s; } protected int y_of_sChanged (int n_y_of_s){ y_of_s += n_y_of_s; return y_of_s; } protected int ageChanged (int n_a){ age += n_a; return age; } 

}

  • Can you add some code to the question? What you have and what you are trying to do and what happens as a result of the experiments. - Alexey Shimansky
  • You have a piece of code fell out of ... - Leonid Lunin
  • already fixed it - Muscled Boy
  • @MuscledBoy My program has worked, here's the result: Возраст p1: 18; год обучения p1: 2016; Возраст p2: 45; Возраст p1 (новій): 19; год обучения p1 (новій): 2017; Возраст p2 (новій): 46. Возраст p1: 18; год обучения p1: 2016; Возраст p2: 45; Возраст p1 (новій): 19; год обучения p1 (новій): 2017; Возраст p2 (новій): 46. Возраст p1: 18; год обучения p1: 2016; Возраст p2: 45; Возраст p1 (новій): 19; год обучения p1 (новій): 2017; Возраст p2 (новій): 46. - Leonid Lunin
  • one
    @MuscledBoy To understand this, it is enough to put the Man class in one file, and the Student class in another, and public static void main() in the third, and then look at which ones will work. Student and Man in theory should have an access modifier in the default or public constructor. Methods are also public or default . private , protected are used primarily when the class has internal logic that should not be accessible from the outside. Suppose we want to print a body index for a student, then we need to make a private height, weight, and make the function that returns the body index public - Leonid Lunin

2 answers 2

A modifier allows access inside a single .java file, if you have different files, the first class has a private access modifier, then its successor simply cannot see it. If you add two classes into one file, then you can change their modifiers as you please, but you won’t get to these classes from another file if they don’t have a public or default(без модификатора, доступен внутри одного пекейджа) closed to open is built like this: private, default(без модификатора), protected,public

  • that is, if I have a public parent, then the heir can only have public? if I understand you correctly, and my default one also works. it is right? - Muscled Boy
  • Wrong, misunderstand you, refresh page - Leonid Lunin
  • The fact is that both classes are in the same file, compiled only when the successor is package - Muscled Boy
  • public class Main { public static void main(String[] args) { } public class SampleClass { SampleClass() { } } private class ThisClass extends SampleClass { ThisClass() { super(); } } } public class Main { public static void main(String[] args) { } public class SampleClass { SampleClass() { } } private class ThisClass extends SampleClass { ThisClass() { super(); } } } same as you, I have perfectly compiled - Leonid Lunin

I think the problem is that you are trying to point a private / protected modifier to a class at the top level, and you cannot do it by specification. From Java Language specification:

Enclosing class declaration

  • You can translate this answer here to stackoverflow.com/a/3491156/6104996 with a link to it)) It will be useful - Alexey Shimansky
  • I just started to learn java, say "highest level" is this a class that is just a successor of object? - Muscled Boy
  • You can say so - Leonid Lunin