Java Language Learning Summarizes the Concepts of Advanced Text Inheritance and Its Use

Posted by labourstart on Thu, 23 Jan 2020 08:05:37 +0100


Java has three main object-oriented features: encapsulation, inheritance, polymorphism, inheritance is a prerequisite for polymorphism, there is no polymorphism without inheritance.
Inheritance refers to the abstraction of attributes common to several classes into a class that, when defining a new class, can directly inherit and invoke common variables or methods.Remove the hassle of redefining the same code
Inheritance relationships are characterized by:

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

The parent class is called a base class or superclass, and the child class is called a derived class. Each class can be considered a parent.

Inheritance Definition Format

public class subclass name extends parent class name {

Sample code:

  1. Create parent class
public class ExampleFather {
	public void method(String son) {
		System.out.println("This is the method of the parent class and is" + son + "Inherited");

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

  1. Create the first subclass:
public class ExampleSonFirst extends ExampleFather {
	public void methodSonFirst() {
		System.out.println("SonFirst Class's own method");

	public static void main(String[] args) {


  1. Create a second subclass
public class ExampleSonSecond extends ExampleFather{
	public void methodSonSecond() {
		System.out.println("This is SonSecond Class's own method");

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


  1. Create debug classes and define objects of subclasses
public class TestExample {

	public static void main(String[] args) {
		ExampleSonFirst sonfirst = new ExampleSonFirst();
		ExampleSonSecond sonsecond = new ExampleSonSecond();

The output is as follows:

Access rules when parent and child class members have the same name

Member variable rename

In the inheritance relationship of a parent-child class, if the member variable has a duplicate name, there are two ways to access subclass objects when they are created:

Access member variables directly through subclass objects: Whoever is to the left of the equal sign will take precedence over others, and if not, look up
Access member variables indirectly through the member method: Whoever the method belongs to, takes precedence over, and looks up if it doesn't

Code example:
Create the following parent and child classes

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

The debug class defines the object and calls the variable method code as follows:

public class TestFuZi {

	public static void main(String[] args) {
		Fu fu = new Fu();
		Zi zi = new Zi();




Output results:

Member Method Rename

In the inheritance relationship between parent and child classes, create subclass objects and access rules for member methods:
Whose object is created will take precedence over others, and if not, look up

Note: Whether member methods or member variables are not all up-looking parent classes, never down-looking child classes

Override override of methods in inheritance

In an inheritance relationship, the method has the same name and the parameter list is the same.
Override the override feature: Subclass objects are created, subclass methods are preferred

** @override **: Write in front of the method to check if the correct override is valid.This is optional, but it is recommended that you check if the override method is correct as long as it is written.

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

super keyword

Method that directly accesses the parent class in a subclass using the super keyword.You can use this when the parent and child classes have methods with duplicate names.

Access characteristics of constructors in inheritance

Access characteristics of parent-child class constructors in inheritance relationships:

  1. There is a default implied "super" call in the subclass construction method, so it must be the parent construction invoked before the subclass construction executed.
  2. The super keyword can be used to invoke parent class overload constructs from subclass constructs.
    Subclasses must call the parent construction method, give super() if they don't write, write the specified super call if they do, and super can only write one.
Published 50 original articles, won approval 3, visited 5107
Private letter follow

Topics: Java less