Directory
- Abstract class
- Interface
1 Abstract class Definitions
If a class does not have enough information to describe a specific object and needs other concrete classes to instantiate the class, we call it an abstract class.
Characteristics of abstract Classes
- An abstract class cannot be instantiated, but it can refer to a child class object.
- Subclasses must implement abstract methods of the abstract class. If the subclass is also an abstract class, you can not implement methods of the abstract class.
- An abstract class can contain specific methods, or it can contain abstract methods.
- The abstract method in the subclass cannot have the same name as the abstract method of the parent class.
- Abstract cannot decorate the same class with final.
- Abstract cannot be decorated with private, static, final, or native side by side.
Example
Public Abstract class test{ // attribute public int var = 0; // abstract Methods Public Abstract void hello (); // Specific Methods Public void hello2{ System.out.println ("Hello2"); } }
2 Interface Definitions
- Interfaces are not classes, and the way they are written is similar to classes, but they are different concepts. Class describes the properties and methods of an object. The interface contains the methods to be implemented by the class.
- Unless the class that implements the interface is an abstract class, the class defines all the methods in the interface.
- The interface cannot be instantiated, but it can be implemented. An abstract class can inherit an interface but not implement a class in an interface.
- An interface type can be used to declare a variable. As a reference.
- The interface describes the local part of a specific object.
Interface Features
- Each method in the interface is implicitly abstract, and the methods in the interface are implicitly designated as public abstract (only public abstract, and other modifiers will be error-marked).
- The interface can contain variables, but the variables in the interface are implicitly designated as public static final variables (and can only be public, and will be error-marked with private decoration)
- The methods in an interface cannot be implemented in an interface, but only by the classes that implement the interface.
Example
Public Interface test{ // Property public staticfinalint var = 1 ; // Method Public void // implicitly defined as public abstract}
The difference between an abstract class and an interface
- A method in an abstract class can have a method body, which is a specific function that can implement a method, but the method in an interface is not.
- member variables in an abstract class can be of various types, whereas member variables in an interface are only public static final types.
- An interface cannot contain static code blocks and static methods, whereas abstract classes can have static code blocks and static methods.
- A class can inherit only one abstract class, and a class may implement multiple interfaces.
Reference:
Http://www.runoob.com/java/java-interfaces.html
[Java] Abstract classes and interfaces