1. In the declaration of a class, create a subclass of a class with the keyword extends. A class declares itself to use one or more interfaces through the keyword implements.
Extends is inherited from a class, you can use the parent class method after inheritance, or you can override the parent class method; Implements is to implement multiple interfaces, the method of the interface is generally empty and must be rewritten to use
2.extends is inherited from the parent class, as long as the class is not declared final or the class is defined as abstract can inherit, Java does not support multiple inheritance, but can be implemented with interfaces, so it is necessary to use implements, inheritance can inherit only one class, But implements can implement multiple interfaces, separated by commas.
In contrast to abstract classes, interfaces are abstract classes that are more abstract than abstract classes, without the slightest realization of the content.
Interfaces can be constrained by more canonical sub-classes.
The interface is fully professionally implemented: separation of specifications and specific implementations.
An interface is a specification, defined by a set of rules, that is presented in the real world "if you are ... It must be ... "thought. If you are an animal, you must be active.
The essence of an interface is a contract, just like the law. Everyone will abide by it when it is well established.
The specific needs of the project are changeable, we must status quo to calmly develop, here "unchanged" is "specification".
Therefore, our development projects are often interface-oriented programming!
Example:
[Java]View PlainCopy
- Package com.ws.basic;
- Interface gobj{
- public abstract void attack ();
- }
- Class Tower implements gobj{
- @Override
- public void Attack () {
- System.out.println ("All targets within the Arrow Tower attack range, the armor unit has damage bonus!") ");
- }
- }
- Public class Test {
- public static void Main (string[] args) {
- Tower T = new tower ();
- T.attack ();
- }
- }
Definition of the interface:
There are only constants and abstract methods in the interface:
Constants are public static final
Method is public abstract
If it is not written, it is the default.
Implements
Implements is a class that implements an interface with a keyword that is used to implement an abstract method defined in an interface.
Java does not have many inheritance, in order to compensate for the shortcomings here, the use of interface.
The interface definition in Java is basically a class that is defined in a class like C + + that contains a real virtual function, which is an abstract class in C + +.
An interface cannot create an instance, but can be used to declare a reference variable type.
A class implements an interface and must implement all the methods in the interface.
The interface supports multiple inheritance, for example: Class A extends B implements C,d,e
Differences between interfaces and classes:
An interface cannot be used to instantiate an object.
Interface has no constructor method.
All methods in an interface must be abstract methods.
An interface cannot contain member variables, except static and final variables.
Interfaces are not inherited by classes, but are implemented by classes.
The interface supports multiple inheritance.
The difference between an interface and an abstract class:
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.
Interfaces cannot contain static blocks of code and static methods (methods that are decorated with static), whereas abstract classes can have static code blocks and static methods.
A class can inherit only one abstract class, while a class may implement multiple interfaces.
Main applications of the interface:
Multi-state interface dynamic loading;
The interface is passed as the parameter of the method;
Generates a constant group.
Java Interface Definition (interface) and use (implements)