Java study notes six (inheritance and related)

Object-oriented features of the two: inheritance

Benefits of inheritance:
① reduce code redundancy, improving the reusability of code
② facilitate extended function
③ provided preconditions for the following polymorphic

Inheritance format:
class A the extends B {}
wherein A is a subclass derived class, subclass
B is the parent class, the superclass, the base class, the superclass
reflect inheritance : Once a subclass of A inherited from a parent Classes B, considered subclass a got all the attributes and methods of the parent class B declared; in particular, the parent class declared properties and methods private, the subclasses after inheriting the parent class, still think subclass inherits the parent class private structure, but because of the impact of the package, it can not be called directly in the subclass.
After the subclass inherits the parent class may also declare their own unique properties and methods to achieve extension.

Provisions on inheritance :
1, is the Java single inheritance, a subclass can have only one direct parent;
2, direct subclass inherits the parent class called direct parent, indirect inherits the parent class called indirect parent; after the subclass inherits the parent class, you get all the attributes and methods of direct and indirect parent declared in the parent class;
3, although a subclass can only inherit a parent, but a parent can be inherited by subclasses;

4, if no parent explicitly declare a class, then the class will inherit from java.lang.Object class, all classes inherit directly or indirectly in the java.lang.Object class, that is all java classes have attributes and methods declared in the class java.lang.Object.

package com.atguigu.java;

public class Creature {
	
	public void breath(){
		System.out.println("呼吸");
	}
	
}
package com.atguigu.java;

public class Person extends Creature{//Person中虽然没有定义breath方法
									//但从Creature中继承得到了该方法
	
	String name;
	private int age;
	
	public Person(){
		
	}
	
	public Person(String name,int age){
		this.name = name;
		this.age = age;
	}
	
	public void eat(){
		System.out.println("吃饭");
		sleep();
	}
	
	private void sleep(){
		System.out.println("睡觉");
	}

	public int getAge() {
		return age;
	}

	public void setAge(int age) {
		this.age = age;
	}
	
	
}

The method of rewriting: (override / overwrite)

Rewriting : After subclass inherits the parent class, method of the same name can be overridden with the parameters parent class.
Application : After the rewrite, if you create a subclass of the object, call the method with the same name as the parameter of the child the parent class through an object subclass, the subclass method is overridden actually executed.
Rewriting the predetermined :
Method statement: permission modifier return type method name (parameter list) throws Exception {} type
points Note :
① Method subclasses and override the method name and parameter list for a parent class the method of the same name to be rewritten and a method parameter list;
permissions override method ② subclass modifier is rewritten in the parent class is not less than; special circumstances, a subclass can override the parent class declared as private authority Methods.
Return Type void, the subclass is the parent class ③ override method overridden method return type can only be void.
④ parent class is overridden method return type is a type A, a method override the return type of the subclass may be of type A and type A subclass.
⑤ parent class method returns the value to be overwritten is the basic type of data types, the method subclasses can also override the same type of basic data types.
⑥ type of exception thrown subclass override is not greater than the parent class is thrown exception types overridden methods (particularly when speaking into exception handling).
⑦ subclass and of the same name in the parent class with parameters either declare the non-static (consider rewriting), or are declared static (not rewrite).

public class Student extends Person{

	String major;
	
	public Student(){
		
	}
	public Student(String major){
		this.major = major;
	}
	
	public void study(){
		System.out.println("学习。专业是:" + major);
	}
	
	//对父类中的eat()进行了重写
	public void eat(){
		System.out.println("学生应该多吃有营养的食物");
	}
	
	public void show(){
		System.out.println("我是一个学生");
	}
	
	public String info(){
		return null;
	}
	
//	public int info1(){
//		return 1;
//	}
	
//	public void walk(int distance){
//		System.out.println("重写的方法");
//	}
	
	
	public void walk(int distance) {
		System.out.println("重写的方法");
	}

}

Subclass object instance of the entire process

1, from the results, :( inheritance)
subclass inherits the parent class after it acquired the property or method declared in the parent class.
Create an object subclass, in the heap space, it will load all the properties declared in the parent class.

2, from a process point of view :
When we create a subclass object constructor by a subclass, we will call the direct or indirect parent class constructor, and then call the parent class constructor of the parent class, until the call ... Until the class java.lang.Object hollow-argument constructor. Because of all the structures of the parent class loader, so I can see the memory in a structured parent class, subclass object can be called to consider.
Clear : While create a subclass of object, call the constructor of the parent class, but from beginning to end to create an object too, is the new sub-class objects.

Use super keyword

. 1, super understood as: parent class
2, can be used to call super: properties, methods, constructors
3, the use of super : call properties and methods

  • 3.1 we can subclass method or the constructor. By using the "Super. Properties" or "Super. Method" approach, property or method call the parent class explicitly declared. However, under normal circumstances, we are accustomed to omit "super."
  • 3.2 Special case: When the parent class and subclass defines the properties of the same name, we want to call the parent class properties declared in a subclass, you must explicitly use ". Super property" approach, indicating that the call It is a property of the parent class declaration.
  • 3.3 Special case: When a subclass overrides a parent class in the future, we want the parent class method is overridden method call subclass, you must explicitly use the "super method." Manner, shows that the method is called the parent class to be rewritten.

4. Super call the constructor

  • 4.1 We can explicitly use "super (parameter list)" method, call the parent declared in the constructor subclass specified in the constructor
  • 4.2 "super (parameter list)," the use, must be declared in the first line of the subclass constructor!
  • 4.3 We constructor class, for in "this (parameter list)" or "super (parameter list)" can only choose one, not both
  • 4.4 in the first row constructor, no explicit statement "the this (parameter list)" or "Super (parameter list)", the default is the parent class is called a hollow argument constructor: super ()
  • 4.5 plurality of class's constructor, at least a class constructor uses "Super (parameter list)", call the parent class constructor
public class SuperTest {
	public static void main(String[] args) {
		
		Student s = new Student();
		s.show();
		
		System.out.println();
		
		s.study();
		
		Student s1 = new Student("Tom", 21, "IT");
		s1.show();
		
		System.out.println("************");
		Student s2 = new Student();
		
	}
}

Published 11 original articles · won praise 14 · views 5454

Guess you like

Origin blog.csdn.net/ssnszds/article/details/104492124