Abstract class for Java

Source: Internet
Author: User

Abstract class
1. Syntax definition: Use abstract keyword decoration before abstraction class

2. Application Scenario
A, the parent class only knows what kind of method the subclass contains, and does not know how the method is implemented (the constraint subclass must have a method)

B, from a class with the same characteristics, abstract a class, with this abstract class as a subclass template, so as to avoid the arbitrary design of sub-class

3, role: Limit the subclass must have those methods, do not pay attention to the specific details

4. Rules of Use:
A, abstract defines a class that modifies abstractions
B, abstract defines an abstraction method, declares only, does not need to implement
C, classes that contain abstract methods are abstract classes
D, abstract classes can contain ordinary methods, or there can be no abstract method.
E, abstract classes cannot be created directly, you can define reference variables, point to child class objects

Example: Define an abstract class phone (parent class) whose subclasses have cellphone and smartphone. Defining abstract class Methods: Call and Message

Test class:

Question 1: In the creation of inherited phone abstract parent class when the line error, press F2 to find the problem and according to the prompt to modify (delete @override and then reopen it)
--reason: Compiler version issue

Question 2: When creating the smartphone subclass, accidentally ticked the abstract, and made an abstraction class, so in the main function (test Class) in the smartphone instantiation of the failure
--reason: cannot instantiate an abstract class

When defining the Smartphone class, add the abstract modifier and delete it.

Abstract class for Java

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.