[Java]--Abstract Class vs Interfaces

1. Abstract Class

Definition

  1. An abstract class is a class that is declared abstract—it may or may not include abstract methods.
  2. An abstract classes cannot be instantiated.
  3. An abstract classes can be subclassed.
  4. If a class include abstract methods, the class itself must be declared abstract.
    Some Code example:
public abstract class GraphicObject {
   // declare fields
   // declare nonabstract methods
   abstract void draw();
}

2. Abstract Methods

The subclass of the abstract class must implement all the abstract methods in its parent class. IF NOT, then the subclass must be declared abstract too

3. Interface

  1. Interface cannot be instantiated either.
  2. Interface may also contain a mix of methods declared with or without implementation .
  3. All fields are automatically public, static, and final.
  4. All methods that you declare or define (as default methods) are public.
  5. Methods in an interface (see the Interfaces section) that are not declared as default or static are implicitly abstract, so the abstract modifier is not used with interface methods. (It can be used, but it is unnecessary.)

Reference

  1. https://docs.oracle.com/javase/tutorial/java/IandI/abstract.html
  2. http://stackoverflow.com/questions/1320745/abstract-class-in-java

你可能感兴趣的:(Java)