Inherited
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.
Class Son extends super{...}
Inheriting subclasses of the parent class can do the following:
- 1, subclasses can not be restricted to call the parent class non-private properties and methods
- 2, subclasses can have their own properties and methods, that is, subclasses can extend the parent class
- 3. Subclasses can implement methods of the parent class in their own way (overriding the parent class method)
There are three points to understand in inheritance: constructors, protected keywords, and upward transformation.
Note the point:
You can use super when the properties of a subclass are the same as the parent class's property name ( not private and under the same package as the default adornment ) when you want to invoke the property of the parent class in the subclass . Property name '.
You can use super when a subclass overrides a method of the parent class, and you need to call the overridden method of the parent class in the subclass . Method '.
classsuperclass{Private intAge ; PublicString name= "AA"; Public voidShow () {System.out.println ("Superclass"); }}classSonclassextendssuperclass{PrivateString name= "Sonclass"; Public voidShow () {Super. Show (); System.out.println ("Sonclass" + name +Super. Name); }}Constructors
The subclass will call the constructor of the parent class by default, 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).
1, if the parent class does not actively create a constructor or if the parent class has a constructor that has no arguments, the subclass may not consider the constructor of the parent class.
2. Use the Super command if the subclass needs to inherit the parameter constructor of the parent class (if the parent class contains more than one parameter constructor, use the Super command according to the parameters)
3. The subclass must use the Super command if the parent class establishes a constructor and all parameters are included.
Protected
For protected, it indicates that he is private to the class user, but is accessible to any subclass that inherits from this class, or any other class that is in the same package.
- Public (available to anyone)
- Protect (inherited classes can be accessed and have the same permissions as private)
- Default (Package access, which can be accessed throughout the package)
- Private (elements that cannot be accessed by anyone other than the type creator and the internal method of the type)
Upward transformation
Transition up: The object that the subclass refers to is converted to a parent class type called upward transformation. In layman's words, a subclass object is converted to a parent class object. Here the parent object can be an interface
Downward transformation: The parent class references an object that is converted to a subclass type called downward transformation.
Issues to be aware of in transition:
When you move up, the parent class points to the subclass reference object and loses other methods that are common to the parent object, that is, during the transformation, the new methods of the subclass are lost, and at compile time, the system provides an error that the method cannot be found.
In the downward transformation process, there are two situations:
Case one: If the object referenced by the parent class refers to a sub-class object that is pointed to, it is safe in the process of downward transformation. That is, compiling does not make mistakes.
Scenario Two: If the parent class refers to an object that is the parent class itself, it is unsafe in the process of downward transformation and compiles without errors, but java.lang.ClassCastException errors occur at run time. It can use instanceof to avoid errors like this. Examples are as follows:
classaa{Private intAge ; Public voidShow () {System.out.println ("AA"); }}classBbextendsaa{PrivateString name; Public voidShow () {System.out.println ("BB"); } Public voidprint () {System.out.println ("BBBBBBBBBB"); }} Public classExtendsdemo { Public Static voidMain (string[] args) {BB b=NewBB (); //Upward TransformationAA AB =(AA) b; //can not call BB's unique methodab.show (); AA a=NewAA (); //Down Transformation//bb ba = (BB) A;//Direct Error ClassCastException if(AinstanceofBB) {BB ba=(BB) A; System.out.println (Down); } }}
Summary of transitions between subclasses and parent classes:
1. The parent class reference can point to the subclass object, and the subclass reference cannot point to the parent class object.
2, the object of the class directly assigned to the parent class reference called upcasting upward transformation, upward transformation without forced transformation.
such as Father father = new Son ();
3. Assign the parent reference of the child class object to the subclass reference called down Transformation (downcasting), to force the transformation.
If father is a parent reference to a subclass object, assign father to the subclass reference son, son son = (son) father;
Where the Father Front (Son) must be added for casting.
4, Upcasting will lose the subclass-specific methods, but subclasses overriding the method of the parent class, the subclass method is valid
5, the role of upward transformation, reduce duplication of code, the parent class as parameters, and sometimes sub-class as a parameter, is the use of upward transformation. This makes the code concise. Embodies the idea of Java abstract programming.
Using Inheritance with caution
Inheritance has the following drawbacks:
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, inheritance is a strong coupling relationship .
ask yourself if you need to transform from subclass to parent class. If you have to move up, inheritance is necessary, but if you don't, you should consider whether you need to inherit.
Three main features of Java object-oriented--inheritance