I. Basic concepts of abstract classes
The same functionality occurs in multiple classes, but the functional body is different. You can then pull up. At this point, only the function definition is extracted. Without extracting the functional body
Classes declared with the abstract keyword are abstract classes
Second, the characteristics of abstract class
1. When the method is not implemented, the method is abstract and needs to be modified by abstract
2, the abstract method must be in the abstract class. (That is, the class is abstract as long as the abstract method is in the class)
3. Abstract methods and abstract classes must be modified by the abstract keyword
4. Abstract classes cannot create objects with new because it is meaningless to invoke an abstract method.
5, abstract class in the abstract method to be used, the child class must be replicated all its abstract methods, the creation of a subclass object call
6, if the subclass only partially covers the abstract method, then the subclass is an abstract class
Iii. some problems in abstract class
1. Are there constructors in abstract classes?
There, which is used to initialize the Child class object
2. Can abstract classes not define abstract methods?
Yes, but it is rare to keep the class from creating objects, usually the methods in the class have a method body, but no content
3, abstract class key can not and where the key word coexistence?
Private: Because if the abstract method is private, the method cannot be overwritten by the quilt class
Final: Because the final modified method cannot be replicated
Static: Because if the abstract method is statically decorated, it can be called directly with the class name, but it has no meaning
4, the difference between abstract class and general class
The general class of abstract analogy is not much different
How to describe things as to how to describe things, except that there are some things that are not understood
These uncertain parts, and the function of the thing, need to be explicit, but cannot define the subject
Represented by an abstract method.
Abstract analogy the general class has an abstract function, that is, you can define abstract methods in a class
Abstract classes cannot be instantiated
5, abstract class must be a parent class
Yes. Subclasses can be instantiated because they need to overwrite their abstract methods
1 Public classAbstractdemo {2 Public Static voidMain (string[] args) {3 4 }5 }6 7 Abstract classstudent{8 Abstract voidStudy ();//abstract methods must be stored in abstract classes, so classes are also abstracted9 Ten } One classBasestudentextendsstudent{ A voidStudy () { -SYSTEM.OUT.PRINTLN ("Base study"); - } the } - classAdvstudentextendsstudent{ - voidStudy () { -SYSTEM.OUT.PRINTLN ("ADV Study")); + } -}
Iv. Application of abstract class
If we need to model employees when we develop a department, employees have 3 attributes, name, work number, and salary.
The manager is also an employee, in addition to the attributes of the employee, there is also a bonus attribute, please use the idea of inheritance to design the employee class and manager class, require the class to provide the necessary methods for property access
Employee class: Name ID pay
Manager class: Inherit the staff, and have their own unique bonus.
1 Public classAbstracttest {2 Public Static voidMain (string[] args) {3 4 }5 }6 Abstract classemployee{7 PrivateString name;8 PrivateString ID;9 Private DoublePay ;Ten OneEmployee (String name,string ID,DoublePay ) { A This. Name =name; - This. ID =ID; - This. Pay =Pay ; the } - Public Abstract voidWork (); - } - classProextendsemployee{ +Pro (String name,string ID,DoublePay ) { - Super(Name,id,pay); + } A Public voidWork () { atSystem.out.println ("Pro work"); - } - } - classManagerextendsemployee{ - Private intbonus; -Manager (String name,string ID,DoublePayintbonus) { in Super(Name,id,pay); - This. Bonus =bonus; to } + Public voidWork () { -System.out.println ("Manager work"); the } *}
Object-oriented _ abstract class