Dark Horse programmer _ object-oriented abstract classes and interfaces

Source: Internet
Author: User

Abstract class )????

When writing a class, some methods are often defined for the class. The use of these methods is used to describe the behavior of the class, so these methods have specific methods. However, in some cases, a parent class only knows how the child class should contain, but cannot precisely know how these child classes implement these methods, such as defining a gettime class, this class provides a gettime Method for Calculating the running time of a program, but you cannot know the specific method body to run. In this case, you need to use an abstract method to implement it.

Abstract class concept

If multiple classes have the same function but different feature subjects, you can extract the feature from the upper menu. Only the function definition is extracted, but the function subject is not extracted. Abstract when things are uncertain.

Features:

1. abstract methods can only be defined in abstract classes.

2. Abstract methods and abstract classes must be abstract and modified

3. An abstract class cannot use new to create an object because it does not make sense to create an object.

4. To use abstract methods in abstract classes, you must create a subclass object to call them after the subclass rewrites the abstract methods.

5. If the subclass only covers some abstract methods, the subclass is still an abstract class. If you do not want the subclass to be a required abstract class, rewrite all abstract methods in the parent class abstract class.

When to use Abstraction:

Abstract classes are not much different from general classes,When something has an uncertain function, it must be clearly pointed out, but the subject cannot be defined and expressed through abstract methods..

Abstract classAbstract classes have more abstract methods than general classes. abstract classes cannot be instantiated. abstract classes can also define non-abstract methods to prevent this class from creating objects..

Abstract class member

Abstract classes can containCommon methods, member variables,Constructor.

Abstract format features

????Abstract is used to modify the method to be abstracted. At the same time, the method does not have a method body, ending.

Application:

The following program illustrates how to use an abstract definition to get a certain period of running time.

????AbstractClassGettime

???? {

????????PublicFinalVoidGettime ()

???????? {

????????????LongStart = system.Currenttimemillis();

???????????? Runcode (); // call the specified Method

????????????LongEnd = system.Currenttimemillis();

???????????? System.Out. Println (end-Start); // defines the abstract method for obtaining the program running time. The subclass must overwrite this method.

????????}

????????PublicAbstractVoidRuncode ();

????}

????ClassSubtimeExtendsGettime {

????????PublicVoidRuncode () {// subclass override abstract method. This method is the method that needs to calculate the running time.

????????????For(IntX = 0; x <1000; X ++ ){

???????????????? System.Out. Println (X );

????????????}

????????}

????}

Note:

    1. Cannot be used together with final: abstraction is used for inheritance, so it cannot be used together with final.
    2. Cannot be used together with private: the private abstract method in the abstract class cannot be rewritten without the knowledge of the quilt class. Abstract methods need to be rewritten.
    3. Cannot be used with static: If static can modify the abstract method, the object is saved and the class name can be called directly. However, the abstract method is meaningless.
    4. Abstract classes can describe how to describe things, but some content is "incomprehensible". Therefore, they can include member variables and methods and constructor methods.
    5. If an abstract method exists in a class, the class must be modified with abstract (this class is an abstract class)

Interface)

?

?

?

?

?

?

?

?

?

?

?

?

?

?

?

?

?

?

?

?

?

?

?

?

Dark Horse programmer _ object-oriented abstract classes and interfaces

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.