Abstract classes, interfaces and final

  1. What is an abstract class, it is used in what time?
  2. What is the interface? What is its role?
  3. What final, what role?
Abstract class
1. When the methods of the parent class is not sure what time, can use the abstract keyword the method [abstract method] modified, modified with abstract class [abstract]. The method not only achieved statement.
 
2. The subclasses going to implement abstract methods of its parent class!
abstract  class Animal { 
    String name; 
    int Age;
     abstract  public  void Cry (); // Do not add this later {}, it would be achieved! 
} 
 
// When a class inherits the parent class is an abstract class, then, we need to abstract methods abstract class all realize! 
class Cat the extends Animal { 
    @Override 
    public  void Cry () {
         // do Nothing ... 
    } 
}

 

 
3. With regard to the abstract class need to know that:   
  • An abstract class is not instantiated, it is not new
  • An abstract class may contain attributes, methods, construction method, but the method can not be used to construct new instances (not originally new abstract class), it can only be used by subclasses to call !
  • An abstract class can only be inherited
  • An abstract class does not have to contain abstract methods. If the abstract class contains an abstract method, then the method may not have any abstract implementation (even in the subsequently added nor {}), and subclasses of the abstract class must implement the abstract methods
  • Once the class contains abstract methods, the class must be declared as abstract!
  • Abstract methods abstract class must be no method body! {} Have not
  • Subclass inherits the abstract class must implement the abstract methods of the parent class!
Meaning of abstract classes are: to provide a unified sub-class, standard template.
 
"Class can not implement all methods of abstract classes and interfaces declared, of course, in this case, the class also have to be declared as abstract." This sentence is correct, how to understand it? See examples:
package com.testabstract;
abstract class Ab_stract {
       abstract void fun1() ;
       abstract void fun3();
       abstract void fun4();
       public void fun2() {
             System.out.println("Hello World");
       }
}
public abstract class TestAbstract02 extends Ab_stract{
       public void fun1() {
             System.out.println("Hi!Java");
       }
       
       public voidfun2 () { 
             System.out.println ( "the Hello" ); 
       } 
       
       // if fun4 () method does not intend to realize the words are possible, but this class is necessary for your TestAbstract02 abstract 
}
So indeed we can not implement all abstract methods abstract parent class, but this time your subclasses must be declared as abstract abstract class
 
"An abstract class can implement the interface without providing an interface method implementation." This sentence, how to understand it? See examples:
package com.testabstract;
interface Bbb{
       public void function1();
       public void function2();
       public void function3();
}
abstract class Aaa implements Bbb{
       abstract void fun1() ;
       abstract void fun3();
       abstract void fun4();
       public void fun2() {
             System.out.println("Hello World");
       }
}
Abstract class implements the interface Aaa Bbb, but which is not a method to implement any interfaces Bbb in, but Eclipse has not reported any mistakes, why? The original is because the interface method, originally the abstract. In short, it is an abstract class which do not have to implement the interface methods in the abstract class implemented.

Modifiers can not be put together: Final and abstract, Private and abstract, static and abstract, since abstract methods that must be implemented modified in its subclasses (cover) in order to invoke the polymorphism, in the modified method of the above modifier subclass period are not covered by this method, final is not covered, private inheritance is not able to subclasses, and therefore would not be covered, static byte code as part of the static, the object does not need to run, but there is no way abstract method body, running does not make sense, it can not coexist
 

 
Interfaces (abstract in the abstract)
1. Interface syntax:
interface interface name { 
        method declarations; // method can not have a body! Just a statement! 
}
 
2. The implementation of the interface method:
implements the interface name
 
3.
  • When a class implements an interface, it requires that all such methods to achieve all of the interfaces (abstract classes and this is similar)
  • Interfaces can not be instantiated (this is an abstract class and the like)
  • Interface methods are public by default
  • Interface methods can not have a method body! I.e., it can not be achieved; abstract class only an abstract method can have a method thereof, a method can not abstract a method thereof.
  • A class can implement multiple interfaces, this is the multiple inheritance
  • Interfaces may be variable! Note, however, that variable can not be modified by private and protected, and essentially static, regardless of whether you add the static keyword. But also final. That is, the default is public static final, if the constant interface to be accessed directly using the "Interface name. Constant name" to
  • In Java development, we often common variables defined in the interface, the use of global variables as
  • An interface can not inherit from other classes, but to inherit other interfaces, namely interface to inherit more than one interface
 
The difference between inheritance and interfaces
Java is the only single inheritance, but can implement interfaces and more, in order to achieve multiple inheritance. Inheritance is level. Implements the interface can be seen as an extension to the inheritance
 
Interface using polymorphism:
interface Car { 
    String getName (); 
    int the getPrice (); 
} 
 
class the BMW the implements Car {
     // implements an interface going to implement a method interface 
    public String getName () {
         return "the BMW" ; 
    } 
    public  int the getPrice () {
         return 300000 ; 
    } 
} 
 
class CheryQQ the implements Car {
     // implements an interface going to implement a method interface 
    public String getName () {
         return "CheryQQ" ; 
    } 
    public  intthe getPrice () {
         return "200000" ; 
    } 
} 
 
public  class CarShop {
     Private  int Money = 0 ; 
 
    public  void sellCar (Car CAR) { // focus here, there is an interface 
        System.out.println ( "model:" + car.getName () + "Price:" + car.getPrice ()); 
        Money + = car.getPrice (); 
    } 
 
    public  int getMoney () {
         return Money; 
    } 
    public  static  void main (String args []) { 
       CarShop C = new new CarShop ();
       CQ CheryQQ = new new CheryQQ ();
             // CQ is CheryQQ type 
       c.sellCar (CQ); 
    } 
}

 

 
All methods may not be implemented abstract class and interface declarations of the classes, of course, in this case, the class must have to be declared abstract.
 
final keyword:
final variable or method can be modified
In some cases, programmers may have the following requirements:
1. When you do not want a class quilt cover method (override) the parent class, you can use the keyword final modification
2. When the value of a variable do not want a class to be modified, can be modified with a final (final variables and methods can be inherited!)
3. When not want the class to be inherited, can be modified with final
 
In other words:
  • The modified final class can not be inherited
  • When the method of the parent class is a final modification, not subclasses to override it
  • The modified final a variable, its value can not be modified
final use items should be noted:
  • When using the final, it should be noted, final modified variables must be given to the initial value when you define! After the first initial value can not be defined!
  • final modified variable called constants, usually with xx_xx_xx named
 
 

Guess you like

Origin www.cnblogs.com/Kundalini/p/11707777.html