Object Oriented Programming in Java
An object-oriented program replaces the concept of a linear sequence of steps with steps that create instances of classes, connect them together, and set them to communicate with each other
For example, in an HR/Payroll application, we might have object classes that represent employees, dependent, checks, departments, financial accounts, etc The act of creating a check for an employee could trigger a number of actions:
- update the employee's accumulated pay, etc., fields - done in an employee object
- update accumulated department totals - performed in a department object
- debit a financial account - done in an account object
- cause a physical check to be printed - a check object would be created encapsulating the information, and sent to a printing module (in some sort of object dedicated to similar generic operations)
- upon successful printing of the check, notifications would be sent to the above objects to finalize their operations
Encapsulation is the concept that the data and operations that work on or with that data are bundled together into one unit - the object Objects are said to have state and behaviour
- the state is the current set of values for all the data values
- terms commonly used to describe the state values are:
- data members
- the behaviour is the set of operations that the object can perform (the
functional code for the object)
- commonly called methods, but also sometimes called functions
What is Object Oriented Programming (OOP)?
- A software design method that models the characteristics of real or abstract objects using software classes and objects.
Characteristics of objects:
- State (what the objects have)
- Behaviour (what the objects do)
- Identity (what makes them unique)
- Definition: an object is a software bundle of related fields (variables) and methods.
- In OOP, a program is a collection of objects that act on one another (vs. procedures).
For example, a car is an object. Its state includes current:
- Fuel level
- Engine temperature
Its behaviours include:
- Change Gear
- Go faster/slower
- Go in reverse
Its identity is:
- License Plate
Why Object Oriented Programming (OOP)?
- ModularitySeparating entities into separate logical units makes them easier to code, understand, analyse, test, and maintain.
- Data hiding (encapsulation)The implementation of an object?s private data and actions can change without affecting other objects that depend on it.
Code reuse through:
- Composition Objects can contain other objects
- Inheritance Objects can inherit state and behaviour of other objects
Even though OOP takes some getting used to, its main benefit is to make it easier to solve real-world problems by modelling natural objects in software objects. The OO thought process is more intuitive than procedural, especially for tackling complex problems. Although a lot of great software is implemented in procedural languages like C, OO languages typically scale better for taking on medium to large software projects.