16- abstract classes and interfaces

Abstract class 
if the class definition of a method, but no specific code execution, this method is an abstract method, modified with abstract methods abstract. This abstract method can not perform, so this class must be declared as an abstract class ( abstract  class ) 
abstract class itself is designed to be used only for the succession, forcing subclasses to implement its abstract methods defined. Thus, the abstract methods defined in fact equivalent "standard." 
// the Person class defines the abstract methods run (), then, when implementing a subclass of Student must override run () method 
public  class the Main {
     public  static  void main (String [] args) { 
        the Person P = new new Student (); 
        p.run (); 
    } 
} 

abstract  class the Person {
     public  abstract  void RUN (); 
} 

class Student the extends the Person { 
    @Override
    public  void run () { 
        . the System OUT .println ( " Student.run " ); 
    } 
} 


Note: the parent class Person run () method is not practical sense, can not remove the run () method of the parent class. Removing parent class run () method, the characteristics of the lost polymorphism. 

Abstract-oriented programming 

when we define an abstract class Person, as well as specific Student, Teacher subclass, we can go to the specific reference to an instance of a subclass of the abstract class by Person type, this reference is that the benefits of an abstract class, we its method invocation, the specific subtype is not concerned with the Person type variable. 
This type of possible reference level, to avoid the actual reference subtype, called abstract-oriented programming. 
S the Person = new new Student (); 
the Person T = new new Teacher (); 

personal understanding of: subclass upward transition parent class, whereas the parent class Custom "standard", a specific implementation by subclasses override all, since " the presence of norms ", the entire inheritance tree in exactly the same signature for a certain type of method, then in the specific operation, the only reference to the father, without caring about the specific implementation subclasses. 

interface
In an abstract class, an abstract method is essentially defined interface specifications: high-level interface to a predetermined class that is, to ensure that all subclasses have the same interface, so that, polymorphic able to play power. 
If the field is not an abstract class, all methods are all abstract methods, it can be rewritten as the abstract class interfaces: interface .
abstract  class the Person {
     public  abstract  void RUN ();
     public  abstract String getName (); 
} 
// program to the interface interface 
interface the Person {
     void RUN (); 
    String getName (); 
} 

called interface, is also abstract than abstract class pure abstract interface, even the fields are not there. All methods defined by the interface because the default is public abstract, so two modifiers can be omitted. 
When a specific class to implement an interface, need to use the implements keyword in Java, a class can only inherit from another class, you can not inherit from multiple classes. However, a class can implement multiple interface. 
class Student the implements the Person, the Hello { // implements two interface 
   PrivateName String; 

    public Student (String name) {
         the this .name = name; 
    } 

    @Override 
    public  void RUN () { 
        the System. OUT .println ( the this .name + " RUN " ); 
    } 
} 


The term & 
distinction terms: 
the Java interface especially interface definition, represents the interface type and a set of method signatures, the interface specification refers to programming interfaces, such as method signatures, data formats, network protocols. 
Abstract class interface and Comparative follows: 
------------------------------------------ --------------------------------        
abstract      class interface inheritance can only extends a class implements multiple interface Field may be defined not define an instance field instance field abstract methods defined in an abstract method may be abstract methods defined non-abstract methods defined non-abstract methods defined in the default method
------------------- -------------------------------------------------- ------ interface inherits an interface can inherit from another interface. interface inherited from interface using extends, it is equivalent to extend the methods of the interface.
interface Hello { void hello (); } interface the Person the extends Hello { void RUN (); String getName (); } // the Person interface is now hello Hello interface method may be implemented. └──────── ───────┘ ▲ ┌───────────────────┐ Reasonable grounds inheritance design interface and abstract class, you can fully reuse code. In general, common logic fits in the abstract class, subclass specific logic in each, representative of the level of abstraction interface hierarchy. Inheritance a set of interfaces and abstract classes specific subclass of the Java collection classes defined: ┌───────────────┐ │ │ Iterable Interface ▲ ▲ │ │ Object │ parent ┌───────────────┐ └───────────────────┘ │ Collection │ ▲ └─ │ ──────────────┘ ▲ ▲ ┌───────────────────┐ │ └───────── ─│AbstractCollection │ abstract class ┌───────────────┐ └───────────────────┘ │ │ List ▲ └─ │ ──────────────┘ ▲ ┌───────────────────┐ └──────────│ │ AbstractList └───────────────────┘ │ │ │ │ ┌────────────┐ ┌──────── ────┐ │ the ArrayList │ │ │ the LinkedList concrete subclasses └────────────┘ └────────────┘ when in use, always will be instantiated object could be a specific subclass, but always to refer to it via the interface, as more abstract interfaces (interface callbacks) than the abstract class: List List = new new the ArrayList (); // with reference to specific sub-List interface class Collection List = Coll; // upcast Collection interface Iterable IT = Coll; // upcast the Iterable interface Note: Iterable interface may also call the concrete subclasses. default method in the interface may be defined in the default method. Typically, a subclass inherits from interface needs override all interface methods, but the default method, you do not have a subclass of all changes, you only need to override the new method where needed overwritten. common method of default methods and abstract classes are different. Because the interface is no field, default method can not access the field, while the ordinary method of abstract class can access instance fields. public class Main { public static void main(String[] args) { Person p = new Student("Xiao Ming"); p.run(); } } interface Person { String getName(); default void run() { System.out.println(getName() + " run"); } } class Student implements Person { private String name; public Student(String name) { this.name = name; } PublicGetName String () { return the this .name; } // without overwriting run method }

 

Guess you like

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