Java Language Learning summarizes the advanced version of the concept of inheritance and its use

inherit

Java object-oriented has three characteristics: encapsulation, inheritance, polymorphism , inheritance, polymorphism is a prerequisite, not inherited no polymorphism.
There are several classes that inherit properties, abstracted into a class, when you define a new class can inherit variables or method calls directly shared. Eliminating the need to repeat the definition of the same trouble codes
characteristic inheritance relationship is:

  1. Subclasses can have a negative kind of "content"
  2. Subclasses can also have their own proprietary content

The parent class is called a base class or super class, sub-class is called a derived class , every class can be regarded as the parent class.

Inheritance Definition Format

public class name of the subclass extends the parent class name {
//
}

Sample code:

  1. The establishment of the parent class

public class ExampleFather {
	public void method(String son) {
		System.out.println("这是父类的方法,被" + son + "继承了");
	}

	public static void main(String[] args) {
		// TODO Auto-generated method stub

	}
  1. The establishment of the first sub-categories:
public class ExampleSonFirst extends ExampleFather {
	public void methodSonFirst() {
		System.out.println("SonFirst类自己的方法");
	}

	public static void main(String[] args) {
		

	}

}
  1. Establishing a second sub-category
public class ExampleSonSecond extends ExampleFather{
	public void methodSonSecond() {
		System.out.println("这是SonSecond类自己的方法");
	}

	public static void main(String[] args) {
		// TODO Auto-generated method stub

	}

}
  1. The establishment of debugging class and subclass definition object
public class TestExample {

	public static void main(String[] args) {
		ExampleSonFirst sonfirst = new ExampleSonFirst();
		sonfirst.method("SonFirst");
		sonfirst.methodSonFirst();
		
		ExampleSonSecond sonsecond = new ExampleSonSecond();
		sonsecond.method("SonSecond");
		sonsecond.methodSonSecond();
	}
}

Output:
Output

Parent and child class member access rules for duplicate names

Member variables the same name

In the parent-child class inheritance, if the member variables of the same name, subclass object is created, accessed in two ways:

Subclass object directly access member variables : who is left of the equal sign, it is a priority with anyone, not looking up the
indirect access to member variables by members of the method : This method belongs to whom, by whom on priority, then there is no looking up

Code Example:
establishing parent-child class as follows

public class Fu {
	int numFu = 100;
	int num = 2000;
	public void methodFu() {
		System.out.println(num);
	}
}
public class Zi extends Fu{
	int numZi=300;
	int num=1000;
	public void methodZi() {
		System.out.println(num);
	}
}

Debug definition object class, calling methods variable code as follows:

public class TestFuZi {

	public static void main(String[] args) {
		Fu fu = new Fu();
		System.out.println(fu.numFu);
		System.out.println("=================");
		
		Zi zi = new Zi();
		System.out.println(zi.numZi);
		System.out.println(zi.numFu);
		System.out.println("=================");

		System.out.println(zi.num);
		System.out.println("=================");
		
		zi.methodZi();
		zi.methodFu();
		fu.methodFu();

	}

}

Output:
Output

Member method of the same name

In the parent-child inheritance relationship among classes, create sub-class object, a member of the rules of access methods :
Who created object is on priority with whom, if not then look up

Note: Whether a member or member variables method, if no parent is looking up, never looking down subclass

Covering methods inherited rewrite

Rewriting (override):
In the hierarchy, the same method name, parameter list, too.
Overwritten features: create a subclass of objects, methods used on priority subclass

** @override **: written before the method for detecting a valid right is not overwritten. This is optional, but recommended as long as the method is to rewrite all write, rewrite detection method is correct.

The return value must be less than the subclass method equal the parent class method returns the value range
java.lang.Object class is the common parent of all classes of the highest

super keyword

Subclass with direct access to the super keyword in the parent class. When the parent and child classes methods have the same name may be used.

In the constructor inherited access features

Inheritance, the access characteristics of the parent-child class constructor:

  1. Constructor of a subclass which has a default implicit "super" to call, so the parent class constructor must be the first call, the sub-class constructor after the execution.
  2. We can call the parent class overrides constructed by super keyword subclass constructor.
    Subclasses must call the parent class constructor, do not write the gift super (), written by writing the specified call super, super can only write one.
    --------------------------------
Published 50 original articles · won praise 3 · Views 5171

Guess you like

Origin blog.csdn.net/Ace_bb/article/details/104074597