Hello, there was a problem compiling the project. It is necessary to create a class and its successor and an overridden method. Next to each class, it writes class ... is public name and it offers to rename the file. How I understand the problem is that the classes had to scatter in different files? But I read that it is possible to keep several in one. Help me please.

package l2; public class Man { int age; double weight; String name, gender; public Man (int age, double weight, String name, String gender) { this.age = age; this.weight = weight; this.name = name; this.gender = gender; } public int ageChanged (int n_a){ age = n_a; return age; } public double weightChanged (double n_w){ weight = n_w; return weight = n_w; } public String nameChanged (String n_n){ name = n_n; return name; } } public class Student extends Man { int y_of_s; public 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; } public int y_of_sChanged (int n_y_of_s){ y_of_s += n_y_of_s; return y_of_s; } public int ageChanged (int n_a){ age += n_a; return age; } } public class Main { 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: "+p1.age+";"); System.out.println("Возраст p1 (новый): "+p1.ageChanged (1)+"; год обучения p1 (новый): "+p1.y_of_sChanged (1)+";"); System.out.println("Возраст p2 (новый): "+p1.ageChanged (46)+";"); } } 

    1 answer 1

    You can put several classes in one file, but only one of them can be public. And its name should coincide with the name of the file.

    The idea is that you put a public class in the file, and small private classes related to it (all sorts of helpers, for example, or internal functionality).

    The meaning of this design is to force programmers to properly organize the code, using files to separate the functionality.

    • then I ask you about the following: should I have this Public Main or Man? - Muscled Boy
    • yes, talking about the class - Muscled Boy
    • @MuscledBoy: I see you have two classes - Man and Main . Put each one in a separate file. - VladD
    • @MuscledBoy: The decision on which of the classes to declare public and which not, should be made by you, regardless of the code layout for the files. - VladD
    • Sorry, stupid question, but completely confused. Senya has a class man and his student heir. The entry point to the program is the main method, and it belongs to the Main class. Or does he need it (the Main class)? - Muscled Boy