Java Learning Personal Memo interface

Source: Internet
Author: User

Abstract class Absdemo{abstract void Show1 (); abstract void Show2 ();}


When a method in an abstract class is abstract, the abstract class can be defined and represented in a different form-----interface interface


The keyword used to define the interface is not class, it is interface.

For common members in interfaces: And these members have fixed modifiers
1. Global constants: public static final
2. Abstract method: Public abstract

This concludes that the members in the interface are public permissions.

Interface demo{public static final int NUM = 4;public abstract void Show1 ();p ublic abstract void Show2 ();}

If the invariant modifier is not written in the interface, the following:

Interface Demo{int NUM = 4;void show1 (); void Show2 ();}

Compile the time will be automatically added to you, so it is also possible to write.
However, it is not recommended to write, affecting the reading.


Implement implements

Interface demo{public static final int NUM = 4;public abstract void Show1 ();p ublic abstract void Show2 ();}

A class is an inheritance relationship between a class and a class, and an implementation relationship between classes and interfaces.

Interfaces cannot be instantiated.
The subclass can be instantiated only by implementing the subclass of the interface and overriding all the abstract methods in the interface.
Otherwise, this subclass is an abstract class.

Class Demoimpl Implements/* implementation */Demo{public void Show1 () {}public void Show2 () {}}class interfacedemo{public static void Ma In (string[] args) {Demoimpl d = new Demoimpl (); System.out.println (D.num); System.out.println (Demoimpl.num); System.out.println (demo.num);//The 3 output method can}}



Multi-Implementation

Multiple inheritance is not directly supported in Java because of the uncertainty of the call.
So Java improved the multi-inheritance mechanism and became a multi-implementation in Java.

A class can implement multiple interfaces.

Interface a{public abstract void Show (); Interface z{public abstract void Show (); Class Test implements A,Z//multiple implementations {public void Show ()//The above two show methods are covered. {}}


A class can also implement multiple interfaces while inheriting another.

Class Q{public void Method () {}}class Test2 extends Q implements a,z{}//interface avoids the limitations of single inheritance class Interfacedemo{public static V OID Main (string[] args) {Test t = new Test (); T.show ();}}

Interface cc{void Show ();} Interface Mm{void method ();} Interface QQ extends CC,MM//interface and interface is an inheritance relationship, can be multiple inheritance, because there is no method body {}



Notebook PC Use
In order to extend the functionality of the notebook, but what happens later on the device does not know.
Define a rule, as long as the devices that appear in the future conform to this rule.
Rules are interfaces in Java.

Interface usb//storm drain rule {public void open ();p ublic void Close ();} Class Bookpc{public static void Main (string[] args) {//usemouse (null); Useusb (new Upan ());//One year later  feature extension//interface type reference for receiving The child class object of the (point) interface. USEUSB (new Usbmouse ()); Use the rule public static void Useusb (USB u) {u.open (); U.close ();}} A year later, bought a U disk//Implementation rule class Upan implements usb{public Void Open () {System.out.println ("Upan Open");} public void Close () {System.out.println ("Upan close");}} This is the pre-opening interface, the latter can be used in the device class Usbmouse implements usb{public Void Open () {System.out.println ("Usbmouse Open");} public void Close () {System.out.println ("usbmouse close");}}

Java Learning Personal Memo 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.