[Think in Java] Chapter 9th interface

Source: Internet
Author: User

9.1 Abstract classes and abstract methods

When applying inheritance, many of the methods of the base class are generic interfaces of subclasses, which themselves have no practical meaning. There is no point in creating a base class object, and we even need to organize users to do so. However, if the user does this, we will not find the error at run time. In order for the error message to be captured at compile time, we can define the base class as an abstract class.

Abstract keyword for Abstract,java over the abstract method mechanism, this method is incomplete; only declarations have no method body. Such as:

abstract void f ();

classes that contain abstract methods are called abstract classes. If a class contains one or more abstract methods, the class must be qualified as abstract . If you inherit from an abstract class and want to create an object for the new class, you must provide a method definition for all the abstract methods in the base class. Otherwise the class is still abstract, and the compiler will force you to add the abstract keyword.


9.2 Interface

The keyword for the interface is interface, which produces a completely abstract class. The interface provides only the declaration of the method, not the method body. Interfaces are used to establish the protocol between classes.

The key interface is like creating a class, except that you need to replace the Class keyword with interface. You can add the Public keyword before interface (only for files with the same name as the interface, which is similar to classes). If you do not add the public keyword, only the package access permission. In addition, interfaces can contain domains, but these fields are implicitly static and final , and the fields in the interface are automatically public.

The methods defined in the interface must be public, even if they are not explicitly shown, they are public by default.


multiple inheritance in 9.4java

Inheritance in Java is single-inheritance, but if you need to inherit the interface from multiple places, you can choose to implement it with an interface. The Implement keyword can inherit multiple interfaces. Here is an example.

Interface canfight{void fight ();//default is Public}interface canswim{void swim ();} Interface Canfly{void fly ();} Class Actioncharacter{public void Fight () {}}class Hero extends Actioncharacter implements Canfight, Canfly, canswim{// The fight method inherits from the Actioncharacter. public void Swim () {}public void Fly () {}}public class Adventure {public static void T (Canfight x) {x.fight ();} public static void U (Canswim x) {x.swim ();} public static void V (Canfly x)  {x.fly ();} public static void W (Actioncharacter x) {x.fight ();} public static void Main (String [] args) {Hero x = new Hero (); t (x); u (x); v (x); W (x);}}

hero when combining specific classes and interfaces, specific classes must be placed in front , followed by interfaces. (otherwise the compiler will error).

This brings us to the question, should we use interfaces or abstract classes? If you want to create a base class without any method definitions and member variables, you should select an interface instead of an abstract class. In fact, if you know that a food should be a base class, our first choice should be to make him an interface.


9.5 Interface Inheritance can be used to extend the interface9.8 Nested Interfaces

Several sockets can be nested within a class or other interface. It is important to note that when implementing an interface, you do not need to implement any interfaces nested within it. And the private interface cannot be implemented outside of the class that defines it .


When designing, the proper principle should be to prefer the class rather than the interface. Starting with a class, refactoring if the interface's necessity becomes very clear. Interface is an important tool, but it is easy to be abused.




[Think in Java] Chapter 9th interface

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.