The Java interface

Source: Internet
Author: User

1. Interface overview

We know that a dog is a janitor, and a cat is usually a pet. But now there are a lot of trainers and trainers who can train: The cat drills the fire circle and the dog high jump and the dog calculates and so on. These extra actions are not available to all cats or dogs at the outset. This should belong to a special training training. Therefore, these additional actions are not appropriate for defining the action class and are not intended to be defined directly in a cat or dog, as only some cats and dogs have these functions. Therefore, in order to reflect the extensibility of the function of things, Java provides an interface to define these additional functions, does not give a specific implementation, the future of those cats and dogs need to be trained, as long as this part of the cat and dog to achieve this extra function.


2. Features of the interface

Interface with keyword interface representation, format: interface interface Name {}

Class implementation interface is represented by implements, format: class name implements interface name {}

The interface cannot be instantiated.

The subclass of the interface, either as a class or interface, or to override all abstract methods in the interface. The interface is inherited from the interface.

Package com;//defines an animal training interface interface animaltrain{/** * High jump */public abstract void jumps (); Abstract class implementation Interface abstract class Cat implements animaltrain{}//specific class implementation interface class Dog implements animaltrain{@Overridepublic void jump () {System.out.println ("dog is trained to jump");}} public class Interfacedemo {public static void main (string[] args) {}}


3. Member features of the interface

Member variables: can only be constants and are static. public static final

Constructor method: The interface does not have a constructor method. Because the interface is primarily an extension feature, the interface does not require a constructor method.

Member method: can only be an abstract method, and is public. public abstract

All classes inherit from a class by default: Object.

Class Ojbect are the root classes of class hierarchies, and each class uses Ojbect as a superclass.


4. Classes and classes, classes and interfaces, and interface-to-interface relationships

Classes and classes: Inheritance relationships, which can only be single-inheritance, but multiple-tier inheritance.

Classes and interfaces: implementation relationships can be implemented either single or multiple, and multiple interfaces can be implemented at the same time as one class inherits from another.

Interfaces and interfaces: Inheritance relationships, which can be single-inheritance or multiple-inheritance.


5. The difference between an abstract class and an interface

Member Differences:

Abstract classes: Common methods for variable constants abstract methods

Interfaces: Constants Abstract methods

Relationship Differences

Classes and classes: Inheritance single inheritance

Classes and interfaces: Implementing single implementations and multiple implementations

Interfaces and interfaces: Inheritance single inheritance and multiple inheritance

Differences in design concepts

Abstract class: Inherited is the relationship of is a, common function

Interface: Realized is the relationship extension function of like a


6. Case studies

package cn;/** *  interface--action  * */interface Action{/** *  high jump  */public  void jump ();} /** *  abstract Class--Animal  */abstract class animal{private string name;//name Private  int age;//Age Public animal () {}public animal (string name,int age) {this.age =  age;this.name = name;} Public string getname ()  {return name;} Public void setname (String name)  {this.name = name;} Public int getage ()  {return age;} Public void setage (Int age)  {this.age = age;} /** *  Dinner  */public abstract void eat ();/** *  sleeping  */public   void sleep () {System.out.println ("Sleeping");}} /** *  specific Cat class  */class cat extends animal{public cat () {}public Cat (String  name,int age) {super (name, age);} @Overridepublic  void eat()  {system.out.println ("Cat eats Fish");}} /** *  cat  */class jumpcat extends cat implements action{public with high jump function  jumpcat () {}public jumpcat (string name,int age) {super (name, age);} @Overridepublic  void jump ()  {system.out.println ("Cat high Jump");} /** *  Specific Dog class  */class dog extends animal{public dog () {}public Dog (String  name,int age) {super (name, age);} @Overridepublic  void eat ()  {system.out.println ("Dog eats Meat");}} /** *  Dog with high jump function  */class jumpdog extends dog implements action{@ Overridepublic void jump ()  {system.out.println ("Dog high Jump");}} Test class Public class interfacetest {public static void main (String[] args)  {jumpcat jumpcat = new jumpcat ("High jump cat haha",  3); System.out.println ("Name:" +jumpcat.getname () + ", Age:" +jumpcat.getage ()); Jumpcat.eat (); Jumpcat.sleep (); jumpcat.jump ();}} 
package com;/** *  teacher and study case, add extra features of smoking  *  specific: *  teacher: Name   age   eat   sleep  *   Students: Name   Age   eat   sleep  *   because of the commonality of functionality, we extract a parent class: human  *    *   Human: *   name   age  *   eat   sleep  *   *    the extra function of smoking is not a person or a teacher, or a student should have it at first, so we define smoking as interface  *   *   smoking interface  *    Part teacher Smoking: Realizing smoking interface  *   Department student smoking: Realizing smoking interface  *///smoking interface smoking{/** *  smoking   */public void smoke ();} Abstract class-Human abstract class person{private string name;//name private int age;// Age Public person () {}public person (string name,int age) {This.name = name;this.age  = age;} Public string getname ()  {return name;} Public void setname (String name)  {this.name = name;} Public int getage ()  {return age;} Public void setage (Int age)  {this.age = age;} /** *  eating  */public abstract void eat ();/** *  sleeping  */public void  sleep () {System.out.println ("Sleeping");}} Specific teacher Class teacher extends person{public teacher () {}public teacher (String name, Int age) {super (name, age);} @Overridepublic  void eat ()  {system.out.println ("Teacher Vegetarian");}} Specific Student class Class student extends person{public student () {}public student (String name , Int age) {super (name, age);} @Overridepublic  void eat ()  {system.out.println ("Students eat Meat");}} Smoking Teacher Class smoketeacher extends teacher implements smoking{public smoketeacher ( {}public smoketeacher (string name,int age) {super (name, age);} @Overridepublic  void smoke ()  {system.out.println ("Teacher Smoking");} Smoking student CLASS SMOKESTUDENT EXTENDS STUDENT IMPLEMENTS&NBSp Smoking{public smokestudent () {}public smokestudent (string name,int age) {super (name,  age);} @Overridepublic  void smoke ()  {system.out.println ("smoking Student");}} Public class interfacetest {public static void main (String[] args)  { Smoketeacher steacher = new smoketeacher ("Smoking teacher haha",  25); System.out.println ("Name:" +steacher.getname () + ", Age:" +steacher.getage ()); Steacher.eat (); Steacher.sleep (); Steacher.smoke ();}}


This article is from the "11831428" blog, please be sure to keep this source http://11841428.blog.51cto.com/11831428/1859002

The Java 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.