Java Abstract class

Source: Internet
Author: User

/****: Current Problems:    1.  The run method of the animal class is not described correctly.     2.  no mandatory subclass must override the Run method. Image class Application Scenario:     when we describe a class of things, we find that there is a certain kind of behavior,     but this behavior is currently not specific, then we can * * * Take a statement of this behavior, but      does not implement this behavior, which is what we call a "* * *" behavior, we need to use the * * * Image class.     *** the benefits of the Elephant class:  enforces that subclasses must implement the specified method. The details of the like class:    1.  if a function does not have a method body, the function must be decorated with an abstract modifier, which modifies the function as a function of the * *  .     2.  if a class appears with a function like * * *, then the class must also   use the abstract adornment.     3.  if a non-* * * * * * * * * * * * * * * * * * * Inherits the image class, all the image methods must be implemented. Otherwise, this class can only be a * * * Image class     4. *** like a non-* * * method, can also exist the method of the image.     5. ** * Like class can not exist * * * method. The      5. *** image class is not able to create objects.          question: Why can't the Elephant class create an object?          because * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *        Invoke * * * Image SquareThere is no point in the law. The     6. *** image class is a constructor, and its constructor is provided to the subclass to initialize the properties of the parent class when the object is created. *///Animal Class--->*** Elephant class abstract class animal{    string name;     string  color;    public animal (string  name,string  Color) {        this.name = name;     Methods of     this.color = color;    }//Non-* * * *      public void eat () {        system.out.println (name+) Eat food ");     }    //Mobile ...    public abstract  Void run ();  //as long as there is {}, is the implementation of the method}//dog   is a kind of animal  class Dog extends Animal{     public dog (String name,string color) {         super (Name,color);     }     public void run () {         System.out.println (name+ "Four legs run fast ...");     }}//fish   is a kind of class fish extends in animals.  animal{    public fish (String name,string color) {         super (Name,color);    }    public  Void run () {        system.out.println (name+) "Shake your tail and swim!" ");    }}class demo3 {    public static void  Main (String[] args)  {    /*    dog d = new  dog ("Shepherd Dog", "Brown");     d.run ();     //Create a Fish object      fish f  = new fish ("Koi", "golden");     f.run ();     */    animal&nbSp;a = new animal ();     }} 
/* Requirements:  describes a graph, circle,   rectangle of three classes. No matter which graph will have the calculation area and perimeter behavior, but each kind of graph calculation way is inconsistent. Naming conventions for constants: Capitalize all letters, separating words from words with underscores. Abstract cannot co-decorate a method with the following keywords, because someone else is rewriting a method that 1. abstract cannot co-decorate with private. 2. abstract  cannot work with static to modify a method. 3. abstract  cannot work with final to modify a method. *///abstract *** image//graphic class abstract class myshape{     string name;     public myshape (String name) {         this.name = name;    }    public  abstract  Void getarea ();     public abstract void getlength ();} Circular   is a kind of class circle extends myshape{    double r;  that belongs to graphic class    public static final double PI = 3.14;     Public circle (string name,double r) {    super (name);         this.r =r;    }    public  void  The area of Getarea () {        system.out.println (name+) is: "+PI*r*r);     }    public  void getlength () {         system.out.println (name+ "perimeter is:" +2*pi*r);     }}//rectangle   belongs to the   in the drawing A class rect extends myshape{    int width;    int  height;    public rect (string name,int width, int height) {         super (name);         this.width = width;        this.height = height;     }    public  void getarea () {        &nbSp The area of System.out.println (name+ "is:" +width*height);    }    public  The perimeter of  void getlength () {        system.out.println (name+) is: "+2* (Width+height));     }}class demo4{    public static void main ( String[] args)      {        // System.out.println ("hello world!");         circle c = new circle ("Round", 4.0);         c.getarea ();         C.getlength ();                         //Rectangle         Rect  R = new rect ("Rectangle", 3,4);         r.geTarea ();         r.getlength ();     }} 


This article from "Small Fish Blog" blog, declined reprint!

Java Abstract class

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.