Java Interface Definition (interface) and use (implements)

Source: Internet
Author: User

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
  1. Package com.ws.basic;
  2. Interface gobj{
  3. public abstract void attack ();
  4. }
  5. Class Tower implements gobj{
  6. @Override
  7. public void Attack () {
  8. System.out.println ("All targets within the Arrow Tower attack range, the armor unit has damage bonus!")  ");
  9. }
  10. }
  11. Public class Test {
  12. public static void Main (string[] args) {
  13. Tower T = new tower ();
  14. T.attack ();
  15. }
  16. }

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)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.