Think in Java (vi): interface

Source: Internet
Author: User
Tags multiple inheritance in java

1. Multiple inheritance in Java
In C + +, the behavior of an interface that combines multiple classes is called multiple inheritance.
In Java a class can inherit only once, but multiple interfaces can be implemented, note that inheritance must be written before implementation

2. Core reasons for using the interface:
1) in order to be able to transition upward to multiple parent types (and the resulting flexibility)
2) Prevent programmers from creating objects of this class

3. Policy design mode:

Pass different Stringprocessor implementation classes and will do different things separately

Public abstract class Stringprocessor implements Processor {public String name () {return getclass (). Getsimplename (); public abstract string process (Object input);p ublic static string s = "If she weighs the same as a duck, she ' s made of Woo D ";p ublic static void Main (string[] args) {apply.process (New Upcase (), s); Apply.process (New Downcase (), s); Apply.process (New Splitter (), s);}} Class Upcase extends Stringprocessor {public String process (Object input) {//Covariant Returnreturn ((String) input). ToU Ppercase ();}} Class Downcase extends Stringprocessor {public String process (Object input) {return (String) input). toLowerCase ();}} Class Splitter extends Stringprocessor {public String process (Object input) {return arrays.tostring ((String) input). Split (""));}}


Think in Java (vi): interface

Related Article

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.