Java basics|Inheritance

Inheritance keywords: extends and super

extends

class Parent
{
    
    
	...
}

子类
class child extends Parent
{
    
    
	...
}

super
When the public property or method of the parent class is called in the subclass, the keyword super should be used

super(name,age);

super.方法();

1. When the subclass calls the properties of the parent class, the following error will be displayed if super is not used
Insert picture description here
. 2. When the subclass calls the method of the parent class, the following error will be displayed without using **super.** The
Insert picture description here
subclass method calls the parent class method When you want to super. method ()
super. method () can not be used outside the method of the subclass

In the main function, the subclass object can directly call the superclass method.
example

class parent
{
    
    
	String name;
	int age;
	parent(String name,int age)
	{
    
    
		this.name=name;
		this.age=age;
	}
	public void speak()
	{
    
    
		System.out.println("我的名字"+name+",今年我"+age+"大了");
	}
	public void out()
	{
    
    
		System.out.println("调用父类方法");
	}
	/*private void speak_pri()
	{
		System.out.println("私有化输出:"+"我的名字"+name+",今年我"+age+"大了");
	}*/

	protected void speak_pro()
	{
    
    
		System.out.println("保护输出:"+"我的名字"+name+",今年我"+age+"大了");
	}
}
class Child extends parent
{
    
    
	String school;
	Child(String name,int age,String school)
	{
    
    
		super(name,age);
		this.name=name;
		this.age=age;
		this.school=school;
	}
	public void study()
	{
    
    
		System.out.println("我在"+school+"读书");
		super.speak();
		super.out();
		super.speak_pro();
	}
}
public class Inherit
{
    
    
	public static void main(String[] args)
	{
    
    
		Child c=new Child("张三",18,"电子大学");
		c.study();
		/*c.speak();
		c.out();*/
		parent p=new parent("李四",22);
		p.speak();
		p.speak_pro();
	}
}

Inheritance restrictions

Limitation 1: There is no multiple inheritance, only multiple inheritance.
If class C wants to inherit the functions of class A and class B at the same time

class A
{
    
    }
class B
{
    
    }
class C extends A B//这样是不行的
{
    
    }

The above is wrong, multiple inheritance cannot be achieved

A can only be inherited by B, and C can then inherit B, that is, a class can only be inherited once at a time.
Inheriting multiple classes can be accomplished through multiple inheritance
. Under normal circumstances, it is not recommended to inherit more than 3 levels at most


Limitation 2: The private members of the parent class cannot be directly inherited by the subclass, and can only be called indirectly. The
above test is not possible, because the subclass does not belong to the parent class and is a new class


Restriction 3: When a subclass is instantiating an object, the data members inherited from the parent class must first be initialized by calling the constructor of the parent class.
What does restriction 3 mean? Remember that when constructing the method

你调用父类的数据,其数据具体值必须在父类中的构造函数生成,也就是子类构造方法调用父类数据,此数据在父类中要先诞生

Look at the code to understand the construction method of the
parent class

class parent
{
    
    
	String name;
	int age;
	parent(String name,int age)
	{
    
    
		this.name=name;
		this.age=age;
	}
}

Subclass construction method

class Child extends parent
{
    
    
	String school;
	Child(String name,int age,String school)
	{
    
    
		super(name,age);
		this.name=name;
		this.age=age;
		this.school=school;
	}
}

There is no problem writing the above two paragraphs like this, but! If the code for initializing name and age in the parent class is deleted, an error will occur

class parent
{
    
    
	String name;
	int age;
}

Subclass

class Child extends parent
{
    
    
	String school;
	Child(String name,int age,String school)
	{
    
    
		super(name,age);
		this.name=name;
		this.age=age;
		this.school=school;
	}
}

Insert picture description here
Therefore, the data members of the parent class called by the constructor of the subclass must be initialized in the constructor of the parent class! ! !

**Put the focus! ! ! **If I call the data member of the parent class every time, will it be too troublesome to write a related construction method every time? Are there other forms?
some

class Person
{
    
    
	String name;
	int age;
}

class Student extends Person
{
    
    
	String School;
	Student(String name,int age,String School)
	{
    
    
		super.name=name;
		super.age=age;
		this.School=School;
	}

	public String talk()
	{
    
    
		//return super.talk()+"I am from:"+this.School;
		return "I am from:"+this.School;
	}

}
public class InheritFFF
{
    
    
	public static void main(String[] args)
	{
    
    
		Student S=new Student("jack",24,"zhongg");
		System.out.println(S.talk());
	}
}

Visible:
direct super. data members, no need to initialize the constructor in the parent class

		super.name=name;
		super.age=age;

And in the subclass:

		super(name,age);
		this.name=name;
		this.age=age;

Need to be in the parent class: initialize the data member of the constructor

Person(String name,int age)
	{
    
    
		this.name=name;
		this.age=age;
	}

At the beginning I thought it was all like this, ordinary functions, outside the method; but in fact it is not, the above characteristics only meet the scope of subclass construction method
The following code can be seen, as long as it is not a construction method, you can call it any way! ! !

class A
{
    
    
	String name="张三";
	int age=11;
	int BBB=222;
	int CCC;
	
}
class B extends A
{
    
    
	String name="李四";
	int age=12;
	public B()
	{
    
    
		
		System.out.println(super.BBB);
	}
	public void print()
	{
    
    
		System.out.println(name);
		System.out.println(age);
		System.out.println(super.name);
		System.out.println(super.age);
	}
	public void test(int c)
	{
    
    
		CCC=c;
		System.out.println(CCC);
	}
}

public class InheritTTT
{
    
    
	public static void main(String[] args)
	{
    
    
		B b=new B();
		b.print();
		b.test(33);
	}
}

Limitation 4: The method modified by final cannot be overwritten by subclasses, and the class modified by final cannot be inherited

Final
fina is called "terminator" in Java; when final modifies a method of a class, no secondary modification is allowed. When finalizing a class, it indicates that this class cannot be inherited.

Class override

When the subclass and the parent data member have the same name

class A
{
    
    
	String name="张三";
	int age=11;
}
class B extends A
{
    
    
	String name="李四";
	int age=12;
	public void print()
	{
    
    
		System.out.println(name);
		System.out.println(age);
		System.out.println(super.name);
		System.out.println(super.age);
	}
}
public class InheritTTT
{
    
    
	public static void main(String[] args)
	{
    
    
		B b=new B();
		b.print();
	}
}

Insert picture description here
If super is not added, the parent data member will be overwritten.
In the subclass's construction method to call the data members of the parent class, the data members of the parent class need to be initialized first,
and the subclasses call the data members of the parent class at other times, without this operation!


When the method of the parent class is overridden by the subclass, how to call the method of the parent class?
E.g:

class Person
{
    
    
	String name;
	int age;
	public String talk()
	{
    
    
		return "I am: "+this.name+"I am"+this.age+"years age";
	}
}

class Student extends Person
{
    
    
	String School;
	public Student(String name,int age,String School)//这个不是需要父类初始化吗?
	{
    
    
		super.name=name;
		super.age=age;
		this.School=School;
	}

	public String talk()
	{
    
    
		return "I am from:"+this.School;
	}

}

public class InheritFFF
{
    
    
	public static void main(String[] args)
	{
    
    
		Student S=new Student("jack",24,"zhongg");
		System.out.println(S.talk());
	}
}

Result:
Insert picture description here
but add super.talk()
to return.
Insert picture description here
Insert picture description here
This is an ordinary method, so no super

Guess you like

Origin blog.csdn.net/weixin_46096297/article/details/114644818
Recommended