[Java] class inheritance

Inheritance using the extends keyword in between Java class that implements

The format is:

Access specifier [modifiers List] class extends the parent class name class name {

    Defined attributes

    Defined constructor (super keyword)

    Definition method

}

Access control character with the public without the use of public and

Modifiers list of commonly used static, final, etc.

1. private property subclass the parent class can not access, protected property sub-class of the parent class can access

class Person {
	private String name;
	private int age;
	
	public Person(String name, int age) {
		this.name = name;
		this.age = age;
	}
	
	public Person(String name) {
		this.name = name;
		age = 0;
	}
	
	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 String say () {
		return (this.name+" is saying sth");
	}
}

class Student extends Person {
	public Student(String name, int age) {
		super(name, age);
		// TODO Auto-generated constructor stub
	}
	
	public Student(String name) {
		super(name);
	}

	private String school;

	public String getSchool() {
		return school;
	}

	public void setSchool(String school) {
		this.school = school;
	}
	
}
public class T2 {
	
	public static void main(String [] args) {
		Student xiaoming = new Student("xiaoming", 21);
		xiaoming.say();
		System.out.print(xiaoming.getName()+" in "+xiaoming.getAge()+" states: "+xiaoming.say());
	}
}

operation result:

Use protected:

class Person {
	private String name;
	private int age;
	
	protected Person(String name, int age) {
		this.name = name;
		this.age = age;
	}
	
	protected Person(String name) {
		this.name = name;
		age = 0;
	}
	
	protected String getName() {
		return name;
	}
	protected void setName(String name) {
		this.name = name;
	}
	protected int getAge() {
		return age;
	}
	protected void setAge(int age) {
		this.age = age;
	}
	protected String say () {
		return (this.name+" is saying sth");
	}
}

class Student extends Person {
	public Student(String name, int age) {
		super(name, age);
		// TODO Auto-generated constructor stub
	}
	
	public Student(String name) {
		super(name);
	}

	private String school;

	public String getSchool() {
		return school;
	}

	public void setSchool(String school) {
		this.school = school;
	}
	
}
public class T2 {
	
	public static void main(String [] args) {
		Student xiaoming = new Student("xiaoming", 21);
		xiaoming.say();
		System.out.print(xiaoming.getName()+" in "+xiaoming.getAge()+" states: "+xiaoming.say());
	}
}

operation result:

Subclasses can still access protected by a parent class the parent class to private property, the normal still

2. Subclasses override inherited methods

Subclasses that override the parent class will inherit the parent class method body obtained modified method, while the remainder of the method is unchanged (return value, the parameter list, method name, ...)

The new method is a method to get rewritten, while restricted access, access can not be reduced subclass method

public> default (without modifier)> protected> private

Note that this does not make sense to consider private, as a subclass of itself can not inherit private methods of the parent class, even if the same definition methods, subclass own new definition can only be considered, but can not be considered rewrite, so just look at the front the three on it

public> default (without modifier)> protected

class Person {
	private String name;
	private int age;
	
	protected Person(String name, int age) {
		this.name = name;
		this.age = age;
	}
	
	protected Person(String name) {
		this.name = name;
		age = 0;
	}
	
	protected String getName() {
		return name;
	}
	protected void setName(String name) {
		this.name = name;
	}
	protected int getAge() {
		return age;
	}
	protected void setAge(int age) {
		this.age = age;
	}
	protected String say () {
		return (this.name+" is saying sth");
	}
}

class Student extends Person {
	public Student(String name, int age) {
		super(name, age);
		// TODO Auto-generated constructor stub
	}
	
	public Student(String name) {
		super(name);
	}

	private String school;

	public String getSchool() {
		return school;
	}

	public void setSchool(String school) {
		this.school = school;
	}
	
	public String say() {
		return getName() + " said something";
	}
	
}
public class T2 {
	
	public static void main(String [] args) {
		Student xiaoming = new Student("xiaoming", 21);
		xiaoming.say();
		System.out.print(xiaoming.getName()+" in "+xiaoming.getAge()+" states: "+xiaoming.say());
	}
}

Output:

The above subclasses override the parent class method, and access to modify the public by the protected, expanded the mandate of

It is configured to rewrite the relationship between the sub-classes and superclasses, only the method in the same class overloading

In addition to rewriting method, you can also override variables, hidden variables of the parent class that only calls the parent class method when appropriate, will access the variables of the parent class

Otherwise it is a subclass of variable access rewritten

