First, the basic concept
Abstract things, if there is no specific information can describe this thing, this thing can be called abstract things.
Abstract classes are constantly drawn upward. Extract the method declaration without determining the specific method content. The specific method content is accomplished by different subclasses.
(i) Characteristics of abstract classes:
1. Abstract methods have no method body and must be modified by abstract keyword abstraction.
1, abstract methods must be present in the abstract class, it is necessary to have abstract keyword abstraction to decorate.
2. Abstract classes cannot be instantiated. You cannot create an object with the New keyword.
3. Subclasses must overwrite all abstract methods in the abstract class before they can be instantiated. Otherwise, the subclass is an abstract class.
(b) There can be constructors in an abstract class:
Because abstract classes cannot create objects, constructors in all abstract classes are not instantiated for objects of abstract classes. However, abstract classes can have subclasses, and the constructors of abstract classes can initialize objects of subclasses. Constructors can be found in all abstract classes.
(iii) The distinction between abstract and general classes:
An abstract class can describe the properties and behavior of a thing. The information describing a thing is not specific, you can define an abstract method, but you cannot instantiate it.
A generic class can describe the properties and behavior of a thing. The information of a thing is specific, it is not possible to define an abstract method, but it can be instantiated.
(iv) An abstract class must be a parent class:
You can instantiate an abstract method only if it needs to be overridden by the child class. To use these methods.
(v) Abstract methods may not be defined in a class:
The function of the abstract class at this point is simply to make the class unable to create the object.
(vi) Abstract keyword abstractions and those keywords cannot coexist:
Keyword final, keyword private, keyword static.
Second, the example code
Requirements: There are programmers and project managers in the company. Programmers have names, work numbers, and salaries. and work for the company. The project manager has a bonus in addition to his name, work number and salary. Also work for the company. Data modeling of the given requirements.
Analysis:
Programmer: Attributes: Name, job number, salary
Behavior: Work
Project Manager: attribute: Name, work number, salary, bonus
Behavior: Work
There is no relationship between the two, but there is something in common that can be pulled up as an employee.
Employee: attribute: Name, work number, salary
Behavior: Work
1 //Abstract class Employee2 Abstract classEmployee3 {4 PrivateString name;5 Private intID;6 Private DoublePay ;7 8 Public voidsetName (String name)9 {Ten This. Name =name; One } A Public voidSetId (intID) - { - This. ID =ID; the } - Public voidSetPay (DoublePay ) - { - This. Pay =Pay ; + } - + PublicString getName () A { at returnname; - } - Public intgetId () - { - returnID; - } in Public DoubleGetpay () - { to returnPay ; + } - theEmployee (String name,intIdDoublePay ) * { $ This. Name =name;Panax Notoginseng This. ID =ID; - This. Pay =Pay ; the } + A //Abstract Method work the Public Abstract voidWork (); + } - $ //Description Programmer Inherits abstract class Employee $ classProgrammerextendsEmployee - { -Programmer (String name,intIdDoublePay ) the { - Super(Name,id,pay); Wuyi } the Public voidWork () - { WuSystem.out.println ("Name:" + This. GetName () + "ID:" + This. GetId () + "Pay:" + This. Getpay ()); -System.out.println ("Programmer work ..."); About } $ - } - - A //describe project manager inheritance abstract class Employee + classManagerextendsEmployee the - { $ Private Doublebonus; the theManager (String name,intIdDoublePayDoublebonus) the { the Super(Name,id,pay); - This. Bonus =bonus; in } the Public voidWork () the { AboutSystem.out.println ("Name:" + This. GetName () + "ID:" + This. GetId () + "Pay:" + This. Getpay () + "Bonus:" +bonus); theSystem.out.println ("Manager work ..."); the } the } + - classAbstractdemo the {Bayi Public Static voidMain (string[] args) the { theProgrammer A =NewProgrammer ("Xiaoming", 001,5000.00); - a.work (); -Manager m =NewManager ("Xiaohong", 010,8000.00,600.00); the m.work (); the } the}
Third, code execution
Java object-oriented abstract class abstraction primer instance