Object-oriented inheritance of 04 (method override emphasis)

The main characteristic is inherited subclass can extend existing function according to the function of the parent class, the attributes and methods in the subclass defined property or method when it is possible to define with the same name as the parent class, referred to overwrite this
method override
when the subclass definition with the same name as the parent class method, the same type and number of parameters of the method, overwriting occurs
an effect observed overwriting

class A{
    public void fun(){
        System.out.println("A类中的方法");
    }
}

class B extends A{
}

public class testDemo{
    public static void main(String args[]){
        B b = new B();
        b.fun();
    }
}


Because this time the subclass was not fun () method, so that subclasses inherit from the parent class's fun () method
override method

class A{
    public void fun(){
        System.out.println("A类中的方法");
    }
}

class B extends A{
    public void fun(){
        System.out.println("B类中的方法");
    }
}

public class testDemo{
    public static void main(String args[]){
        B b = new B();
        b.fun();
    }
}


When overwriting occurs, then calls the instance subclasses method has been overridden in
a class may produce a plurality of subclasses, each subclass may extend their method
override feature analysis result
1. The observed instance of that class is
2 to see if this method to instantiate the class which has been covered calls written, if not overwritten before, the parent class is called
the principle overwritten use (passive):
If we find that the parent class inadequate methods name function (not suitable for this class), but it must use this method name when you need to use override this concept to achieve
the above code has been implemented override function, but in order to better achieve the override, but also you must take into account access issues. The method can not be overridden have more stringent than the parent class access control permissions
for access control permissions have been understood in three: public> default> private
99% of cases the use of public
right override

class A{
     void fun(){
        System.out.println("A类中的方法");
    }
}

class B extends A{
    public void fun(){
        System.out.println("B类中的方法");
    }
}

public class testDemo{
    public static void main(String args[]){
        B b = new B();
        b.fun();
    }
}

Mistakenly overwritten

class A{
    public void fun(){
        System.out.println("A类中的方法");
    }
}

class B extends A{
     void fun(){
        System.out.println("B类中的方法");
    }
}

public class testDemo{
    public static void main(String args[]){
        B b = new B();
        b.fun();
    }
}

Use a subclass of the default permissions, the parent class using a public authority, are more stringent category

at this time can not be called overwrite
doubt? If the method in the parent class using a private statement, subclass uses a public statement, so called overwrite it
Conceptually, the parent class is private, belonging to a small range of authority, and belongs to expand the scope of public authority. Meet the requirements of the override authority.
Correct override method
put aside private not look, start with a normal overwrite operation

class A{
    public void fun(){
        print();
    }
    public void print(){
        System.out.println("A类中的方法");
        
    }
}

class B extends A{
    public void print(){
        System.out.println("B类中的方法");
    }
}

public class testDemo{
    public static void main(String args[]){
        B b = new B();
        b.fun();
    }
}

Use private declarations parent class

class A{
    public void fun(){
        print();
    }
    private void print(){
        System.out.println("A类中的方法");
        
    }
}

class B extends A{
    public void print(){
        System.out.println("B类中的方法");
    }
}

public class testDemo{
    public static void main(String args[]){
        B b = new B();
        b.fun();
    }
}


In this case we found no subclasses override print () method, that is, the use of a private statement, this method is not visible to subclasses, even if the child class defines a method override in full compliance with the requirements of the parent class , it can not happen overwritten. In fact equivalent to a subclass defines a new approach
must be the default method of coating wrote erupted class can be invoked. To the parent class method has been overwritten can be explicitly invoked by subclasses, you can add "super. Method ()" Access

class A{
    public void fun(){
        print();
    }
    public void print(){
        System.out.println("A类中的方法");
        
    }
}

class B extends A{
    public void print(){
        super.print();
        System.out.println("B类中的方法");
    }
}

public class testDemo{
    public static void main(String args[]){
        B b = new B();
        b.fun();
    }
}


About the difference between super. Method () with this. Method () is?
Use this. Method () will first look whether there is a method name to call this class, there is the call, or to find out if there is this method in the parent class, there is called, being given not compiled

using super. Method (), clear not represent a subclass method calls (does not look subclass method), directly call the parent class method
interview: Please explain the difference between overloading and override? (Please explain the difference Overloading and Overwrite?)
No. | differences | reload | overwrite |

  • |: -: |: -: |: -: |
    . 1 | English | Overloading is the | OVERRIDE |
    2 | range | occurs inside a class | occur in succession relations |
    same method name, parameters of the same type and number of the | 3 | Definition | name the same method, the same type and number of parameters, the method returns the same value |
    4 | rights | overloaded without permission restrictions | methods are overridden can not have more stringent than the parent class access control permissions |
    return in the use of overloading can different values?
    In the event of overloading relations, the return value may be different, but considering the programming of unity, when overloaded try to ensure that a consistent method return value type

Guess you like

Origin www.cnblogs.com/anyux/p/11900422.html
Recommended