class Person {
	private String name;
	private int age;
	String sex = "secret";
	protected Person(String name, int age) {
		this.name = name;
		this.age = age;
	}
	
	protected Person(String name) {
		this.name = name;
		age = 0;
	}
	
	protected String getName() {
		return name;
	}
	protected void setName(String name) {
		this.name = name;
	}
	protected int getAge() {
		return age;
	}
	protected void setAge(int age) {
		this.age = age;
	}
	protected String say () {
		return (this.name+" is saying sth");
	}
	
	public String getOldSex() {
		return sex;
	}
}

class Student extends Person {
	String sex = "male ";
	public Student(String name, int age) {
		super(name, age);
		// TODO Auto-generated constructor stub
	}
	
	public String getSex() {
		return sex;
	}
	
	public Student(String name) {
		super(name);
	}

	private String school;

	public String getSchool() {
		return school;
	}

	public void setSchool(String school) {
		this.school = school;
	}
	
	public String say() {
		return getName() + " said something";
	}
	
}
public class T2 {
	
	public static void main(String [] args) {
		Student xiaoming = new Student("xiaoming", 21);
		xiaoming.say();
		System.out.print(xiaoming.getOldSex()+" "+xiaoming.getSex());
	}
}

Output:

GetOldSex call the parent class's method () property -secret access to sex parent, call the subclass getSex () method to access a subclass overrides of sex attributes -male

3. The process constructor succession

A constructor is a special discussed separately:

class Person {
	private String name;
	private int age;
	String sex = "secret";
	protected Person(String name, int age) {
		this.name = name;
		this.age = age;
		System.out.println("Father constructor 1th");
	}
	
	protected Person(String name) {
		this.name = name;
		age = 0;
		System.out.println("Father constructor 2th");
	}
	
	protected String getName() {
		return name;
	}
	protected void setName(String name) {
		this.name = name;
	}
	protected int getAge() {
		return age;
	}
	protected void setAge(int age) {
		this.age = age;
	}
	protected String say () {
		return (this.name+" is saying sth");
	}
	
	public String getOldSex() {
		return sex;
	}
}

class Student extends Person {
	String sex = "male ";
	public Student(String name, int age) {
		super(name, age);
		System.out.println("child constructor 2th");
		// TODO Auto-generated constructor stub
	}
	
	public String getSex() {
		return sex;
	}
	
	public Student(String name) {
		super(name);
		System.out.println("child constructor 1th");
	}

	private String school;

	public String getSchool() {
		return school;
	}

	public void setSchool(String school) {
		this.school = school;
	}
	
	public String say() {
		return getName() + " said something";
	}
	
}
public class T2 {
	
	public static void main(String [] args) {
		Student xiaoming = new Student("xiaoming", 21);
		xiaoming.say();
		System.out.print(xiaoming.getOldSex()+" "+xiaoming.getSex());
	}
}

Output:

We can see, first call the parent class constructor, and then call their own subclass constructor

In the example of the time, need to first initialize the parent class attributes, if no explicit call super () keyword

Either an error, or because there is no need to initialize the property without an error, then it is implicitly a super keyword

For the properties of the parent class is initialized

4. The two keywords this and super

this and super role is basically the same keywords

The only difference is the scope, the this call this class constructors and properties, or to refer to the current object with

super call attributes and methods of the parent class (including constructor)

1.this use

class Person {
	private String name;
	private int age;
	String sex = "secret";
	protected Person(String name, int age) {
		this(name);
		this.age = age;
		System.out.println("Father constructor 1th");
	}
	
	protected Person(String name) {
		this.name = name;
		System.out.println("Father constructor 2th");
	}
	
	protected String getName() {
		return name;
	}
	protected void setName(String name) {
		this.name = name;
	}
	protected int getAge() {
		return age;
	}
	protected void setAge(int age) {
		this.age = age;
	}
	protected String say () {
		return (this.name+" is saying sth");
	}
	
	public String getOldSex() {
		return sex;
	}
}

class Student extends Person {
	String sex = "male ";
	public Student(String name, int age) {
		super(name, age);
		System.out.println("child constructor 2th");
		// TODO Auto-generated constructor stub
	}
	
	public String getSex() {
		return sex;
	}
	
	public Student(String name) {
		super(name);
		System.out.println("child constructor 1th");
	}

	private String school;

	public String getSchool() {
		return school;
	}

	public void setSchool(String school) {
		this.school = school;
	}
	
