14- inheritance

Inheritance 
Inheritance is a very powerful object-oriented programming for a mechanism that can reuse code. When asked to subclass inherits from a parent class, subclass will get all the features of the parent class. 
Java uses the extends keyword to implement inheritance: 
class the Person {
     Private String name;
     Private  int Age; 

    public String getName () {...}
     public  void setName (String name) {...}
     public  int getAge () {.. .}
     public  void the setAge ( int age) {...} 
} 

class Student the extends the Person {
     // do not repeat the name and age field / method,
     // only need to define new score fields / methods: 
    Private  int score; 

    public  intgetScore () {...}
     public  void setScore ( int Score) {...} 
} 

The term OOP, we called the superclass Person (Super class ), the parent class (parent class ), the base class ( Base  class ), the Student referred subclass (subclass), extended type (extended class ). 

Inheritance tree 
in the definition of Person, and did not write extends. In Java, there is no clear writing extends the class, the compiler will automatically add extends Object. Therefore, any type, except Object, will inherit from a class. Java allows only one class inherits from a class, therefore, a class has one and only one parent class. Only Object special, it has no parent. 

If we define a class that inherits from Person of the Teacher, their inheritance tree as follows: 
       ┌───────────┐ 
       │ Object │ 
       └───────────┘ 
       ┌─ ──────────┐  
       │ │ the Person
       └───────────┘ 
          ▲ ▲ 
          │ │ 
          │ │ 
┌───────────┐ ┌───────────┐ 
│ │ │ Teacher Student │ 
└───────────┘ └───────────┘ 


protected (protection) 
inheritance has a feature that a subclass can not access the private field or private parent class method, in order to allow subclasses can access the parent class of the field, we need to change the private protected. Can be subclassed to access protected with modified fields: 
class the Person {
     protected String name;
     protected  int Age; 
} 

class Student the extends the Person {
     public String Hello () {
         return  " the Hello, " + name; // the OK! 
    } 
}

protected keyword can access the fields and methods of controlling the internal inheritance tree, a protected fields and methods can be accessed by its subclasses, subclasses and subclass. 

Super 
Super keyword indicates that the parent class (super class). A subclass referenced parent class field, may be used super.fieldName. 
class Student the extends the Person {
     public String Hello () {
         return  " the Hello, " + super.name; 
    } 
} 

used herein super.name, or this.name, or name, the effect is the same. The compiler will automatically locate the parent class name field. 
In Java, any class constructor, the first statement must be a call to the constructor of the parent class. If you do not call the parent class constructor explicitly, the compiler will automatically help us add that super () ;, but if the parent is no default constructor, the subclass must explicitly call super () and gives the parameters so that the compiler locates a suitable configuration of the parent class. 
class Student the extends the Person {
     protected  int Score; 

    public Student (String name, int Age,int Score) { 
        super (name, Age); // call the constructor of the superclass Person (String, int) 
        the this .score = Score; 
    } 
} 

Note: not inherited subclasses of the parent class constructor, but the super ()transfer. 

Upward transition 
if the type is a reference variable subclass, it can point to an instance inherits the parent class types: 
Person the p- = new new Student (); 

this is because Student inherits from Person, it has all the features of Person. Person type variable type if the point Student instance, a subclass of the type which safely in the parent class type assignment, referred to upcast (upcasting). 
S Student = new new Student (); 
the Person P = S; // upcasting, OK 
Object O1 = P; // upcasting, OK 
Object O2 = S; // upcasting, OK

Note: The inheritance tree is Student > the Person> Object, therefore, can transition Student type Person, or a higher level of Object. 

Downcast 
and upcast Conversely, if the parent class type casts a subclass type, it is downward transition (downcasting). Downcast but is likely to fail. When the failure, Java virtual machine newspaper ClassCastException. Because a subclass of functions than the parent class, many functions can not be conjured out of thin air. 
P2 the Person = new new the Person (); 
Student S2 = (Student) p2; // !! A ClassCastException Runtime error
 // actual type p2 is Person, not the parent class become subclass 

in order to avoid mistakes downward transition, Java provides instanceof operator can first determine a variable points instance whether the specified type, or subclass of this type: 
the Person P = new new Student ();
 iF (P instanceof Student) {
     // only determines success comes downcast : 
    Student S = (Student) P; // will succeed 
}


Distinguish between inheritance and composition 
in the use of inheritance, should pay attention to logical consistency. 
class Book {
     protected String name;
     public String getName () {...}
     public  void the setName (String name) {...} 
} 

When Book class also has a name field, then re-build the Student class, why not Student inherited from the Book? 
Since Student is a Person, which is is the relationship (containing, change the parent class, subclass change along), but not the Student Book. In fact the relationship between Student and Book has a relationship (with only the same field, and do not share). 

Reference: HTTPS:
// www.cnblogs.com/feichengwulai/archive/2014/03/29/3632537.html HTTPS: // blog.csdn.net/qq_36767910/article/details/80552133

 

Guess you like

Origin www.cnblogs.com/nsss/p/11417487.html