Relationship [Posts] rewriting the Java polymorphism and overloading and

Relations rewrite in Java and overloading and polymorphism of

Disclaimer: This article is a blogger original article, follow the  CC 4.0 BY-SA  copyright agreement, reproduced, please attach the original source link and this statement.
This link: https://blog.csdn.net/qq_34383019/article/details/100550142

Rewrite (Override)

Rewrite refers to a subclass of parent class can allow access to re-write, they have the same name, the same parameters, return the same value, but the content is not the same, the new subclass will override the parent class of the original Methods.

Overload (Overload)

Overload in the same class, the same method name, different parameters. Different parameters of such a method with the same name is called reload.

Overloaded VS rewrite

Rewrite rules:

1, the parent class members can only be issued to rewrite its subclasses

2, sub-class method access modifier must be greater than the parent class access modifier (public> protected> default> private).

3, sub-classes and superclasses in the same package, then the subclass can override all the parent class, except that a method declared private and final.

4, sub-classes and superclasses are not the same package, then the subclass can only override the parent class declared as non-final public or protected methods are.

5, the final statement of the method can not be overridden.

6, declared as static methods can not be rewritten, but can be declared again.

7, the same parameters as a list of sub-class method, method name, return value must be inherited methods

8, construction methods can not be overridden.

9, if you can not inherit a method, you can not override this method.

Rewrite example:

public class Cat {
    public static void main(String[] args) {
        Cat cat = new Jack();
        cat.name();
    }

    public void name(){
        System.out.println("cat");
    }
}

class Jack extends Cat{

    public void name(){
        System.out.println("jack");
    }
}

Overloading rules:

1, a method must be changed overloaded parameter list (number of parameters or not the same type);

2, the method may be varied overloaded return type;

3, a method is overloaded access modifiers may be varied;

4, overloaded methods can declare a new or broader checked exceptions;

5, or the method can be overridden in a subclass in the same class.

6, not as a type of return value standard to distinguish overloaded function.

Overloaded example:


public class Cat {

    public static void main(String[] args) {
        Cat cat = new Cat ();
        cat.name();
        cat.name("Jack");
    }

    public void name(){
        System.out.println("cat");
    }

    public void name(String newName){
        System.out.println(newName);
    }
}

Having a heavy load and rewrite the above, we need to look at what is down polymorphic it? As well as between it and the heavy load and rewrite what kind of relationship?

Polymorphism

Concept of polymorphism, the same operation is applied to different objects can have different interpretations, produce different execution result.

If you follow this concept to define, then it should be a multi-state operation of the state.

Achieve polymorphism, it requires three conditions:

1, that is inherited classes or interfaces.

2, subclasses to override the parent class method

3, the reference to the parent class pointing object subclass.

More often, most people think that dynamic is also divided into dynamic polymorphism and static polymorphism. The overloaded considered static polymorphism, because he needs to decide which specific method calls at compile time, dynamic static on this argument, I prefer to overloading and polymorphism fact is irrelevant. I think, should be a multi-state operation of properties, Java rewrite is polymorphic expression.

Guess you like

Origin www.cnblogs.com/jinanxiaolaohu/p/11970038.html