Interface Definition in Java 8 and Java 8 Interface Definition

Source: Internet
Author: User

Interface Definition in Java 8 and Java 8 Interface Definition

Define an interface. If there is a class to implement this interface, you must implement the methods defined in this interface; if the interface has been written, the class is also written, but if the interface is modified again, a method is added, before Java 8, you must change the interface implementation class. There are two ways to change the class;

1. Add an abstract class. The newly added method www.twitterchina.net in the abstract class interface is used to enable the Implementation class of the original interface to inherit the virtual class.

2. Modify the implementation class of the interface and implement the newly added method in the interface.

Of course, the first method is better, which may have less impact on the project.

 

You do not need to modify the implementation class of the interface in Java 8.

Public interface Annimal {

Void setType (String type );

String getType ();


// String getAnnimalSex (); // written before Java 8
// Write in Java 8
Default String getAnnimalSex (){
Return "1 ";
};

 

// Implementation class
Public class Dog implements Annimal {

@ Override
Public void setType (String type ){
// TODO Auto-generated method stub

}

@ Override
Public String getType (){
// TODO Auto-generated method stub
Return null;
}

// @ Override
// Public String getAnnimalSex (){
/// TODO Auto-generated method stub
// Return null;
//}

}

}

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.