To understand the interface we need to know the abstract class first. So what is abstract class?
problem Description: In life we have a lot of objects can not be specifically described, such as: we can say that the four-sided shape has four sides. Or the concrete point is that the rectangle is symmetrical and equal, and the square is symmetrical and equal. However, it is difficult to describe the normal graphics in detail. Converting to the Java language means that we can easily customize its properties and methods for a very specific class , but how it is implemented when there are some classes that we find difficult to understand. At this point we can use abstract classes.
The syntax format for abstract classes is as follows:
1 Public Abstract class class Name {2 Abstract return value type Method Name (parameter list); // defining abstract methods 3 }
The keyword that defines the abstract method is: abstract.
Classes defined with abstract are called abstract classes, whereas methods defined using abstract are called abstract methods.
Abstract methods do not have a method body and do not have any meaning in themselves, so abstract classes can not be instantiated.
When it is inherited by its subclasses, subclasses must implement all the methods in them.
As long as there is an abstract method in the class, this class must be defined as an abstract class.
Understanding the abstract class, let's continue to think about the following question, and let's look at the following code:
Public Abstract classShape {//defining a graphical abstract class Public Abstract voidToString ();//ToString Abstract Method Public Abstract voidPaint ();//abstract method of drawing} Public classTriangleextendsshape{ Public voidtoString () {System.out.println ("ToString"); } Public voidpaint () {System.out.println ("Paint"); }}
It looks like there's nothing wrong with that. What if we don't want it to have a paint method in some classes? Put the paint method in another class? But other classes need to use this method as well. And the Java syntax stipulates that the class is not more inherited, how to do? Then we can consider using the interface.
The interface is an extension of the abstract class, which solves the disadvantage that Java cannot inherit more. We can encapsulate the paint method into an interface, and then let the subclass of the graphics class we need to implement this interface. Whereas a class in Java can implement multiple interfaces, we can encapsulate different methods in the interface and give it to the class.
The key word for the interface is interface. It is defined as follows:
Public interface Interface name extends class name implements interface,.... { // define constants Public Static final data type constant name = value; // defining abstract methods Public Abstract returns the value type method name (parameter list); }
Note that: 1. The interface cannot be instantiated 2. The access modifier for an interface can only be public 3. The public abstract static final keyword is provided by default on the interface
PS: Small White Road, welcome to correct.
Abstract classes and Interfaces in Java