Java Tour _ Object-oriented _ interface

Source: Internet
Author: User

Reference excerpt: http://www.runoob.com/java/java-interfaces.html

An interface (interface) is an abstract type in Java and a collection of abstract methods.

A class implements all the abstract methods in an interface through the implements inheritance interface. (except abstract classes)

declaring interfaces:

Interface animal{    publicvoid  eat ();      Public void Travel ();}

The interface has the following characteristics:

1. Interfaces are implicitly abstract and do not need to use the abstract keyword when declaring an interface

2. Each method in the interface is implicitly abstract, and the declaration does not require the abstract keyword

3. The methods in the interface are all public

Implementation of the interface:

class implements the interface using the Implements keyword.

When a class implements an interface, it is necessary to implement all the methods in the interface, otherwise the class must be declared as an abstract class

Instance:

 Public classDogImplementsanimal{ Public voideat () {System.out.println ("Dog Eat"); }     Public voidTravel () {System.out.println ("Dog Travel"); }     Public voidOthermethod () {System.out.println ("This class method"); }     Public Static voidMain (string[] args) {Dog d=NewDog ();        D.eat ();    D.travel (); }}
Operation Result:
Dog Eatdog Travel

Inheritance of interfaces:

Using the extends keyword, similar to inheritance between classes

//file name: Sports.java Public Interfacesports{ Public voidSethometeam (String name);  Public voidSetvisitingteam (String name);}//file name: Football.java Public InterfaceFootballextendssports{ Public voidHometeamscored (intpoints);}//file name: Hockey.java Public InterfaceHockeyextendssports{ Public voidhomegoalscored ();}

The football interface itself declares a method, inherits two methods from sports, so that the class implementing the football interface needs to implement 3 methods

Multiple Inheritance of interfaces:

The class cannot inherit more, but the interface can

 Public Interface extends Sports,football

Tag interface:

Interfaces that do not have any methods are called labeled interfaces.

 Package Java.util;  Public Interface eventlistener{}

Java Tour _ Object-oriented _ 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.