Two basic characteristics of the object-oriented Java: inheritance

Two of the basic features of object-oriented: Inheritance

1, inheritance benefits

(1) code reuse

Extension (2) code

2, how to inherit?

Syntax:

[Modifier] subclass extends parent class {
}

3, inherited characteristics

(1) The subclass inherits all the characteristics (properties, methods) of the parent class

However, private in a subclass can not be used directly

(2) sub-class does not inherit the parent class constructor

Because, the constructor of the parent class is the parent class object is created for

(3) sub-class constructor must go and call the constructor of the parent class

While creating a subclass object, is initialized with the properties inherited from a parent class, the parent can use the code constructor for class attribute assignment.

(4) Java supports only single inheritance: a subclass can only have a "direct" parent

(5) Java and supports multiple inheritance: the parent class can also have a parent class, will feature from generation to generation

(6) a parent can have many sub-categories at the same time

Keyword super

super keyword: reference to the parent class, find the parent class xx

usage:

(1) super. Properties

When the child and the parent class declares a member variable of the same name, so if you want to represent a member variable is the parent class, you can add "super."

(2) super. Method

When a subclass overrides a parent class, but also need to call the parent class method is overridden in a subclass, you can use the "super."

(. 3) super () or super (argument list)

super (): represents the no-argument constructor calls the parent class

super (argument list): Indicates call the parent class's constructor parameters

(1) If you want to write super () or super (argument list), you must be written in the first line of the subclass constructor

(2) If you do not write the constructor of the subclass: super () or super (argument list), the default will be super ()

(3) If the parent class is not constructor with no arguments, then the first line in the constructor subclass of "must" write super (argument list)

Rewrite method

1, the overriding method (Override)

When a subclass inherits the parent class, but also the realization that the body of the parent class is not suitable for sub-class, sub-class can choose to be rewritten.

Rewriting of claim 2, the method

(1) Method name: must be the same

(2) parameter list: to be the same

(3) modifier

Permissions modifiers:> =

(4) Return Type

If the basic data types and void: must be the same

If reference data type: <=

3, the difference overload (Overload) and rewritable (Override) of

Overload (Overload): In the same class, the same method name, parameters different lists, and returns two or more methods of independent value type.

Rewriting (Override): between the parent-child class.

Three basic features of object-oriented: Polymorphism

1, polymorphism:

Syntax:

Referenced parent class / subclass objects = variable;

2, provided that:

(1) inheritance

Rewrite (2) Method

(3) Multi-state reference

3, phenomenon:

Look to the left when compiling / "parent" to see the right runtime / "subclasses."

Compile time, because according to the parent class compiler, then only some parent class method, expand the subclass method can not be invoked;

Execution must be rewritten to run through a method subclass thereof.

Sample code:

{the Person class
    public void EAT () {
        System.out.println ( "food");
    }
    public void Walk () {
        System.out.println ( "walking");
    }
}
class Woman the extends the Person {
    public void EAT () {
        System.out.println ( "small mouth meal");
    }
    public void walk () {
        System.out.println ( "sexy walk");
    }
}
class Man the extends the Person {
    public void eAT () {
        the System.out .println ( "big mouth meal");
    }
    public void walk () {
        System.out.println ( "walk fast");
    }
}
class Test{
    public static void main(String[] args){
        Person p = new Woman (); // polymorphic reference
        p.eat (); // perform a subclass overrides
        p.walk (); // perform subclass overrides
    }
}

4, Application:

(1) Multi-modal parameters: the parameter is a parent class, subclass object argument is

(2) Multi-state array: the array element type is the parent element is stored subclass object

Sample Code: polymorphic Parameter

{the Test class
    public static void main (String [] args) {
        Test (new new Woman ()); // the object argument is a subclass
        test (new Man ()); // argument is a subclass object
    }
    public static void test (Person p) {// is the parent class parameter type
        p.eat ();
        p.walk ();
    }
}

Sample Code: polymorphic array

class Test{
    public static void main(String[] args){
        Person[] arr = new Person[2];//多态数组
        arr[0] = new Woman();
        arr[1] = new Man();
        
        for(int i=0; i<arr.length; i++){
            all[i].eat();
            all[i].walk();
        }
    }
}

5, upward transition and the downward transition: the transition between the parent-child class

(1) upcast: automatic conversion

When an object is assigned to the sub-class parent class variable (i.e. polymorphic reference), at compile time, the transformation of a parent class object is upward. At this time, a subclass can not see the "unique extension" approach.

(2) downcast: cast. risky.

When you need the parent class variable assignment to a variable of a subclass, you need to downcast.

Run-time type transformation in order to succeed, we must ensure the preservation of the variable object type is <= strong turn

6、instanceof

Expression syntax:

Object / variable instanceof type

Operation result: true or false

effect:

It used to determine whether the object belongs to this type, or, if this is the object of the object type or subclass of this type

public class Test{
     public static void main(String[] args){
         Person p = new Person();
         Woman w = new Woman();
         if(p instanceof Woman){   //false 
         }
         if(w instanceof Woman){   //true   
         }
     }
 }

 

Guess you like

Origin blog.csdn.net/Brevity6/article/details/90702586