Java know how much (38) the concept and use of abstract classes

Source: Internet
Author: User
Tags ming

In a top-down inheritance hierarchy, classes on the upper level are more generic and may even be more abstract. From a certain point of view, the ancestor class is more general, it contains only some of the most basic members, people only use it as a base class to derive other classes, and not to create objects. Even if you can just give the definition of the method and not implement it, the subclass is specifically implemented according to the specific needs.

This method, which only gives the method definition but not the concrete implementation, is called the abstract method, the abstract method has no method body, and there is no "{}" in the expression of the code. A class that contains one or more abstract methods must also be declared as an abstract class.

An abstract modifier is used to represent an abstraction method and an abstract class.

Abstract classes can contain specific variables and specific methods in addition to abstract methods. A class can be declared as an abstract class, even if it does not contain an abstract method, to prevent it from being instantiated.

Abstract classes cannot be instantiated, and abstract methods must be implemented in subclasses. Take a look at the following code:
1 Import Staticjava.lang.system.*;2  Public Final classdemo{3      Public Static voidMain (string[] args) {4Teacher T =NewTeacher ();5T.setname ("Wang Ming");6 t.work ();7        8Driver d =NewDriver ();9D.setname ("Xiao Chen"));Ten d.work (); One     } A } - //define an abstract class - Abstract classpeople{ the     PrivateString name;//instance Variable -     -     //common Setter and Getter methods -      Public voidsetName (String name) { +          This. Name =name; -     } +      PublicString GetName () { A         return  This. Name; at     } -     -     //Abstract Methods -      Public Abstract voidWork (); - } - classTeacherextendspeople{ in     //The method must be implemented -      Public voidWork () { toOut.println ("My name is called" + This. GetName () + ", I'm lecturing, please don't wander around ..."); +     } - } the classDriverextendspeople{ *     //The method must be implemented $      Public voidWork () {Panax NotoginsengOut.println ("My name is called" + This. GetName () + ", I'm driving, I can't answer the phone ..."); -     } the}
Operation Result:
My name is Wang Ming, I am lecturing, please do not wander around ...
My name is Xiao Chen, I'm driving, I can't answer the phone ...

A few notes about abstract classes:
    • Abstract classes cannot be used directly, you must use subclasses to implement abstract classes, and then use instances of their subclasses. However, you can create a variable whose type is an abstract class that points to an instance of a specific subclass, that is, you can use an abstract class to act as a formal parameter, and actually implement the class as an argument, that is, a polymorphic application.
    • There can be no abstract constructor or abstract static method.


A class becomes an abstract class in the following cases:
    1. When one or more methods of a class are abstract methods;
    2. When a class is a subclass of an abstract class and cannot provide any implementation details or method bodies for any abstract method;
    3. When a class implements an interface and cannot provide implementation details or method bodies for any abstract method; Note:
      • It is said that in these cases a class will become an abstract class, without saying that abstract classes are bound to have these conditions.
      • A typical error: An abstract class must contain an abstract method. But conversely, the "class containing abstract methods must be abstract" is correct.
      • In fact, an abstract class can be a completely normal implementation of a class
Series Articles:

Java know how much (top)

Java know how much (38) the concept and use of abstract classes

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.