------Abstract class of Java

Source: Internet
Author: User

Enclosing image abstract class and its subclasses

Area.java:

Public interface area{public   double Area ();}


Circle.java:

Public final class Circle extends Ellipse        //Circle class, End class {public    circle (double radius)                 //constructor method, radius parameter specifies radius of circle    {        super (Radius,radius);        This.shape = "Round";    }    Public Circle ()    {This        (0);    }    Public String toString ()    {        return "radius" +this.radius_a;    }    public static void Main (string[] args)     {        new Circle (). print ();    }}


Closedfigure.java:

Public abstract class Closedfigure//Closed graph abstract Classes {protected String shape;    Shape protected closedfigure (String shape)//constructor method, cannot be abstract method {this.shape = shape;    } protected Closedfigure () {This ("unknown");               } public abstract double area (); Calculate area, abstract method, with semicolon ";"          Ends public abstract double perimeter (); Calculate perimeter, abstract method public void print ()//Display shape, property, perimeter, and area {System.out.println ("one" +this.shape    + "," +this.tostring () + ", Circumference is" +this.perimeter () + ", area is" +this.area () ");  }}class closedfigure_ex {public static void main (String args[])//abstract class can contain the main () method {Closedfigure g =     New Ellipse (10,20);                               G Reference Ellipse object G.print ();                      Show Ellipse Properties g = new Circle (10);        Round G.print ();                g = new Rectangle (10,20);                               G Reference Rectangle object G.print ();Display Rectangle Property g = new Square (10);    Square g.print (); }}


Ellipse.java:

public class Ellipse extends closedfigure              //Ellipse class {    protected double radius_a;                   A-axis radius    protected double radius_b;                   B-Axis radius public    Ellipse (double radius_a, double radius_b)//construction method    {        super ("Ellipse");        This.radius_a = radius_a;        This.radius_b = Radius_b;    }    Public Ellipse ()    {This        (0,0);    }        Public String toString ()    {        return "A-axis radius" +this.radius_a+ ", B-Axis radius" +this.radius_b;    }        Public double area ()                         //computes the ellipse size, overriding the parent class's abstract method    {        return math.pi*this.radius_a*this.radius_b;    }    Public double perimeter ()                    //computes the perimeter of the ellipse, overriding the abstract method of the parent class    {        return math.pi* (this.radius_a+this.radius_b);    }    public static void Main (String args[])    {        new Ellipse (10,20). print ();    }}


Rectangle.java:

public class Rectangle extends Closedfigure//Rectangle class {protected double length;                      length protected double width;        Width public Rectangle (double length, double width)//constructor method {Super ("rectangle");        this.length = length;    This.width = width;    } public Rectangle () {this (0,0);    } public Rectangle (Rectangle R) {This (r.length, r.width);    The public String toString () {return "Length" +this.length+ ", width" +this.width;    } public double area ()//calculates the rectangle size, implementing the parent class's abstract method {return this.width*this.length;    } public double perimeter ()//calculates the perimeter of the rectangle, implementing the abstract method of the parent class {return (this.width+this.length) * *;    } public void SetLength (double length) {this.length = length;    } public void SetWidth (double width) {this.width = width;    } public double GetLength () {return this.length; } public DoubLe getwidth () {return this.width;    } public static void Main (String args[]) {new Rectangle (10,20). print (); }}


Square.java:

Public final class Square extends Rectangle      //Square is a special case of rectangle {public    square (double width)    {        Super (width, width);        This.shape = "Square";    }    Public Square ()    {This        (0);    }        Public String toString ()    {        return "edge length" +this.length;    }    public static void Main (string[] args) {        new Square (). print ();}}


------Abstract class of Java

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.