Interfaces in Java

Source: Internet
Author: User

I. Purpose of using interfaces (interface)

Java only supports single inheritance, that is, a class can inherit at most one direct parent, and the main function of the interface is the ability to implement multiple inheritance similar to a class.

Two. Nature of the interface

1. The interface has inheritance, that is, the sub-interface inherits the properties and methods of the parent interface. And you don't want a class to inherit only one parent class, an interface can inherit multiple parent interfaces.

2. The methods in the interface are abstract methods.

3. To implement an interface for a class, this class implements an abstract method that is not implemented in the interface.

Example:

 PackageL1105;Interfacetestimplement { Public voidSun ();//defining an abstract method in an interface    }classRiverImplementstestimplement{//declare the interface that this class implements by using the Implements keyword in the class     intA;  PublicRiverinta) {          This. a=A; System.out.println ("Hello today is" +a); }           Public voidSun () {//implementing methods that are not implemented in an interface in a classSystem.out.println ("This is a dog"); }        }classmoon{ Public Static voidMain (String args[]) {testimplement im1=NewRiver (10);            Im1.sun (); }}
examples of use of interface

Interfaces in Java

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.