"Java Knowledge point Summary" final keyword and abstract class

Source: Internet
Author: User
Tags class manager define abstract

Final keyword: Concept:

Learning to inherit we find that inheritance has a disadvantage, that is, it will break the encapsulation, if you do not allow other classes to inherit the class, there will be no rewrite, → Can be implemented by a few keywords in Java, final (final).

The final keyword is a modifier that can be used to decorate classes, methods, variables (member variables, local variables, static variables).

When do you use it?

When a data used in the program is fixed, in order to increase the reading, you can give the data a name, this is the variable, in order to ensure that the value of the variable is not modified, plus final modification, this is a very strong reading constants. Writing specification: The final modified constant name all the letters are uppercase, if it consists of multiple words, between the words through _.

Features:

1,final Modified class is a final class, not hungry to be able to derive subclasses again (if a part of the class can be overridden, some cannot, as long as the specified method is final);

2,final Modification method is the most red method, can not be rewritten;

A 3,final modified variable is a constant that can only be assigned once.


Abstract class: Produce:

When describing things, there is not enough information to describe a thing, when the thing is abstract, its function is not specific, these non-specific functions need to be identified in the class, using the keyword in Java abstract (abstract).

Classes that define abstract functions must also be decorated with the abstract keyword, and the class that is decorated by the abstract keyword is an abstract class.

Features:

1, abstract classes and abstract methods need to be modified, abstract methods must be defined in the abstract class;

2, abstract class cannot create an instance, Reason: Calling abstract method is meaningless;

3, only if all the abstract methods in the abstract class are overwritten, its subclasses can be instantiated, or the subclass is an abstract class.

"Detail problem"

1, the abstract class must be a parent class?
Yes, because of the constant extraction.


2, does the abstract class have constructors?
Yes, although you cannot initialize your own objects, you can initialize your own subclass objects.
abstract class and general class similarities and differences:
same:
1, which are all used to describe things.
2, in which you can define properties and behaviors.

different:
1, the general class can describe things concretely.
abstract class describes the information of a thing is not specific
2, you can define more than one member in an abstract class: an abstract function.
3, a generic class can create an object, and an abstract class cannot create an object.

3, whether abstract methods can be undefined in an abstract class.
is possible, what is the meaning of the existence of this abstract class? Just don't let the class create objects.

4, what keywords can abstract keywords not coexist with?
1,final:
2,private:
3,static:


Practice:

Requirements: The programmer in the company has name, work number, salary, job content. Project Manager In addition to name, job number, salary, bonus, work content
Find the objects involved in the problem area.

Programmers
Properties: Name, work number, salary
Behavior: Work

Project Manager
Properties: Name, work number, salary, bonus
Behavior: Work

Do these objects have anything to do with it? Found the similarities between them, there should be a relationship.
They can be drawn up to the commonality type: employees.
Employees:
Properties: Name, work number, salary
Behavior: Work
Find that the employee's work content is not specific. should be abstract and embodied by specific subclasses.

Abstract class Employee{private string Name;private string id;private double pay;/** constructs an employee object with three properties initialized. */public Employee (String name,string id,double pay) {this.name = Name;this.id = Id;this.pay = pay;} /** work behavior. */public abstract void work ();} Specific subclass: Programmer. Class Programmer extends Employee{public Programmer (String name,string id,double pay) {super (name,id,pay);} public void work () {System.out.println ("code ....");}} Specific subclass: Manager. Class Manager extends employee{//unique properties. Private double Bonus;public Manager (String name,string id,double pay,double bonus) {super (name,id,pay); This.bonus = Bonus;} public void work () {System.out.println ("Manage");}} Class Abstracttest {public static void main (string[] args) {System.out.println ("Hello world!");}}

———— Mr. BI DAY09 video

"Java Knowledge point Summary" final keyword and 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.