The second major feature of java: inheritance and the use of super keywords

inherit
First of all, you need to know that the base class of all classes is Object class.
As long as you create a class, you inherit the Object class by default.
1. In the inheritance relationship, "the child class is a parent class", that is to say, the child class can be treated as a parent class.
For example, if the parent is human and the child is student, then the student is human.

2. Defining a parent class is a common class

3. Define the format of subclasses:
public class subclass name extends parent class name{
}

this keyword solves the problem of renaming member variables and local variables (methods)
4. After the subclass inherits the parent class, the attributes, methods and subclasses declared by the parent class can be obtained.
Note: When there are private attributes or methods in the parent class, subclasses can also be retrieved. For encapsulation reasons, attributes can not be used directly, but can be written to getset method calls.

5. In addition to inheriting the attributes and methods of the parent class, subclasses can also define their own unique methods and attributes.

6.java inheritance is single inheritance, a subclass can only inherit one parent class, a parent class can have more than one subclass

super keyword
Why use extends
Need a new object, (the object is also called an instance of a class, using the keyword new constructor (... ) Way)
To access methods of the parent class?
Because of the new construction method, the super keyword is omitted.
And super (); causes you to call the parent class's parametric constructor
Only then can we call other methods in the parent class and inherit this statement.
Of course, the use of super keywords goes beyond that.
super keywords can modify attributes, methods, construction methods
1. When the child class and the parent class property are renamed, the properties declared in the parent class can be invoked by "super. property name"

If you want to distinguish renames of attributes and local variables in a classification, use "this. attribute name"“

2. After the subclass overrides the parent method, if the subclass wants to call the method overridden by the parent again, it can be modified with the super. method name.
3. Super modifies the constructor by explicitly invoking the constructor specified by the parent class by using "super" in the subclass

Note: Within the construction method, "super" must be declared in the first line.
Within the construction method, this or super can only appear one.
Within the constructor, the call "super" is not displayed, and the parent class's parametric constructor is invoked by default.

Define a person parent class
Among them are the eat method defined by the getset method and the parameter-free construction method.

public class Person {
 
	 int id=1001;//ID number
	private String name;//Full name
	private int age;//Age
	public int getId() {
		return id;
	}
	public void setId(int id) {
		this.id = id;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public int getAge() {
		return age;
	}
	public void setAge(int age) {
		this.age = age;
	}
	public Person(int id, String name, int age) {
		super();
		this.id = id;
		this.name = name;
		this.age = age;
	}
	public Person(String name, int age) {
		super();
		this.name = name;
		this.age = age;
	}
	public Person() {
		super();
		System.out.println("hehe");
	}
     
   public void eat(){
	   System.out.println("Having dinner");
   }
   public void eat(int i){
	   System.out.println("Having dinner");
   }
   public void sleep(){
	   System.out.println("Sleep");
   }
   public void info(){
	   System.out.println("Full name:"+name+",Age:"+age+",Student number:"+id);
   }
}

Subclass Student Class

public class Student extends Person{
         int id=1002;//Student ID
         private String schoolName;
         
	    public Student() {
			super();
     	System.out.println("hehe2");
		}
	    
		public Student(String schoolName) {
			super(1001,"Bachelor",100);//The construction method of three parameters of the parent class is invoked.
			//Direct Reference Transfer
			this.schoolName = schoolName;
		}

		public void learn(){
        	 System.out.println("Study");
         }
	    public void eat(){
	    	System.out.println("I am a student, because I am the motherland of many, so I want to eat nutrition.");
	    }
	    public void show(){
	    	System.out.println(this.id);//1002
	        System.out.println(super.id);//1001
	        eat();//Subclass methods
	        super.eat();//Parent method
	       
	    }
}

Test class
You can call the method yourself to see what the output is.

In particular:
Protected: In the same class, under the same package, different packages but inheriting the parent class can call protected methods or attributes.

public class OrderA {
   public int no;
   protected String name;
   int orderNum;
   private String desc;
   }

Keywords: Java Attribute

Added by SlyOne on Fri, 02 Aug 2019 10:19:20 +0300