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; } }
...- Leonid LuninВозраст 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 LuninManclass in one file, and theStudentclass in another, andpublic static void main()in the third, and then look at which ones will work.StudentandManin theory should have an access modifier in thedefaultorpublicconstructor. Methods are alsopublicordefault.private,protectedare 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 aprivateheight, weight, and make the function that returns the body indexpublic- Leonid Lunin