Overloading and override abstract classes and interfaces

Overloading and rewrite

Overload

Overload refers to a class, there are two or more methods of the same name, the type parameters of these methods, the number, a different sequence, in which case the method is overloaded. The same type of return value can not be determined whether the return value according to the type of overload.

Rewrite

Rewriting means parent-child classes, subclasses and superclasses have a method name, parameters, and the number of the same type, then the method is subclass overrides methods of the parent class.

Compared

Overload:

  • The same method name, parameters of different types, the number, order
  • The method does not require access to a range
  • Application of the method within a class

Rewrite:

  • To find two compliance with a big two small

    With two: refers to the same method name and parameter list
    for two hours: refers to the return type and subclass declared method abnormally small (or the same) than the parent class
    the parent class subclasses than overridden method access modifiers: a large large (or the same)

  • Application of the parent class and subclass

Abstract classes and interfaces

An abstract class is an abstraction of the source of the abstract is: what.
Interface is an abstract behavior and abstract is: do.

Abstract class

  1. An abstract class is modified with abstract means, which may be internal abstract methods, may be non-abstract methods
  2. Has a structure and method for multiple inheritance can not be instantiated.
  3. Abstract method of the abstract class only method body, no specific implementation.
  4. After the subclass inherits the abstract class, if all the abstract methods of the parent class is not achieved, then the child class is an abstract class can not be instantiated.

interface

  1. Interface refers to the use of modified interface, its interior are abstract methods.
  2. You can achieve more than, and can not be instantiated. No constructor.
  3. Basic data type interface are public static final, rather than an abstract class.
  4. Examples of the interface can not contain fields and static methods (static methods must be implemented, and interface methods are abstract, no method body) (but after the addition of static methods java8)

public static final reason :( and the default static final variable, without displaying the statement)
because the interface can achieve more than by static modification, the variable can be used anywhere , a holistic similar variables, if the implementation class needs to use in the case.
Interface is a manifestation of standardized role, if not final modification, then the realization of a class can be modified, in violation of the principle of normalization. Which prohibits the implementation class to modify it.

java8 Interface New Features

  1. There may be a static method, but it must be implemented and can not be rewritten.
interface Method{
//    int pp();
    static void printM(){
        System.out.println("print");
    }
}

The purpose static existence can be called directly by the class name, so the reason can not be rewritten if a class implements multiple interfaces, and these interfaces have the same name, with the participation of the static method, then the implementation class does not know to call that interface the static method, we are not allowed to use java way "to achieve class interface static method" approach call interface, but the use of "interface interface static method" of. Method.printM();Static methods may be defined and implemented in the class with the same name as the interface parameters, it can not be overridden. And the meaning of existence is similar to the static variable. It can be called from anywhere. You can not be rewritten (final similar variables)
2. The method can default, and it must be implemented. Implementation class can override the default method, but the statement to be displayed for the public methods.

interface Method{
//    int pp();
    static void printM(){
        System.out.println("print");
    }
    
    default void printD(){
        System.out.println("default");
    }
}
class MethidIms implements Method{
    @Override
    public void printD(){
        System.out.println("ims override default");
    }
}

The reason
default method and common method is similar to the meaning of its existence is to implement the method of the class has a default method (similar to the default function), when the implementation class does not override the default method, then the default interface method calls. But when the default is the same method on multiple interfaces, when implementing these interfaces, to override this default method, otherwise I do not know the implementation class to which default interface method to be invoked.

interface Method{
    static void printM(){
        System.out.println("print");
    }

    default void printD(){
        System.out.println("Method1 default");
    }
}
interface Method2{
    default void printD(){
        System.out.println("Method2 default");
    }
}
class MethodIms implements Method,Method2{
    @Override
    public void printD(){
        System.out.println("ims override default");
    }
}
class MethodIms2 implements Method{
}
public class Main {
    public static void main(String[] args){
        MethodIms ms = new MethodIms();
        ms.printD();
        MethodIms2 ms2 = new MethodIms2();
        ms2.printD();
        Method.printM();
    }
}

//输出
//ims override default
//Method1 default
//print
Published 12 original articles · won praise 0 · Views 193

Guess you like

Origin blog.csdn.net/N_a_n/article/details/105021937