java--interface-Oriented programming

Source: Internet
Author: User

The notes of a book I read earlier last week when I looked at design patterns, I thought about the notes before reading a book.

Interface-oriented programming is an important point is the interface callback, the interface declaration of the variable is called an interface variable, is a reference variable, you can hold a reference to the instance of the class that implements the interface, the object reference.

Interface callback: You can assign a reference to an object created by a class that implements an interface to an interface variable declared by that interface, then the interface variable can invoke an interface method that is implemented by the class.

com com; Interface

Implcom object; Object that implements the interface

com = object; Interface callback, COM implements different interface methods depending on the object, and the callback class overrides the interface method

The interface and the abstract class are compared as follows:

Abstract classes and interfaces can have an abstract method.

There can be only constants in an interface, no variables, and there can be constants or variables in an abstract class.

Abstract classes can also have non-abstract methods, interfaces are not available.

You should design your program to determine whether to use an abstract class or an interface based on a specific analysis. The abstract class provides variables and non-abstract methods that can be inherited by subclasses in addition to the important abstract methods that need to be rewritten. If an important problem requires inheritance for better resolution, for example, subclasses need to override the abstract method of the parent class, and also need to inherit some variables from the parent class or inherit some important non-abstract methods, you can consider using the abstract class. If a problem does not require inheritance, but requires several classes to give the implementation details of some important abstract methods, you can consider using an interface.

The most important core idea to use interface programming is to use interface callbacks and interface variables to hold references to objects that implement the interface's classes, so that interface variables can be called back to the interface methods implemented by the class.

1 234 public  interface  advertisement {      public  void  showadvertisement ();      public  string getcorpname ();

2, Design Advertisementboard Class (Billboard), The class has a show (advertisement Adver) method, the parameter of the method is the type of the interface advertisement, it is obvious that the parameter adver can hold any reference to the object of the class that implements the advertisement interface, The callback class overrides the interface method Showadvertisement () to display the company's advertising words, and the callback class overrides the interface method Getcorpname to get the company name.

123456 publicclass AdvertisementBoard{    public voidshow (Advertisement adver){        System.out.println(adver.getCorpName()+"广告词");        adver.showAdvertisement(); //接口回调    }}

A company class that implements the interface:

12345678 publicclassAcorp implement Advertisement {    publicvoidshowAdvertisement(){        System.out.println("AAAAAAAAAAAAAAAAA");    }    publicString getCorpName(){        return"A Corp";    }}

The B Company class that implements the interface:

12345678 publicclassBcorp implement Advertisement {    publicvoidshowAdvertisement(){        System.out.println("BBBBBBBBBBBBBBBBB");    }    publicString getCorpName(){        return"B Corp";    }}

Run Live program:

1234567 publicclasstest (){    publicstate voidmain (string args[]){        AdvertisementBoard board = newAdvertisementBoard();        board.show(newAcorp());        board.show(newBcorp());    }}

The final program calls different methods depending on the object. If you want to add Company C, just implement the method of the interface.

java--interface-Oriented programming

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.