------Java Training, Android training, iOS training,. NET training, look forward to communicating with you! -------
Inherited:
1. When a class contains all the variables of another class, but the other class does not contain all the variables and methods of the other class, the representation of a smaller range can be the parent of the other class.
The collection represents: a belongs to the b,b does not belong to A,a can be the parent class of B, B inherits a
2. You cannot inherit to simplify your code just to get the functionality of other classes.
3. The owning relationship between classes and classes must be inherited, and the owning relationship looks at the previous collection
Characteristics of Inheritance:
1. Multiple inheritance is not supported, only single inheritance is supported:
Many inherited words are prone to security risks:
Class A{demo{system.out.println ("A");}} Class B{demo{system.out.println ("B");}} Class C extends a,b{c C = new C (); C.demo ();}
At this point, C will have a selection error, do not know whether to call a or call B.
2. Inheritance improves the reusability of code
3. There is a relationship between classes and classes, and with this relationship, there is a polymorphic nature.
Class Person{int age; String name;} Class Student extends Person{void study () {System.out.println ("good Student");}} Class work extends person{void work () {System.out.println ("good Worker");}} public class Extendsdemo{public static void Main (string[] args) {student s = new student (); s.name = "Archer"; s.age = 21; System.out.println ("Age:" +age+ "Name:" +name), S.work (); Work w = new work (); W.work ();}}
JAva supports multilayer inheritance. An inheritance system.
How do you use the functionality of an inheritance system?
To use the system, first look at the description of the parent class of the system, as the parent class defines the system's communist functionality.
By understanding the common features, you can know the basic functions of the system.
Then the system can be used basically.
So, in the case of a specific invocation, what is the object that will create the most subclass?
One is because it is possible that the parent class cannot create an object.
The second is to create subclass objects that can use more functionality, including basic and also unique.
A simple sentence: Check the parent class feature to create a subclass object usage feature.
When a subclass and a parent class appear, what are the characteristics of the members in the class:
1. Member variables
When the same property appears in the child parent class, the object of the subclass type is called, and the value is the property value of the child class
Class Student{int num = 4;study () {System.out.println ("good Study");} Show () {System.out.println (num);}} Class Smallstudent extends Student{int num = 5;show () {System.out.println (num);}} public class Extendsdemo{public static void Main (String args[]) {new Smallstudent (). Show ();//output value is 5, not 4}}
if you want to invoke the property values in the parent class, you need to use the keyword super
The difference between this and super:
This: Represents an object reference of this class type
Super: Represents a reference to the memory space in the parent class to which the subclass belongs
Note that a member variable of the same name is not normally present in the word parent class, because the parent class is defined and the subclass is no longer defined and inherited directly.
Super keyword.
Class Student{int num = 4;study () {System.out.println ("good Study");} Show () {System.out.println (num);}} Class Smallstudent extends Student{int num = 5;show () {System.out.println (super.num);//output is the value of the parent class, which is 4}}public class Extendsdemo{public static void Main (String args[]) {new Smallstudent (). Show ();//output has a value of 4}}
2. member function
When the same method appears in the child's parent class, the object that creates the subclass runs the methods in the subclass as if the methods in the parent class are overwritten, so this is another feature of the function-overriding (overriding)
when do I use overrides?
when the functionality of a class needs to be modified, it can be implemented by overwriting.
3. Constructor
when the subclass constructor runs, the parent class constructor is run, Why is it? The first line in all the constructors of the
subclass actually has a stealth statement super ();
Super (): Represents the constructor of the parent class and calls the constructor in the parent class that corresponds to the argument, and Super (): Is the null argument constructor in the call to the parent class.
Why do you need to call a function in the parent class when the subclass object is initialized, which is why the first line of the subclass constructor joins the Super ()?
because the subclass inherits the parent class, it inherits the data from the parent class, so it's important to see how the parent class initializes its own data, so the child class invokes the constructor of the parent class when the object is initialized. This is the instantiation process for subclasses.
Class Student{student () {System.out.println ("student");}} Class Smallstudent extends Student{smallstudent () {System.out.println ("smallstudent");}} public class Extendsdemo{public static void Main (String args[]) {smallstudent s = new smallstudent (); s.smallstudent ();// The output is student//smallstudent}} so the child class constructor is instantiated by first accessing the constructor of the parent class. Caveats: Class Student{student () {System.out.println ("student");} Student (int n) {System.out.println ("4");}} Class Smallstudent extends Student{smallstudent () {//Here is the Super keyword SYSTEM.OUT.PRINTLN ("smallstudent");} smallstudent (int m) {//here can be the This keyword, or it can be super keyword SYSTEM.OUT.PRINTLN ("5");}} public class Extendsdemo{public static void Main (String args[]) {smallstudent s = new smallstudent (); S.smallstudent ();}} But if the parent does not have a constructor for empty arguments, then you need to manually add super () to the first row of the subclass. Class student{student (int n) {System.out.println ("4");}} Class Smallstudent extends Student{smallstudent () {super (3); System.out.println ("Smallstudent");} smallstudent (int m) {super (4); System.out.println ("5");}} public class Extendsdemo{public static void MAin (String args[]) {smallstudent s = new smallstudent (); S.smallstudent ();}}
Otherwise it will be an error, why must be the first line, because the constructor is to initialize, if placed in the following, the subclass first initialized, the parent class is initialized, the value of the class will be changed, so the initialization of the action must be completed first.
When the method is covered, note the two point:
1 when a subclass overrides a parent class, it must be guaranteed that the permissions of the subclass method must be greater than or equal to the parent class method permission implementation inheritance, otherwise, the compilation fails.
2. When overridden, either static or static, because static can only overwrite static.
The default permission for a function is the package, which is between private and public.
Private, public, protected permission size:
Private: only 1. The function in the class, 2. Its friend function is accessed.
The object of the class cannot be accessed by any other access.
Protected: Can be 1. A function in this class, a function of 2. Subclass, and 3. Its friend function is accessed. But cannot be accessed by objects of that class.
Public: Can be 1. The function in the class, the function of the 2. Subclass, 3. Its friend function is accessed by 4. The object of the class.
Inheritance of a disadvantage, breaking the encapsulation of the class, for some classes, or the function of the class is to be inherited, or is rewritten, how to solve it?
Use keyword: final
Final features:
1. This keyword is a modifier that can be used to modify classes, methods, and variables
2. The final modified class is an eventual class that cannot be overwritten and cannot be inherited
3. The final modified method is an ultimate method that cannot be overridden
4. The final modified variable is a constant and can only be assigned once
Class Student{final Showt () {System.out.println ("student");} This method can not be modified in}class Smallstudent extends Student{smallstudent () {System.out.println ("smallstudent");}} public class Extendsdemo{public static void Main (String args[]) {final PI = 3.14;//This is a bit like the Define in C language, More similar to const smallstudent s = new smallstudent (); S.smallstudent ();}} Abstract: What is abstraction? Not specific, can not understand, abstract class representation in the process of continuous extraction, the common content of the method declaration extraction, but the method is not the same, no extraction, which is extracted to the method, is not specific, need to be specified by the keyword abstract marked, declared as an abstract method. The class in which the abstract method resides must be marked as an abstract class, meaning that the class needs to be modified by the Abstract keyword Class Student{study () {System.out.println ("study");}} Class Smallstudent extends Student{study () {System.out.println ("study 中文版");}} Class Bigstudent extends Student{study () {System.out.println ("study Code");}} Ppublic class Abstractdemo{public static void Main (String args[]) {}} The Bigstudent method is used in both smallstudent and study. However, the specific implementation of the function within the same, and then inherit the student class is meaningless, this time you need to use abstract classes. Abstract class Student{abstract study ();//This time there is only method, but there is no method body}class Smallstudent extends Student{study () { SYSTEM.OUT.PRINTLN ("study 中文版");}} Class Bigstudent extends StUdent{study () {System.out.println ("study Code");}} Ppublic class Abstractdemo{public static void Main (String args[]) {}}
If an abstract class contains multiple abstract methods, the subclass must initialize all of the abstract methods, or else an error will occur.
Features of abstract classes:
1, the abstract method must be in the abstract class.
2, both abstract methods and abstract classes must be decorated with the abstract keyword.
3, abstract classes can not create objects with new. Because it doesn't make sense to invoke an abstract method.
4, abstract methods in the abstract class to be used, the subclass must be replicated all the abstract methods, the child class object call.
If a subclass overrides only a subset of the abstract methods, then the subclass is an abstract class.
Abstract classes and general classes are not much different.
How to describe things, how to describe things, but there are some things that things do not understand.
These uncertain parts, and the function of the thing, need to be clearly present. However, the principal cannot be defined.
Represented by an abstract method.
Abstract analogies generally have multiple abstract functions. is to define an abstract method in a class.
An abstract class cannot be instantiated.
Special: Abstract methods may not be defined in a class, and this simply does not allow the class to establish an object.
The abstract keyword, and which keywords cannot coexist.
Final: A class that is final decorated cannot have subclasses. The class that is modified by the abstract must be a parent class.
Private: Abstract methods in the abstract class, not known by the class, can not be replicated.
And the abstract approach is that it needs to be replicated.
Static: If static can modify the abstract method, then even the object is saved, the direct class name is called.
But abstract methods do not make sense to run.
Is there a constructor in an abstract class?
Yes, an abstract class is a parent class that provides the initialization of an instance to a subclass
Exercise: Employee has name, employee number, salary, manager also belongs to employee, but he has bonus.
Analysis:
1. There must be an employee class
2. The manager belongs to the employee, but not exclusively, he has his own special attributes.
3. Attributes required by the Employee class: Name/id/gongzi
4. Manager attached properties: Bonus
Class Employee{private string name;private string id;private int gongzi;//Initialize Employee (string name, string id, int gongzi) {this.name = Name;this.id = Id;thi.gongzi = Gongzi;} Public abstract work ();//no method body}class Manage extends employee{private int bonus; Manage (string name, string id, int gongzi, int bonus) {super (Name,id,gongzi); this.bonus = bonus;} public void work () {System.ut.println ("Manage Work");} Difference between}class Pro extends Employee{pro (string name, string id, int gongzi) {super (Name,id,gongzi);} public void work () {System.ut.println (' pro Work ');}}
Dark Horse Programmer--java Foundation--Object-oriented--inheritance