Final and abstract keywords

Source: Internet
Author: User

Final features:

1: This keyword is a modifier that can be used to modify classes, methods, and variables.

2: The final class is a final class and cannot be inherited.

3: The final modification method is a final method and cannot be overwritten.

4: The variable modified by final is a constant and can be assigned only once.

 

In fact, the reason is to give some fixed data a name with strong readability.

Can I use it without final modification? This value is a variable that can be changed. With final, the program is more rigorous. A constant name is defined in a regular way. All letters are capitalized. If it is composed of multiple words_Connection.

 

Abstract class: Abstract

Abstract: It is not specific and cannot be understood. Abstract class representation.

In the process of continuous extraction, the methods declared in the common content are extracted, but the methods are different and are not extracted. The extracted methods are not specific and need to be marked by the specified keyword abstract, the Declaration is an abstract method.

The class of the abstract method must be marked as an abstract class, that is, the class must be modified by the abstract keyword.

 

Abstract class features:

1: abstract methods can only be defined in abstract classes. abstract classes and abstract methods must be modified by abstract keywords (classes and methods can be described, but variables cannot be described ).

2: Abstract METHODS only define method declarations, but not method implementations.

3: abstract classes cannot be created (instantiated) objects ).

4: only inherit the abstract class through subclass and overwriteAllAbstract MethodThis subclass can be instantiated. Otherwise, this subclass is still an abstract class.

 

Abstract class details:

1: Is There Any constructor in the abstract class? Yes. It is used to initialize the subclass object.

2: Can non-abstract methods be defined in abstract classes?

Yes. In fact, abstract classes are not much different from general classes. They are all describing things, but some functions are not specific when abstract classes describe things. Therefore, abstract classes and general classes both need to define attributes and behaviors. However, there is an abstract function more than a general class. In addition, there is less part of the object to be created than the general class.

3: Which of the following abstract keywords cannot coexist?Final,Private, static

4: can abstract methods not be defined in abstract classes? Yes. Abstract METHODS only aim to prevent this class from creating objects.

Bytes -----------------------------------------------------------------------------------------------

Template Method Design Mode:

Problem solved: Some implementations are uncertain when some internal functions are implemented. In this case, we can expose uncertain parts and let them be implemented by sub-classes.

Abstract class gettime {

PublicFinalVoid gettime (){// If this function does not need to be rewritten, the final limit can be added.

Long start = system. currenttimemillis ();

Code ();// Uncertain functions are extracted and implemented using abstract methods.

Long end = system. currenttimemillis ();

System. Out. println ("millisecond:" + (end-Start ));

}

PublicAbstractVoid code ();// Abstract uncertain functions, so that sub-classes can be rewritten.

}

Class subdemo extends gettime {

Public void code (){// Method of sub‑class Rewriting

For (INT y = 0; y <1000; y ++ ){

System. Out. println ("Y ");

}

}

}

Final and abstract keywords

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.