Java abstract class in the mind of small white (abstract class)

Source: Internet
Author: User

In Java development, we sometimes define a parent class that only describes the method, but does not write out the implementation of the method in the parent class, which is called an abstract method. Naturally, classes that contain abstract methods are called abstract classes. Modified with the keyword abstract.

For example I write a parent class person:

1 Abstract class person{2      Public Abstract void study (); 3      Public void eat () {4         System.out.println ("Skill to eat"); 5     }6 }

As you can see, this person class is decorated with the abstract modifier, and there is no implementation of the method in the abstract method study (), where {} can be omitted. Now, the person class is a standard abstract class.

Where an abstract class has the following syntax rules to follow:

1. If there is an abstract method in a class, the class must be an abstract class.

2, abstract class can have no abstract method.

3, abstract class can not exist instance object, in other words, abstract class is to be inherited and exist.

4. If a subclass inherits an abstract class, all abstract methods defined in the abstract class must be implemented.

5, abstract classes can inherit abstract classes, you can not override the parent class method.

Next, write two subclasses of the inheriting person class:

1 classStudentextendsperson{2      Public voidStudy () {3SYSTEM.OUT.PRINTLN ("Student's learning content");4     }5 }6 7 classWorkerextendsperson{8      Public voidStudy () {9System.out.println ("The learning content of the worker");Ten     } One}

It can be seen that two subclasses have implemented the abstract Method study (), the implementation will be error.

If the abstract class has only the above benefits, that feeling and ordinary classes there is no big difference, we can define two ordinary class student and workers, to implement the study () method respectively, then the meaning of the abstract class?

In fact, at the time of invocation, we make this call:

1 New Worker (); 2 New Student ();

Called when the type of the parent class points to the syntax of an instance of a subclass, this syntax is called polymorphism. The benefit is that once the requirements change, the changes are flexible and easy to change, without the need to modify too much code.

Finally, when writing code, be aware that abstract cannot be used with the following modifiers:

1, finaly

2. Private

3. Static

  

The Java abstract class in the mind of small white (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.