Java Learning (14), Object-oriented programming (vi) abstract class

Source: Internet
Author: User

Abstract Definition : Abstraction is the common content of multiple things extracted, the essence is to take our attention to the content extracted. (For example: BMW, Mercedes-Benz are cars, cars are abstract concept);

abstract class : In Java You can define a method without a method body, which is specifically implemented by its subclasses. The method that has no method body is called abstract method, and the class containing abstract method is called abstract class;

Abstract method Features :

① only method headers without method body are called abstract methods:

② abstract method is used to modify;

③ abstract method represents an indeterminate operation or behavior;

④ abstract methods cannot be called;

features of abstract classes :

The class that contains abstract methods in ① definition is called abstract class;

② abstract classes are used to modify;

③ abstract class represents an abstract object type;

④ Abstract classes cannot be instantiated;

⑤ Abstract classes can have specific methods, there can be no abstract method;

A is an abstract class

Public abstract class a{

public void MethodA () {

}

Public abstract void MethodB ();

}

Example-analysis design based on the given content

① demand: All employees in the company have work number, name, salary, job;

② Teaching Director In addition to the characteristics of the staff, there are management allowances;

③ data modeling for the given requirements;

1  Public classemployeedemo{2      Public Static voidMain (String []atgs) {3Javateacher teacher=NewJavateacher (1, "Zhang San", 2000);4 teacher.work ();5Leader leader=NewLeader (2, "John Doe", 3000,10000);6 leader.work ();7         8         //Employee Emp=new Employee (3, "Harry", 4000);//Abstract classes cannot be instantiated9         Ten         //abstract classes are also parent classes, and reference variables can refer to objects of subclasses OneEmployee teacher2=NewJavateacher (4, "Zhao Liu", 2500); A teacher2.work ();  -     } - } the  - //Abstract class - Abstract classemployee{ -      +     Private intNumber ; -     PrivateString name; +     Private Doublesalary; A      at      PublicEmployee (intNumber,string name,Doublesalary) { -          This. Number=number;//Work No. -          This. name=name;//name -          This. salary=salary;//Salary -     } -      in      Public voidAA () { -         //methods that can be implemented in an abstract class to     } +      -     //declaration of an abstract method the      Public Abstract voidWork (); * } $ Panax Notoginseng //once a class inherits an abstract class, the class either implements the abstract method in the abstract class - //or go on with the abstraction. the classJavateacherextendsemployee{ +      PublicJavateacher (intNumber,string name,Doublesalary) { A         Super(number,name,salary); the     } +      -     //the implementation of the abstract method $      Public voidWork () { $SYSTEM.OUT.PRINTLN ("Upper Java Lesson"); -     } - } the  - classLeaderextendsemployee{Wuyi     Private Doubleallowance;//Allowances the      PublicLeader (intNumber,string name,DoubleSalaryDoubleallowance) { -         Super(number,name,salary); Wu          This. allowance=allowance; -     } About      $     //the implementation of the abstract method -      Public voidWork () { -SYSTEM.OUT.PRINTLN ("Training New Employees"); -SYSTEM.OUT.PRINTLN ("Design Teaching Course"); A     } +}

Java Learning (14), Object-oriented programming (vi) 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.