JAVA basis (twelve) abstract classes and interfaces

Abstract class

to sum up:

                Class supports single inheritance. Interface supports multiple inheritance

                Class and class single inheritance,

                Class implements an interface, the interface interface inheritance, succession after the first realization

 

   Their understanding of abstract categories: general description, vague, not concrete classes have in common 

Abstract class format 

Public abstract  class class name { 

     member variables 

// methods 

    public   abstract      return type method name (parameter); 

    Public    abstract          void     GET (); // abstraction function. Abstract need modification, and semicolon; end 

}

 

 

1.1 abstract class features:

                         1, abstract classes and abstract methods need to be modified abstract. Abstract method must be defined in an abstract class.

                         2, abstract class can not directly create objects, reason: calling abstract method does not make sense.

                         3, only covers all of the abstract methods in an abstract class, subclass can create objects. Otherwise, the sub-class or an abstract class.

                          4. The reason inherit the abstract class, more so in thought, in the face of common types of operations will be easier.

 

Abstract thought

                     1. abstract class must be a parent class, an abstract class as needed to cover all the abstract methods which subclasses, subclasses can be instantiated objects

                      2. abstract class may not define the abstract methods.                                                                                                        

                      3. Once a class inherits the abstract class.                                                                                                           

                                       3.1  subclass or override the parent class method, to clarify its function

                                       3.2     or continue to go on abstract

       1、private:

                Private subclass inherits is unable to, there was no coverage, using a modified method abstract and private together, abstract it is necessary to subclass a method to achieve this, and private modification subclass can not get the parent class method. contradictory.

                                             2, final, temporary does not care about 3, static, not being concerned

 

The difference between abstract classes and ordinary classes

Abstract class:            1. either define abstract methods, general methods may be defined

                             2. can not be instantiated                                            

                           3. Description of things may be insufficient information                           

Common categories:           1. Only normal function defined                                        

                           2. can be instantiated                                                

                          3. Describe things in great detail                                      

 

                           interface

Own understanding

                       An abstract class is the reality of things description (vague description of common features);

                      Interface is a collection of extra features

Establish interface definition format: interface   keyword

public  interface interface name { 

                 abstract method 
                abstract method 2 
                abstract method 3; 

}                                               

pseudocode 

public  interface . Drink. {
     // abstract methods
      public  abstract  void   Drink (); 

}

                           Use interface instead of the original class,

   Other steps are the same as defined categories:

                        1. interface are abstract methods public access

                        2. The interface can not define a normal member variables

 

Class implements the interface

class class implements the interface { 

   interface method override 

} example

 class subclass extends parent class implements the interface {   override the parent class method         override interface method 
} 
pseudocode 

     




 


public  class Student implements smoking,. Drink. (interface supports multiple inheritance) { 

        ( method override interface) 

     public  void Smoke () { 

            System.out.println ( "student" ); 

  } 
}
in the class implements the interface, the abstract class will be inherited interface method, then the class need to re write the abstract method, to complete the specific logic.

 

 

Features members:

                    1, the interface can define a variable, but must have a fixed variable modifiers modified, public static final variables interface so called constant value can not be changed. Later we will explain the static and final keywords

                     2, the interface can be defined method, there are a fixed method modifiers, public abstract

                     3, the interface can not create the object.

                      4, all subclasses must override the abstract interface methods, subclasses can be instantiated. Otherwise sub-class is an abstract class.

 

Summary: Interface it benefits the development of

1, extends the functionality of the interface appears.

2, the interface is actually rule (method) escaping violence.

3, the interface appears to reduce coupling (the degree of integration), i.e. to achieve a decoupling between the device and the device.

 

Guess you like

Origin www.cnblogs.com/layuechuquwan/p/11287746.html