Learn about the two relationships that exist between things and things in life before understanding inheritance:
1, the relationship between the whole and part--has a relationship; For example: the relationship between the team and the player.
The code examples are as follows:
1 //Players2 classplayer{3 intnum;4 String name;5 PublicPlayer (intnum, String name) {6 This. num =num;7 This. Name =name;8 }9 Public voidrun () {TenSystem.out.println (name+ "open-run ..."); One } A } - - //Team the classteam{ - String name; - Player p; - PublicTeam (String name, Player p) { + This. Name =name; - This. P =p; + } A Public voidStartgame () { atSystem.out.println (name+ "on"); - } - } - - classdemo{ - Public Static voidMain (string[] args) { inPlayer p =NewPlayer (12, "Lionel Messi")); -Team T =NewTeam ("Brazil", p); to t.startgame (); +System.out.println ("Playing player name:" +t.p.name); - } the}
View Code
2. The relationship of inheritance--is a relationship; For example: the relationship between human beings and students.
Inheritance is represented by the keyword extends, in the following format:
Class subclass name extends parent class name {
}
The code examples are as follows:
1 //Human2 classperson{3 String name;4 PublicPerson (String name) {5 This. Name =name;6 }7 PublicPerson () {8System.out.println ("The constructor method of the person class is called ...");9 }Ten Public voideat () { OneSystem.out.println (name+ "at dinner ..."); A } - } - the //Student Class - classStudentextendsperson{ - intnum; - PublicStudent () { +System.out.println ("The constructor method of the student class was called"); - } + Public voidStudy () { ASystem.out.println (name+ "is learning ....")); atSystem.out.println (name+ "The school number is" +num); - } - } - classdemo{ - Public Static voidMain (string[] args) { -Student s =NewStudent (); inS.name = "Dog Doll"; -S.num = 123456; to s.eat (); + s.study (); - } the}
View Code
Things to note in succession:
1. Do not inherit in order to reduce duplication of code, only to inherit when there is a real inheritance relationship.
2. Members that are private to the parent class cannot be inherited.
3. The constructor of the parent class cannot be inherited.
4. When you create a subclass object, the parent class's parameterless constructor is called by default.
Super Keyword:
The Super keyword represents a reference to the parent class space.
The Super keyword works:
1. When the child parent class has a member with the same name, the child class is accessed by default as a member of the subclass, and the members of the parent class can be accessed through the Super keyword.
2. When you create a subclass object, the parent class is called without a parameter by default, and you can specify the constructor method for calling the parent class with the Super keyword.
The Super keyword calls the parent class construction method to note the following:
1. If the constructor of the parent class is not specified on the constructor of the subclass, then the Java compiler adds the super () statement to the constructor of the child class.
2. When the Super keyword calls the constructor of a parent class, the statement must be the first statement in the subclass constructor.
3. The super and the This keyword cannot be present in the same constructor to call other constructors. Because the first statement is required for two statements.
The Super keyword differs from the This keyword:
1. The things represented are inconsistent.
1. The Super keyword represents a reference to a parent class space.
2. The This keyword represents the caller object of the owning function.
2. Inconsistent use of premises.
1. The Super keyword must have an inheritance relationship before it can be used.
2. The This keyword does not require an inheritance relationship to be used.
3. The difference between calling constructors:
1. The Super keyword is the constructor that invokes the parent class.
2. The This keyword is a constructor that calls this class.
method Overrides
Prerequisite for method rewriting: an inherited relationship must exist.
Method overrides: The child parent class has a function of the same name, which we call an override of the method.
What is the time to use the override of a method: When the functionality of the parent class does not meet the requirements of the subclass.
Methods to override the considerations:
1. When overridden by a method, the method name must be the same as the formal parameter list.
2. When overridden by a method, the permission modifier for a subclass must be greater than or equal to the permission modifier of the parent class.
3. When overridden by a method, the return value type of the subclass must be less than or equal to the return value type of the parent class.
4. When overridden by a method, the subclass throws an exception type that is less than or equal to the exception type thrown by the parent class.
Exception (worst)
RuntimeException (small bad)
Examples of using and rewriting code for the Super keyword are as follows:
1 classfu{2 intx = 10;3 String name;4 PublicFu () {5SYSTEM.OUT.PRINTLN ("Non-parametric construction method of the Fu class");6 }7 8 PublicFu (String name) {9 This. Name =name;TenSystem.out.println ("The method of construction of the Fu class"); One } A Public voideat () { -System.out.println ("The Fu class object is eating"); - } the } - - classZiextendsfu{ - intx = 20; + PublicZi (String name) { - Super(name);//calling the parent class with a method for constructing a parameter +System.out.println ("Zi class with parameter construction method"); A } at PublicZi () {//the JVM defaults to calling the parent class's parameterless construction method -System.out.println ("Method of constructing Zi class without parameters"); - } - Public voidprint () { -System.out.println ("x =" in the Zi class +x); -System.out.println ("x =" in the FU class +Super. x);//gets the parent class variable with the same name in the child parent class in } - Public voidEat () {//method Overrides toSystem.out.println ("Zi class at dinner ... "); + } - } the * classdemo{ $ Public Static voidMain (string[] args) {Panax NotoginsengZi z =NewZi (); -Zi z2 =NewZi ("Zhang San"); the z2.print (); + z2.eat (); A } the}
View Code
Java is a single inheritance.
A class can have at most one direct parent class. However, there are multiple indirect parent classes .
Final (final, modifier)
Use of the final keyword:
1. When the final keyword modifies a variable of a primitive type, the variable cannot be re-assigned, and the first value is final.
2. When the FIANL keyword modifies a reference-type variable, the variable cannot be re-directed to the new object.
3. When the final keyword modifies a function, the function cannot be overridden.
4. When the final keyword decorates a class, the class cannot be inherited.
Modifiers for constants are generally: public static final
Naming conventions for constants: Capitalize all letters, separating words from words with underscores.
Object-oriented three characteristics (ii)--Inheritance