	public String say() {
		return getName() + " said something";
	}
	
}
public class T2 {
	
	public static void main(String [] args) {
		Student xiaoming = new Student("xiaoming", 21);
		xiaoming.say();
		System.out.print(xiaoming.getOldSex()+" "+xiaoming.getSex());
	}
}

Used above this (name) of the call to implement the class constructor, using such forms this.age = age solve ambiguity

this usage summary:

Use this configuration alias represents the class itself, a parameter which distinguished constructor call

Call each other can be achieved between the constructor

For example, a class A constructor A () {}

A (int i) {this.i = i;}

A (int i, int j) {this.i = i; this.j = j;}

Then the third constructor can be simplified to :

A (int j) {this(int i); this.j = j;}

Note that when using the constructor call, the this statement must be written in the first line

If a return this;

Like writing, it is to use this to refer to the current object usage, return the object itself

example:

class Person {
	private String name;
	private int age;
	String sex = "secret";
	protected Person(String name, int age) {
		this(name);
		this.age = age;
		System.out.println("Father constructor 1th");
	}
	
	protected Person(String name) {
		this.name = name;
		System.out.println("Father constructor 2th");
	}
	
	protected String getName() {
		return name;
	}
	protected void setName(String name) {
		this.name = name;
	}
	protected int getAge() {
		return age;
	}
	protected void setAge(int age) {
		this.age = age;
	}
	protected String say () {
		return (this.name+" is saying sth");
	}
	
	public String getOldSex() {
		return sex;
	}
	
	public Person getCurrentObj () {
		return this;
	}
}

class Student extends Person {
	String sex = "male ";
	public Student(String name, int age) {
		super(name, age);
		System.out.println("child constructor 2th");
		// TODO Auto-generated constructor stub
	}
	
	public String getSex() {
		return sex;
	}
	
	public Student(String name) {
		super(name);
		System.out.println("child constructor 1th");
	}

	private String school;

	public String getSchool() {
		return school;
	}

	public void setSchool(String school) {
		this.school = school;
	}
	
	public String say() {
		return this.getName() + " said something";
	}

}
public class T2 {
	
	public static void main(String [] args) {
		Student xiaoming = new Student("xiaoming", 21);
		System.out.println(xiaoming.getCurrentObj().say());
	}
}

super keyword usage 1:

Call the parent's method:

1. Constructor

2. The common method

class Person {
	private String name;
	private int age;
	String sex = "secret";
	protected Person(String name, int age) {
		this(name);
		this.age = age;
		System.out.println("Father constructor 1th");
	}
	
	protected Person(String name) {
		this.name = name;
		System.out.println("Father constructor 2th");
	}
	
	protected String getName() {
		return name;
	}
	protected void setName(String name) {
		this.name = name;
	}
	protected int getAge() {
		return age;
	}
	protected void setAge(int age) {
		this.age = age;
	}
	protected String say () {
		return (this.name+" is saying sth");
	}
	
	public String getOldSex() {
		return sex;
	}
	
	public Person getCurrentObj () {
		return this;
	}
}

class Student extends Person {
	String sex = "male ";
	public Student(String name, int age) {
		super(name, age);
		System.out.println("child constructor 2th");
		// TODO Auto-generated constructor stub
	}
	
	public String getSex() {
		return sex;
	}
	
	public Student(String name) {
		super(name);
		System.out.println("child constructor 1th");
	}

	private String school;

	public String getSchool() {
		return school;
	}

	public void setSchool(String school) {
		this.school = school;
	}
	
	public String say() {
		return "super.say() = "+super.say()+"\n"+this.getName() + " said something"+"\n"+"super.sex = "+super.sex;
	}

}
public class T2 {
	
	public static void main(String [] args) {
		Student xiaoming = new Student("xiaoming", 21);
		System.out.println(xiaoming.getCurrentObj().say());
		
	}
}

Examples of the above code super (name, age) is called the parent class constructor

The following super.say () is a normal call parent class

Further, super key tone subclasses can also be used to rewrite the parent class keywords

For example, the above code is used to access a sex super.sex properties of the parent class

super and this note:

1.super and this can not be at the same time on the first line (when calling the constructor), so super and this keyword can not be used simultaneously

2. not when calling the constructor, there is no limit placed on the first line, it can be used simultaneously

 

 

 

Guess you like

Origin blog.csdn.net/chenhanxuan1999/article/details/91489537