1. What are the access control modifiers of Java? What access rights do they have?
Java has four access rights, three of which have access rights modifiers, namely private, public and protected, and one without any modifiers.
public: modifies class method variables. The corresponding access permissions are: any class of all packages
Protected: classes, methods and variables modified with protected can only be accessed by any class in the package and subclasses that inherit the class outside the package. The key point is that subclasses that inherit the class outside the package can only be accessed, which means that methods and member variables in the protected modified class can only be accessed by subclasses, whether the subclass and parent class are in the same package or not
Default: if the class, method and variable do not use any access modifier, the corresponding access modifier is default, and only any class in the package can be accessed
Private: classes, methods and variables modified with private can only be accessed in this class, and no classes inside or outside the package can be accessed
package A; public class test1{ public String name; protected int age; private int score; public test1(String name,int age,int score){ this.name=name; this.age=age; this.score=score; } public String getName(){ return name; } protected int getAge(){ return age; } private int getScore(){ return score; } }
package B; import A.test1; public class test2 extends test1{ public int value; public test2(String name,int age,int score,int value){ super(name,age,score); this.value=value; } public static void main(String[] args) { test1 t1=new test1("abc",18,99); //test1.name--public System.out.println(t1.name); //test1.score--private System.out.println(t1.score);//The field test1.score is not visible //test1.name--protected System.out.println(t1.age)//The field test1.age is not visible test2 t2=new test2("abc",19,100,89); //Subclasses can access protected modified methods or properties System.out.println(t2.age); System.out.println(t2.getAge()); } }
2. Which attributes and methods are visible to the subclass's parent class?
The subclass inherits all the properties and methods of the parent class, but only the public and protected properties and methods are visible in the subclass.
That is, when initializing an object, the private protected data type also allocates space for it, but it is invisible
When a subclass inherits the parent class, it should first satisfy that the parent class can be accessed. For example, when the subclass and the parent class are not in the same package, the parent class modifier must be public; On the premise that the parent class can be accessed, any parent class attribute member or method with public or protected modifier can be accessed by the child class; Private property members or methods cannot be accessed directly. Subclasses cannot directly access the private properties and methods of the parent class. They can call the public methods of the parent class to indirectly access the private properties
package A; class Parent{ public String name; protected int age; private int score; public Parent(String name,int age,int score){ this.name=name; this.age=age; this.score=score; } public String getName(){ return name; } protected int getAge(){ return age; } public int getScore(){ return score; } } class Student extends Parent{ public Student(String name,int age,int score){ super(name,age,score); } } public class test1{ public static void main(String[] args) { Student stu=new Student("abc",100,19); System.out.println(stu.name);//public System.out.println(stu.age);//protected System.out.println(stu.score);//The field Parent.score is not visible System.out.println(stu.getScore());//Access private properties indirectly by calling public methods of the parent class } }
3. What is combination? What does it do?
Composition, a method of java code reuse. As the name suggests, it is to combine multiple existing objects into a new object with more complex and powerful functions. It reflects the relationship between the whole, part and ownership. Because the internal details between objects are invisible, we also say that this way of code reuse is black box code reuse.
That is, an object is used as an attribute of another object
package A; class Student{ public String name; protected int age; private int score; public Student(String name,int age,int score){ this.name=name; this.age=age; this.score=score; } } class Parent{ public Student stu; public Parent(Student stu){ this.stu=stu; } } public class test1{ public static void main(String[] args) { Student stu=new Student("abc",100,19); Parent p=new Parent(stu); System.out.println(p.stu.name); System.out.println(p.stu.age);//protected the same package class can be accessed. If the Parent and Student are not in the same package, they cannot be accessed } }
4. What is overloading? What does it do?
Overloading, in short, * * is the case where functions or methods have the same name but different parameter lists. Such functions or methods with the same name and different parameters are called overloaded functions or overloaded methods** In Java, two or more methods in the same class can have the same name as long as their parameter declarations are different. In this case, the method is called overloading, and this process is called method overloading
1. The main advantage of method overloading is that there is no need to write multiple functions for different parameter types or parameter numbers. Multiple functions use the same name, but the parameter table, that is, the number of parameters or (and) data types can be different. When calling, although the method name is the same, the corresponding function can be called automatically according to the parameter table. In this way, when calling, we don't need to remember so many method names. Instead, when we know the function of the method, we can directly pass different parameters to it, and the compiler will clearly know which method we called. Overloading is more elegant than if... else and reduces the code of if... else.
2. The most direct function of overloading is to facilitate programmers to automatically match methods according to the number, order and type of different parameters, so as to reduce the repeated steps of writing a function name or method name.
package A; class Student{ public String name; protected int age; private int score; public Student(String name,int age,int score){ this.name=name; this.age=age; this.score=score; } public void test(String name){ System.out.println(name); } public int test(){ System.out.println(age); return age; } public void test(String name,int age){ System.out.println(name+" "+age); } public void test(int age,int socre){ System.out.println(age+" "+score); } } public class test1{ public static void main(String[] args) { Student stu=new Student("abc",19,120); stu.test(); stu.test("zzz"); stu.test("zzz",18); stu.test(18,100); } }
5. What is overwrite? What does it do? What are the conditions for overriding parent methods?
Overriding is a subclass's rewriting of a parent method.
The advantage of rewriting is that subclasses can define their own specific behavior as needed. That is, the subclass can implement the methods of the parent class as needed.
1. The access modifier permission of the subclass should be greater than or equal to that of the parent class and cannot be reduced. The subclass of the private method of the parent class cannot be accessed, let alone overridden.
2. The return type of a subclass can be transformed upward into the return type of the parent class, that is, the return type is a subclass of the return type of the parent class.
3. Exceptions should also be able to transform upward into exceptions of the parent class.
4. Method name, parameter type and number must be consistent
Note: overrides can only be applied to non static, non final, and non constructor methods.
After you have the inheritance concept, you must be clear about the use of this and super:
1) This: indicates to find the parent class from this class first. If there is no parent class in this class, then find the parent class;
2) super: indicates that the parent class is directly searched instead of this class;
package A; class Parent{ public String name; protected int age; private int score; public Parent(String name,int age,int score){ this.name=name; this.age=age; this.score=score; } public void speak(){ System.out.println(name+" "+age+" "+score); } private int getScore(){//Private methods cannot be overridden return score; } public Number getAge(){ return age; } protected void test(){ System.out.println("this is test"); } } class Student extends Parent{ public int value; public Student(String name,int age,int score,int value){ super(name,age,score); this.value=value; } @Override public void speak(){ System.out.println(name+" "+age+" "+value); } @Override public void test(){//Permissions can be increased, but not reduced System.out.println("this is new test"); } @Override public Integer getAge(){//The return type must be the same or a subclass of the parent return type return age; } } public class test1{ public static void main(String[] args) { Student stu=new Student("abc",19,120,10); stu.speak(); stu.test(); } }