Inherited characteristics and Notes

Inheritance benefits

 

a: to improve the reusability of code

 

b: to improve the maintainability of the code

 

c: Let generate a relationship between classes, polymorphism is a prerequisite

 

 

Inherited drawbacks

 

Type coupling is enhanced.

 

Inherited characteristics

Characters after the inheritance - a member variable

Member variable is not the same name

If there is a subclass of the parent class is not the same name member variables, then access is not affected

Member variables the same name

If there is a subclass of parent class the same name member variables, then access is influential .

Code demonstrates

class Fu {
     // member variables of Fu. 
    int NUM =. 5 ; 
} 
class Zi the extends Fu {
     // member variables Zi is 
    int NUM =. 6 ;
     public  void Show () {
         // access the parent class NUM 
        System.out.println ( "Fu NUM =" + NUM );
         // NUM access subclass 
        System.out.println ( "Zi NUM =" + NUM); 
    } 
} 
class ExtendsDemo03 {
     public  static  void main (String [] args) {
           // create a subclass object
        Z = zi new new zi (); 
           // call to the subclass show 
        z.show (); 
    } 
} 
Demo Results: 
Fu NUM =. 6 
zi NUM =. 6

 

When a member variable of the same name appeared sub-parent classes, subclasses need access to the parent class-Africa private member variables, use the superkeyword, modifying the parent class member variable, similar to the previously learned this.

Using the format:

Super . parent class member variable name

 

After inherited characteristics - member method

The method is not a member of the same name

If there is a subclass of the parent class is not the same name as a member of the method, when the calls are not affected . When the object method call, will first look for in a subclass have no corresponding method, if there is a subclass will execute the method in the subclass, if there is no subclass will execute the appropriate method in the parent class.

Code demonstrates

class Fu {
     public  void Show () { 
        System.out.println ( "Show Fu class execution method" ); 
    } 
} 
class Zi the extends Fu {
     public  void Show2 () { 
        System.out.println ( "Zi class show2 method performs " ); 
    } 
} 
public   class ExtendsDemo04 {
     public  static  void main (String [] args) { 
        Zi Z = new new Zi ();
          // subclass does not show methods, but the method can be found in the parent to perform 
        z. Show (); 
        z.show2 ();  
    }
}

Member method of the same name - Rewrite (Override)

If there is a subclass of parent class the same name as a member of the method, then the access is a special case, called the method rewrite (Override) .

Method override : Method identical parent class when the child class (type of return value, the method name and parameter list are the same), there will be covering effect, also known as rewriting or replication. Statement unchanged, re-implemented .

Code demonstrates

class Fu {
     public  void show () { 
        System.out.println ( "Fu show" ); 
    } 
} 
class Zi the extends Fu {
     // child class overrides a superclass method show 
    @Override
     public  void show () { 
        the System. Out.println ( "Zi show" ); 
    } 
} 
public  class ExtendsDemo05 {
     public  static  void main (String [] args) { 
        Zi Z = new new Zi ();
          // subclass show methods have to perform only the rewritten show method
        z.show();  // Zi show
    }
}

Rewrite applications

Subclass can act according to their own needs, define specific. Both followed the function name of the parent class, but also needed to re-implement a subclass of parent class methods to extend enhanced.

 

Characters after the inheritance - Constructor

When generating a relationship between classes, in all types of construction methods, but also what impact it?

First we have to define the format and recalled the role of two things that the constructor.

  1. Name of the constructor method is consistent with the class name. So subclass can not inherit the parent class constructor.

  2. The role of the constructor is to initialize the member variables. So subclass initialization process, you must first perform the initialization operation of the parent class. Constructor of a subclass of a default super(), represents the constructor calls the parent class, the parent class member variables are initialized before they can use by subclasses

 

Inheritance Notes

A: sub-class can only inherit the parent of all non-private members (member methods and member variables)

B: a subclass can not inherit the parent class constructor, but by super () key to access the parent class constructor

C: Do away to inherit some of the features

2. Empty arg constructor subclass empty default constructor parameters call the parent class method

2. subclass will have a default constructor parameters empty-argument constructor calls the parent class method

3. In a subclass super () constructor parameter space must access the parent class in the first line

3. Use super (arguments) to access the parent class has a subclass constructor must be placed on a first reference line

4. The method of subclass overrides the parent class method must ensure that the parent class greater than or equal rights authority.

5. The method of subclass overrides the parent class method, the return type, function name, and parameter list must be exactly the same.

 

 

 

Guess you like

Origin www.cnblogs.com/libinhong/p/10990259.html