java Note 9-abstract and interfaces

This section talk about abstract interfaces and abstract keywords   abstract classes and methods can be modified

The modified method is called abstract methods : the method is not a method structured body (not braces) method ( Native modified method not part of the body but not an abstract method), a method of abstract class must be changed to abstract class or interface;

abstract modified class is an abstract class abstract class which does not necessarily have abstract methods,

An abstract class can inherit the abstract class and the general category (serious not so dry)
abstract class does not necessarily contain abstract methods, but there are abstract methods of the class must be abstract class.

// interface can not be instantiated and no constructor can not have a block
 // interfaces can inherit multiple interfaces A class can also implement multiple interfaces 



// declare interface when not write abstract default is implicitly abstract 
public  abstract  interface InterfaceAnimal the extends a, B {
     // attribute default interface is modified static final public
     // are final modified attributes need to assign 
    public  static  final String name = "person" ;
     // interface methods are abstract methods are not shared by default also be written abstract public modified
     // class interface implemented method when the return value must be the same 
    public  abstract String EAT (); 

} 


interface a {
     void SLEEP (String name); 

} 

interface B {
    void talk();

}
// abstract class is abstract modified class is called abstract class
 // abstract class can not be instantiated (beginners easy to make the mistake), if it is instantiated, it will error, the compiler can not pass. Only the abstract class non-abstract subclasses can create objects.
// abstract class does not necessarily contain abstract methods, but there are abstract methods of the class must be abstract class. 
abstract  class TestAbstact the implements InterfaceAnimal {
     // abstract class may contain attributes, static variables, constants, block  static block
     String name;
     static String address;
     Final  int Number =. 1 ;
     // abstract class constructor methods, static methods, general methods may comprise, the final modification method
     // abstract class can not call the constructor method can only be inherited by subclasses using
  // abstract constructor method, class methods (modified with static methods) can not be declared. 
    public TestAbstact () {}
     public TestAbstact ( int A) {
        System.out.println (A); 
    } 
    // static method
     // static methods can not be abstract method, the class name or directly call the static method and no method body 
    public  static  void teststaticmethod () { 

        System.out.println ( "I is a static method " ); 
    } 
    // general method 
    public  void Talk () { 
        System.out.println ( " I speak method implementation of the interface, it is now a common approach " ); 
    } 

    // abstract method is an abstract method no method body ({} this method is called the method body has no body content, not to be confused) 
    public  abstract String EAT ();
     // abstract method overloading 
    public  abstract  void EAT (String name);
      //Subclass inherits (or realized) should put more authority to open the parent class (interfaces) abstract methods, an interface all methods are public methods can only be public permission to modify
     // the abstract class inherits the abstract method (or realized) can the following specific implementation do not write code 
    public   void sLEEP (String name) { 
        System.out.println ( "sleeping abstract class method" ); 
    }; 
} 

// subclass ordinary class inherits the abstract class (an interface) must all abstract methods override the abstract methods of the interface contains an abstract class inherits
 // subclass of abstract class must give a specific implementation of the abstract method of an abstract class, unless the subclass is also abstract class. 

class SunTestAbstact the extends TestAbstact { 
    String name = ". 1" ;
     // need to implement the abstract parent class 
    public String EAT () { 
        System.out.println ( "eat subclass method" );
         return "the value back to eat" ;
    } 
    // need to implement the abstract parent class overloads 
    public  void EAT (String name) { 
        System.out.println (name); 
    } 
    // abstract interface methods need to implement the parent class inherits the interface inheritance If the parent class do the implementation subclass does not need to again achieve the 
    public  void sLEEP (String name) { 
        System.out.println ( "subclass inherits the abstract class method sleep" ;) 
    } 
    // interface need to implement the parent class inherits the interface inheritance the method abstract 
    public  void Talk () { 
        System.out.println ( "subclass discussed" ); 
    } 

} 





// general class 
public  class the AbstractClass {
     public  static  void main (String [] args) {
        SunTestAbstact sun =new SunTestAbstact();
        System.out.println("123");


    }

//implements InterfaceAnimal
}

 

1. Definitions A, B and two interfaces 
2, InterfaceAnimal inherited interfaces A, B interfaces
3, inherits the abstract classes TestAbstact InterfaceAnimal
. 4, the general category SunTestAbstact abstract class inherits

Guess you like

Origin www.cnblogs.com/ysmdbk/p/10942751.html