Java abstract design and abstract methods

Source: Internet
Author: User

/* Abstract design and abstract methods using abstract adornments and methods that have no method body, called abstract methods *//*//graphics class graph{    public double  getares ()     {        return 0.0;     }}//round Class circle extends graph{    private integer  r;    circle (Integer r)     {         this.r = r;    }    //Area      public double getares ()     {         return 3.14 * r * r;    }}//Rectangle Class rectangles  extends Graph{    private Integer witdh;     Private integer beight;        rectangles (INTEGER WITDH ,  integer beight)     {        this.witdh = witdh;         this.beight = beight;    }     //area     public double getares ()     {         return witdh.doublevalue ()  * beight.doublevalue ();     }}//Triangle class triangle extends graph{    private  integer a;    private integer b;    private  Integer c;    triangle (INTEGER A, INTEGER B, INTEGER C)     {        this.a = a;         this.b = b;         This.c = c;   &nbsP;}     //does not define the Getares method in this class, that is, the method that does not overwrite the parent class to find the area}class abstractdemo {     public static void main (String[] args)      {         //the area of the Circle         system.out.println ( New circle () getares ());         //area of the rectangle          system.out.println (New rectangles (5,7). Getares ());         //to find the area of the Triangle         system.out.println (new  Triangle (3,4,5). Getares ()     }}/*/* Appeal Design questions: 1, each figure has an area, so in the graph class to define the method of area but, Different specific graphics to calculate the area of the algorithm is not the same, that is, each subclass must go to overwrite the Getares method, if not overwrite should be syntax error, but there is no error 2, in the graphics class defined the Getares method, the method should not exist method body, Because the different graphics sub-class area algorithm is not the same, the parent class really do not know how to write, so should provide no method body 3, to solve this problem, you have to use an abstract method to solve (no method body, must require subclass coverage) *///-------------------------- Abstract method Debut!!! ----------------------------------/* Use ABSA method tract decorated without a method body, called an abstract method, is a modifier *///a graphic abstract class graph //abstract class (Cannot create an object, even if it does not make sense to create an abstract class object). Abstract classes must have subclasses) {    abstract public double getares ();  //abstract method (no method body, There is no functional code, you must cover this method of the quilt class, there are subclasses to complete the function, such as the subclass does not overwrite the compilation error)         //abstract class can exist common methods, Used to invoke     public void dowork () for subclasses     {         system.out.println ("Hello");     }}//round class circle  Extends graph{    private integer r;    circle (Integer  R)     {        this.r = r;     }    //Area     public double getares ()     {        return 3.14 * r *  r;    }}//Rectangle class&nbsp rectangles extends graph{    private integer witdh;     private integer beight;        rectangles (Integer  witdh, integer beight)     {         this.witdh = witdh;        this.beight =  beight;    }    //Area     public Double  Getares ()     {        return  Witdh.doublevalue ()  * beight.doublevalue ();     }}//Triangle class triangle  extends graph{    private integer a;    private  Integer b;    private integer c;    triangle (Integer  a, integer b, integer C)     {        this.a = a;         this.b = b;         this.c = c;    }    public double getares ()     {        Double p =  (A+B+C)/2.0 ;         return math.sqrt (p* (p-a) * (p-b) * (p-c));     }}class abstractdemo {    public static void main (String[]  args)      {        //area of the Circle          system.out.println (new circle) getares ());         //the area of the rectangle         system.out.println (new  Rectangles (5,7). Getares ());         //to find the area of triangles          System.out.println (New triangle (3,4,5). Getares ());         new  triangle (3,4,5). DoWork ()  //calling a normal method in an abstract class         //cannot create abstract class objects         //graph a = new graph ();     }}


Java abstract design and abstract methods

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.