Java Novice Note 21 interface

Source: Internet
Author: User

1. Interface

Package Com.yfs.javase;public interface IDemo1 {//interface  interface public/*abstract*/void Method1 ();//default abstract method public} Class Child1 implements IDemo1 {@Overridepublic void method1 () {  //Implement Interface method System.out.println ("Call Method1 ()");}}

2. Interface Example 1

Package Com.yfs.javase;public interface IFly {public void fly ();

3. Implementing the interface

Package Com.yfs.javase;public class Bird implements IFly {@Overridepublic void Fly () {System.out.println ("Bird in flight ....");}} Class Wuya  extends Bird {@Overridepublic void Fly () {System.out.println ("Raven flying ....");}} Class Butterfly implements IFly {@Overridepublic void Fly () {System.out.println ("butterfly in flight .....");}}

4.IRun interface

Package Com.yfs.javase;public interface Irun {int a = 10;//default public static finalpublic void Run ();} /*public*/interface Iswim {}class Horse implements Irun {@Overridepublic void run () {System.out.println ("Horse running ..." + a);}}

5. Interface Multiple Inheritance

The package com.yfs.javase;//interface can inherit more public interface ITest extends IFly, Irun, Iswim {}

6.Person

Package Com.yfs.javase;public class Person {private String name;private int age;private char sex;public person () {}public Person (String name) {this.name = name;} Public person (String name, int age, char sex) {this.name = Name;this.age = Age;this.sex = sex;} public void Introduce () {System.out.println ("I am ..."); Public String toString () {return ' name: ' + name + '  Age: ' + ages + '  Gender: ' + sex;} public void Speak () {System.out.println (name +) did you work? ");} public void Sleep () {System.out.println (name +) did you sleep? ");} public void Eat () {System.out.println (name +) did you eat it? ");} Public String GetName () {return name;} public void SetName (String name) {this.name = name;} public int getage () {return age;} public void Setage (int.) {this.age = age;} Public Char Getsex () {return sex;} public void Setsex (char sex) {this.sex = sex;}}

7. Superman heir, implement Irun,ifly interface

Package Com.yfs.javase;public Class Superman extends person implements Irun, IFly {@Overridepublic void run () {System.out. println ("Superman is running ...."); @Overridepublic void Fly () {System.out.println ("Superman is flying ...");}}

8. Interface Testing

Package Com.yfs.javase;public class Test1 {public static void main (string[] args) {IFly f1 = null;//new IFly ();//interface cannot be instantiated F1  = new Bird (); F1.fly (); f1 = new Butterfly (); F1.fly (); Callmethod (new Wuya ()); Irun r1 = new Horse (); R1.run (); Callmethod (new Superman ());} public static  void Callmethod (IFly f) {f.fly ();}}

9. Test 2

Package Com.yfs.javase;public class Test2 {public static void main (string[] args) {Superman S1 = new Superman (); s1.setname ("Tom"); S1.speak ();        S1.run ();                Callmethod (New Horse ());        Callmethod (s1);} public static void Callmethod (Irun r) {R.run ();}}

Java Novice Note 21 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.