Object-oriented: Inheritance

Java is achieved by the extends keyword, implementation inheritance class is called a subclass, the inherited class becomes the parent class or base class, superclass.

Subcategory expansion of the parent class, subclass is a special parent class. Java subclasses can not get the constructor of the parent class

class FatherClass{
    public void fatherMethod(){
        System.out.println("fatherMethod");
    }
}
public class ExtendsClass1 extends FatherClass{
    public static void main(String[] args){
        ExtendsClass1 ec = new ExtendsClass1();
        //获得父类的方法
        ec.fatherMethod();
    }
}

 If a class does not specify a parent class, the default class Object, Object is the parent class of all classes, either direct parent, or is the indirect parent class.

Method override the parent class

class FatherClass {
     public  void fatherMethod () { 
        System.out.println ( "fatherMethod" ); 
    } 
} 
public  class ExtendsClass1 the extends FatherClass { 
    
    public  void fatherMethod () { 
        System.out.println ( "override the parent class method, sonMethod " ); 
    } 
    public  static  void main (String [] args) { 
        ExtendsClass1 EC = new new ExtendsClass1 ();
         // this subclass overrides a superclass method, the method of running the subclass 
        ec.fatherMethod (); 
    } 
}

 Phenomenon subclass contains the same name as the parent class method called overwriting method, override, also called method overriding.

The overriding method to follow the same principle of two small two, with two: the name of the same method, the same parameter list; two small type subclass method returns a value return value type is smaller than or equal to the parent class method, the subclass method smaller or equal than the declared exception thrown by the parent class. A great, great access to the subclass method than the parent class. And a cover coating method or methods are examples of methods, are based either method is not a method of a class is an instance method.

When the subclass overrides the superclass method, not subclass methods of the parent class. Can the parent class overridden methods call parent class using the super keyword or the class name.

class FatherClass {
     public  void fatherMethod () { 
        System.out.println ( "fatherMethod" ); 
    } 
} 
public  class ExtendsClass1 the extends FatherClass { 
    
    public  void fatherMethod () {
         // System.out.println ( "override the parent's method , sonMethod ");
         // Super call the parent class methods 
        Super ; .fatherMethod () 
    } 
    public  static  void main (String [] args) { 
        ExtendsClass1 EC = new new ExtendsClass1 (); 
        ec.fatherMethod (); 
    } 
}

 Java is a super key provided for defining the object to invoke the method it inherits instance variables or derived from the parent class. this can not occur in the static method, super can not appear in the modified static method. modified static methods of the class.

class FatherClass {
     static  int A = 18 is ; 
} 
public  class ExtendsClass2 the extends FatherClass {
     static  int A =. 19 ;
     public  static  void main (String [] args) {
         // this is a value subclass 
        System.out.println ( "sub a class value of "+ a);
         // Super can not be modified static method 
        System.out.println ( Super II.A); 
    } 
}
class FatherClass {
     int a = 18 is ;
     public  void Method () { 
        System.out.println ( "a parent value" + a); 
    } 
} 
public  class ExtendsClass2 the extends FatherClass {
     int a =. 19 ;
     public  void Method () { 
        System.out.println ( "a subclass of a value" + a); 
        System.out.println ( "subclass overrides the parent class method, the output value of the parent class" + Super II.A); 
    } 
    public  static  void main ( String [] args) { 
        ExtendsClass2 EC =new ExtendsClass2();
        ec.method();
    }
}
class FatherClass {
     int a = 18 is ;
     public  void Method () { 
        System.out.println ( "a parent value" + a); 
    } 
} 
public  class ExtendsClass2 the extends FatherClass {
     int a =. 19 ;
     public  void Method () {
         // subclass method call parent class of 
        Super ; .method () 
    } 
    public  static  void main (String [] args) { 
        ExtendsClass2 EC = new new ExtendsClass2 (); 
        ec.method (); 
    }
}

 Superclass private methods and variables subclass can not inherit, you can not be accessed. It is not the same if the same sub-class defines methods and variables, and the parent class.

class FatherClass {
     Private  int A = 100 ;
     Private  void Method () { 
        Sytem.out.println ( "parent private method" ); 
    } 
} 
public  class ExtendsClass3 {
     int A = 200 is ;
     public  void Method () { 
        the System.out. the println ( "subclass method, not inherited from the parent class" ): 
    } 
}

Subclass constructor calls the parent class

 The first sub-row super class constructor performed explicitly call parent class constructor, the system will call the parent class corresponding to the list constructor in accordance with an incoming call super example; first line of code executable subclass use this constructor significant invocation this class constructor overload, the system will call another constructor for this class in accordance with this incoming call argument list, calls the parent class constructor another implementation of this class constructor; sub executable class constructor calls super neither nor this call, the system will not call the parent class implicit constructor parameter before executing the sub-class constructor.

 

Guess you like

Origin www.cnblogs.com/zaijie/p/11032152.html