Java Difficulty Analysis (vii)-abstract class

Source: Internet
Author: User
Tags define abstract

abstract class: Abstraction
Abstract: Not specific, can not understand. Abstract class representation manifests itself.
in the continuous extraction process, the common content of the method declaration extraction, but the method is not the same, no extraction, then the method extracted, not specific, need to be specified by the keyword abstract marked, declared as an abstract method.
the class in which the abstract method resides must be marked as an abstract class, meaning that the class needs to be decorated with the abstract keyword.

features of abstract classes:
1: Abstract methods can only be defined in abstract classes, abstract classes and abstract methods must be decorated by the abstract keyword (you can describe classes and methods, and you cannot describe variables).
2: Abstract methods define method declarations only and do not define method implementations.
3: Abstract class can not be created object (instantiation).
4: This subclass can be instantiated only if the subclass inherits the abstract class and overrides all abstract methods in the abstract class. Otherwise, the subclass is an abstract class.

the details of the abstract class:
1: Is there a constructor in an abstract class? There, used to initialize the subclass object.
2: Is it possible to define non-abstract methods in an abstract class?
you can. In fact, there is not much difference between abstract class and general class, it is to describe things, but abstract classes describe things, some functions are not specific. Therefore, both abstract and generic classes need to define properties and behaviors in the definition. However, there is an abstract function more than the general class. And it's a little less than a generic class. The part that creates the object.
3: Abstract keyword and which cannot coexist? Final, private, static
4: Can I not define abstract methods in an abstract class? OK. The abstract method is intended only to keep the class from creating objects.
-----------------------------------------------------------------------------------------------
Template Method Design pattern:
problem solved: Some implementations are indeterminate when the internal part of the function is implemented. At this point, we can expose the uncertain parts and let the subclasses to achieve them.

Abstract classgettime{ Public Final voidGetTime () {//This feature can be final qualified if no replication is requiredLongStart =system.currenttimemillis (); code ();//an indeterminate part of the function, extracted, implemented by an abstract methodLongEnd =System.currenttimemillis (); System.out.println ("Milliseconds is:" + (end-start));} Public Abstract voidCode ();//abstract and uncertain functions, so that sub-class replication implementation}classSubdemoextendsgettime{ Public voidCode () {//sub-class replication function method for(inty=0; y<1000; y++) {System.out.println ("Y");}}

Java Difficulty Analysis (vii)-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.