Java Advantage interface to implement polymorphic situations, and the use of interfaces to achieve multiple inheritance!

Source: Internet
Author: User

It is the use, square, rectangle, circle, to inherit the interface all interface, to achieve the case of all.

Talk less nonsense, on the code:

Import Java.util.*;interface s{void S ();} Interface C{void C ();} Interface all extends s,c{//This is the multiple inheritance that implements the interface}class Square implements all{private double length = 1;public void S () {System.out. println ("Square area is:" +length*length);} public void C () {System.out.println ("square perimeter is:" +length*4);}} Class Rectangle implements all{private double length =1;private double width = 2;public void S () {System.out.println ("rectangular area is : "+width*length);} public void C () {System.out.println ("Rectangle circumference is:" + (length+width) * *);}} Class Circle implements All{private double r = 1;public void S () {System.out.println ("Circle area is:" +r*r*3.14);} public void C () {System.out.println ("round perimeter is:" +2*r*3.14);}} public class Sandc {public static void result (all a) {A.S (); A.C ();} public static void Main (string[] args) {result (new Square ()); Result (new Rectangle ()); Result (new Circle ());}}
Print out the results, can be tested, can achieve multiple interface multiplexing, effectively implemented, the mechanism of flushing.


This is, Java example, hope to help everyone!


Small red Flag

qq:651134397

Java Advantage interface to implement polymorphic situations, and the use of interfaces to achieve multiple inheritance!

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.