Day10 inheritance in the Java language (i)
I. Overview of Succession:
Inheritance: What is inheritance, the inheritance in the program is different from the inheritance in the life, after inheriting in the program, your parent class and your child class also have a member variable. So why do we inherit medicine? Because we have a lot of code to rewrite when we're programming, which makes our code more cumbersome and error-prone. So define a class as the parent class, define some common attributes in this class, and when a class needs to use these methods, you can directly use the member variables in the parent class by using the extends keyword directly. Convenient and quick. Therefore, who inherits the parent class who is the subclass of this parent class. There is some kind of connection between them. Let's take a concrete look at inheritance. Note: Inheritance is the core knowledge of Java, focusing on learning.
Ii. Inheritance (Extends):
1. Define an inherited syntax:
Modifier class Subclass Class Name extends parent class Name {//class definition part}
This is an inherited syntax pattern, remembering the keyword extends.
2. Parent class (Super Class):
Parent class We do not make any requirements or restrictions, as long as some public use of some variables, methods, constructors, and so on can be defined in a class. Only after this class is inherited, his character is immediately transformed from a normal class to a parent class.
3. Subclass (base class or derived class):
The definition of a subclass means that when you inherit a class with an inherited keyword, you automatically become a subclass of the class, and you have an inherited relationship with the class. All of the variables, methods, and constructors defined within the parent class can be invoked directly, excluding variables and methods that use private modification.
4. Overwrite overrides: Overriding this concept is important, typically subclasses overriding methods in the parent class, why override methods in the parent class? Because some of the methods defined in the parent class do not satisfy the requirements of the subclass, add another thing in the subclass to the method defined by the parent class, which makes the subclass more perfect, see example one below. So, the subclass is an extension to the parent class.
In the rewritten syntax, in order to simplify the code is the repetition rate, we directly use "Super." Method name in the parent class to implement override overrides of the parent class method, there is no need to write the method in the parent class again.
5. Call the constructor:
Instance one (to implement a simple inheritance relationship)
package www.com.c3; //Define a parent class (Super Tired): 3 member variables class user{string name;int age;public void display () {System.out.println ("Name:" + name); System.out.println ("Age:" +age);} } //defines a subclass (derived class): 3 member variables//1, complete inheritance with the extends keyword. Inheritance implements code reuse. 2. The subclass is the inheriting class of the parent class. 3. The private member subclass of the parent class cannot be inherited. All other specifiers can be inherited. class Student extends User{String password; String gender;//overrides the method of the parent class. The method that overrides the definition of the parent class. This is because the parent class defines a requirement that does not satisfy the subclass. @Override//This is a rewrite of the annotations. You can not write, Public void display () {//Use Super to display a member method that is already defined in the parent class. Thus realize the code reuse Super.display (); System.out.println ("Password:" +password); System.out.println ("Gender:" +gender);} } //Main class, public class extends01 {public static void main (String[] args) {//creates an instance of the subclass. Student st = new student ();//Assignment st.name = "Zs";st.age = 15;st.gender = "male";st.password = "123456";//print out a bit st.display ();}} -------------------------------------------------------------------displayed in Control Panel: Name: ZS Age: 15 Password: 123456 Sex: Male
This article from the "Program Ape" blog, reproduced please contact the author!
----inheritance in the Java language (i)