In java learning, the use of interfaces (important and common knowledge points) (small records in java learning) and java knowledge points

Source: Internet
Author: User

In java learning, the use of interfaces (important and common knowledge points) (small records in java learning) and java knowledge points

In java learning, the use of interfaces (important and common knowledge points) (small records in java learning)Author: Star)

 

Interface (Function: Used to add extended functions)

Example: pencil and pencil with a rubber band.

Two functions:

1. Write...> pencil

2. Erase words...> eraser

Idea: define a pencil class, define an eraser class, define a pencil class with an eraser class inherit the pencil class and the eraser class

However, java is a single inheritance. So there is a solution: interface (you can add extended functions)

For example: A pencil class, add an interface to it to give it an extension class (eraser class)

Interface Definition Mode

Interface keyword

Structure: interface name {};

How to Implement the interface:

Structure:

Class name implements Interface Name 1, interface name 2, Interface Name 3 .....{

}

Interface function:

1. Extended Functions

2. Program decoupling

Decoupling: the class and interface are not highly correlated. If this class does not implement this interface, it exists independently. If this class does not implement this interface, it is completely unnecessary.

For example, a Student class inherits the Person class, and Student cannot exist independently without a Person.

Therefore, classes and interfaces are a decoupling process, which is often used by the framework.

3. constraints are allowed.

A project is completed by more than one person and has many topics. For example, Netease news headlines are added, deleted, modified, and queried every day. Each person's approach is different, but it may change if someone else maintains it.Therefore, it is necessary to set a method that is the same for everyone. The rules are the same.

Interface Usage considerations:

1. An interface is a special class that can define attribute methods.

2. The attributes in the interface are modified using final by default. Actually a constant: public static final int a = 10;

3. The methods in the interface are abstract: abstract public void run (){}

4. There is no constructor for the interface. You cannot create an object.

5. interfaces are implemented for classes. Methods in interfaces must be implemented when non-abstract classes implement interfaces.

6. An abstract class can implement interfaces without implementing methods in interfaces, but its subclass must implement methods in interfaces.

7. interfaces are inherited from each other. One interface can inherit multiple interfaces.The format is the same as that of A common class. Use interface A extends B, C...

1 package study; 2 3 // define an interface to erase words 4 interface Easer {5 // define a method to erase words 6 // The methods defined in the interface do not need to be implemented, methods In the interface are called abstract Method 7 public void clear (); // The method modified with abstract by default 8} 9 interface penpencil {10 public void write (); 11} 12 13 class eraserandpenpencil implements pencer, Easer {14 // If a class implements an interface, the class must implement the methods in this interface. 15 public void write () {16 System. out. println ("pencil writing"); 17} 18 // java is 19 public void clear () {20 System. out. println ("pencil erasable font"); 21} 22} 23 24 public class star {25 public static void main (String [] args) {26 eraserandpenpencil star = new eraserandpenpencil (); 27 star. write (); 28 star. clear (); 29} 30}

 

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.