Inheritance of three major features of Java
First, Introduction
Notes Focus: constructors, protected keywords (this self-lookup), up-conversion, private keywords (important points of inheritance)
The Reuse code is one of the many compelling features of Java. But to be a revolutionary language, it's not enough to just copy the code and change it, and it has to be able to do more. The most compelling thing in this sentence is "reuse code", as much as possible to reuse the code that we programmers have been pursuing.
Inheritance is the technique of building a new class using the definition of an existing class, and the definition of a new class can add new data or new functionality, or it can use the functionality of the parent class, but not selectively inherit the parent class. By using inheritance, we can easily reuse the previous code, which can greatly improve the efficiency of development.
Why to inherit?
A: Through inheritance can make the description of the object more clear, you can implement code reuse , you can implement the overridden class variables or methods.
Ii. in-depth understanding of inheritance
The inheritance of 1.Java has the characteristics of single inheritance , that is, only one parent class can be inherited , each subclass has only one direct parent class, but its parent class can inherit from another class , so that subclasses can inherit more than one parent class indirectly. But its essentially division is still a parent class and subclass of the relationship.
The inheritance of 2.Java is implemented by the extends keyword , and the class that implements the inheritance is called the subclass, and the inherited class is called the parent class (some also called the base class, the superclass)
3. Subclasses have properties and methods that are not private to the parent class. (It's easy to understand that it's wrong to say this to you, =.=! That's a good idea. )
4. Subclasses can have their own properties and methods, that is, subclasses can extend the parent class.
5. Subclasses can implement methods of the parent class in their own way.
6. Why do we translate extends into inheritance in China? In addition to the historical reasons, it is also reasonable to translate extends into inheritance: The subclass extends the parent class, it will be able to get all the properties and methods of the parent class, which is very similar to the inheritance in Chinese (The offspring inherit a fortune from their fathers).
Third, what kind of class can not be inherited?
1.Final (final state) decorated classes cannot be inherited, but do not misunderstand that a final class cannot be inherited.
Example 1:
Example 2:
2. The constructor ( note the following about the constructor ) cannot be inherited by the private keyword-decorated class. An explicit constructor must be defined to be inherited ( for example, decorated with the protected and private keywords ).
Example 1:
Four, the constructor (a brief introduction above an example)
What is a constructor?
A: Simply speaking, the constructor is a special method that is the same as the class name, but it does not return a value. in-depth introduction of the constructor in Java, to ensure that each object is initialized, java before the ability to manipulate the object, the system will automatically invoke the appropriate constructor, to ensure that the initialization. The greatest use of the constructor is to perform initialization when an object is created, and when an object is created, the system initializes the instance of the object by default. If you want to change this default initialization, you can do so by customizing the constructor. that is: the constructor is defined as a method for initializing an object in a Java class , is the fundamental way to create an object .
Use the constructor to be aware of the points: 1. If we do not define a constructor by ourselves, the system defaults to providing an argument-free constructor for that class. Once we provide a constructor for a class, the system will not mention the default parameterless constructor. (Examples Example 1, 2 are provided below for comprehension)
2. the constructive method of the parameter and the method of non-parametric construction can exist simultaneously (method overloading)//do not provide an example, knock yourself.
3. the construction method can not only assign a value to a property, but also ensure that a reasonable value is assigned to the object's property .
Example 1:
1 Public classTest1 {2 Public Static voidMain (string[] args) {3 //TODO Auto-generated method stubs4Test2 p=Newtest2 ();5System.out.println ("Hello");6 }7 }8 classtest2{9 PrivateString B;Ten //Public test2 () {}---The system already defaults to providing an argument-free constructor for the class One}
Example 2:
1 Public classTest1 {2 Public Static voidMain (string[] args) {3 //TODO Auto-generated method stubs4 //the new key is to call the constructor and return the class instance5Test2 p=NewTest2 ();//Error6System.out.println ("Hello");7 }8 }9 classtest2{Ten PrivateString B; One //because we have defined a method for constructing a parameter, the system will no longer provide a default parameterless construction method A Publictest2 (String b) { - This. b=b; - } the}
Make a point: The parent class constructor is called in the constructor to complete the initialization, and the parent class constructor has all the knowledge and power required to perform the initialization of the parent class.
1 Public classTest1 {2 Public Static voidMain (string[] args) {3 //TODO Auto-generated method stubs4 //the new key is to call the constructor and return the class instance5Test3 p=NewTest3 ();//Error6System.out.println ("Hello");7 }8 }9 classtest2{Ten Publictest2 () { OneSystem.out.println ("Test2"); A } - } - classTest3extendstest2{ the Publictest3 () { - //super ();---has called the parent class constructor by default -System.out.println ("Test3"); - } +}
Output:
Test2 test3 Hello
parsing: subclasses inherit the parent class from the parent class outward, extending from the parent class to the subclass layer. The subclass will call the constructor of the parent class by default, and the call is a precondition for the parent class to have a default constructor, and if not, the compiler will error: the constructor that conforms to the parent class cannot be found .
1 Public classTest1 {2 Public Static voidMain (string[] args) {3 //TODO Auto-generated method stubs4 //the new key is to call the constructor and return the class instance5Test3 p=NewTest3 ();//Error6System.out.println ("Hello");7 }8 }9 classtest2{Ten Publictest2 (String name) { OneSYSTEM.OUT.PRINTLN ("test2------" +name); A } - } - classTest3extendstest2{ the Publictest3 () { - Super("Zs");//---must call the parent class with the parameter constructor -System.out.println ("Test3"); - } +}
Output:
test2------ZS Test3 Hello
Summary: The subclass will call the constructor of the parent class by default when inheriting, but if there is no default parent class constructor, the subclass must display the constructor for the specified parent class, and must be the first thing to do in the subclass constructor (the first line of code).
Five, upward transformation
The parent class in the inheritance is the relationship of "is a", for example, "Student" class inherits "person", then it can be said that the student is a person (this is the upward transformation). The upward transformation is secure, but some of the properties and methods may be lost (during the transition, the new methods of the subclass are lost, and at compile time, the system provides an error that the method cannot be found).
1 Public classTest1 {2 Public Static voidMain (string[] args) {3 //TODO Auto-generated method stubs4 //the new key is to call the constructor and return the class instance5Test3 p=NewTest3 ();//Error6Test2 p1=NewTest2 ("351");7P1.P (P);//-----Transformation and is safe.8 }9 }Ten classtest2{ One Publictest2 (String name) { ASYSTEM.OUT.PRINTLN ("test2------" +name); - } - Public voidp (test2 p1) { theSystem.out.println ("+++++"); - } - } - classTest3extendstest2{ + Publictest3 () { - Super("Zs"); +System.out.println ("Test3"); A } at}
Extended:
1, the parent class changes, the subclass must change.
2, inheritance destroys the encapsulation, for the parent class, its implementation details are transparent to the subclass.
3, in fact, the subclass has not only the parent class of non-private properties and methods, but also the parent class of private properties and methods. First of all need to be clear: private, public and protected these keywords, and inheritance is not related to the relationship should be called "visibility." The parent class cannot control its own members the Quilt class inherits the walk, but can constrain some inherited member variables, which are not visible to subclasses.
(The individual understands that all the properties and methods of the parent class for a subclass are simply unusable, and this is not a nonsense to find out).
Java Essay: Inheritance of three major features