Programming OOP with Java

OOP stands for Object-Oriented Programming. Meaning object-oriented programming.

There are 4 properties in OOP:

  1. Abstraction
  2. Inheritance
  3. Polymorphism
  4. Packing properties
  • Abstraction means creating abstract methods in interface or abstract class. I do not need to pay any attention to what classes include properties, how specific methods are implemented. We only need to report the names of abstract methods to inherited subclasses that implement those methods specifically. The purpose of abstraction is to summarize the general behavior of the entity.
  • Packing is capable of packing properties, methods, and data of an object. It does not allow direct access to the data but it must go through the methods provided by the object to access its own data. This is a very important data hiding ability in object-oriented programming.
  • Inheritance is the subclass can re-use the properties and methods of the parent class or implement the abstract methods in the parent class (abstract method of abstract class with interface). It is my step to extends class with implement interface. The purpose of inheritance to extend, limit code duplication
  • Polymorphism means that the step in your child class inherits the parent class, you can overried the method of the parent class when the method of the child class has the same name and the same parameter (the same data type) with the method father class. Or I can also overload the existing method in the same class with the parameter passed to that method. The purpose of polymorphism is to deploy specifically in subclasses.

To understand more about OOP, we do the following problem:

A university managing staff information (including lecturers and administrative staff) in the school. With lecturers need to manage information: name, faculty, level (bachelor, master, doctor), allowance, number of lessons taught in the month, salary coefficient. For administrative staff to manage: Full name, department, number of working days, salary, allowance, position (head, deputy, staff). Staff allowance is calculated according to the table: Bachelor of 300. Master of 500. Doctor of 1000. Head of Department 2000. Deputy Head of Division 1000. Employee 500. The salary of lecturers is calculated as follows: Salary coefficient x 730 + allowance + number of lessons x 45. Staff salaries are calculated as follows: Salary x 730 + allowances + number of work days x 30;

Write a school information management program that includes the following functions:

  1. Data entry for school officials.
  2. Search for staff by name and department and print out a detailed information screen for this employee.
  3. Export a list of all teachers in the school and arrange alphabetically, if the names are the same, sort by the salary received from high to low. (Note, the list of lecturers and staff is printed with the salary information column) Requirements: Applying knowledge of object-oriented programming to build the program. Proficient in abstraction, packaging, polymorphism, inheritance and applying abstract class and interface in the lesson. The main function only makes calls to functions without writing any other business.

This is my code to handle the problem on: https://github.com/nguyenvanthieub/OOP

Here, I created interface ICanBo with abstract method tinhLuong ()

Let 2 classes subclass JiangVien and NhanVien to be redundant (implements ICanBo) (expressing inheritance).

Because both GiangVien and NhanVien have the same attributes: hoTen, heSoLuong, phuCap, luong. I have created abstract class CanBo, let GiangVien class and NhanVien inherit to avoid being duplicated code for both GiangVien and NhanVien classes to write (expressing abstraction)

From there, 2 subclasses GiangVien and NhanVien process themselves (override) method tinhLuong () to suit the problem. (represents polymorphism) The example below is the NhanVien class

To ensure packing, I have left the properties to private, other classes want to access the properties of NhanVien, then getter, setter I created.

ITZone via Viblo.asia

Share the news now