Getting Started with Java Basics-parent class referencing subclass object (2)-abstract class

Source: Internet
Author: User

Abstract classes: Classes with abstract methods that need to be decorated with the abstractions keyword

Relationship Diagram

The code below

Package com.ray.testobject;/** * Abstract class *  * @author Ray * @since 2015-05-02 * @version 1.0 *  */public abstract class person {public abstract void getdescription ();}


Package com.ray.testobject;/** * Implementation class *  * @author Ray * @since 2015-05-02 * @version 1.0 * */public class Man extends P Erson {@Overridepublic void GetDescription () {System.out.println ("I Am a Man");}}


Package com.ray.testobject;/** * continues to abstract a class *  * @author Ray * @since 2015-05-02 * @version 1.0 * */public abstract class Woman extends person {public abstract void getdescription ();}


Package com.ray.testobject;/** * Implementation class *  * @author Ray * @since 2015-05-02 * @version 1.0 * */public class Beatifulwoman Extends Woman {@Overridepublic void GetDescription () {System.out.println ("I am a beatiful Woman");}}


Package com.ray.testobject;/** * Testing class *  * @author Ray * @since 2015-05-02 * @version 1.0 */public Class Test {public St atic void Main (string[] args) {person mans = new Man (); Person Beatifulwoman = new Beatifulwoman (); person[] persons = new PERSON[2];p ersons[0] = man;persons[1] = beatifulwoman;for (int i = 0; i < persons.length; i++) { Persons[i].getdescription ();}}}


Output:

I am a man
I am a beatiful woman

In fact, here is the main explanation is the test inside for the loop inside, each person's class, the implementation of the sub-class

The subclass then implements the abstract method of the parent class, requiring only the parent class to refer to the subclass object, which can be called by invoking the same method and then outputting different things

In particular, it is necessary to note that abstract classes are not instantiated.

Getting Started with Java Basics-parent class referencing subclass object (2)-abstract class

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.