Object-oriented programming (OOP) is a programming paradigm based on the concept of objects, which can contain data and code. The data is in the form of fields, and the code is in the form of methods.
OOP uses Classes and Objects
Classes - Definitions for the data format and procedures for a given class of object
Objects - Instances of classes
Objects or classes can be based on other objects or classes. The child will inherit the fields and methods of the parent object. For example, an object of the Manager class is a child of the Employee class. It inherits the Employee properties such as name and employeNumber, and it extends the Employee class by adding managementRole and managementLevel.
A child class is a subclass of its parent. A parent is a superclass of its child.
The Manager object is an Employee object, but further extends its functionality with more fields and methods. This is an example of an is-a relationship.
It is possible to prevent inheritance of specific fields if the parent class specifies that a field is private.
Subclasses can override the methods defined by superclasses. For example, the Employee class has a toString method and the Manager class also has a toString method. The Manager class's toString method overrides its superclass's toString method.
Advantage: promotes reusability of code with a is-a relationship
Polymorphism means many forms. Depending on the situation, code can be flexible by being able to change the parameters it requires and the data it returns. Two examples of this are method overloading and constructor overloading.
Method overloading
Method overloading is the ability to create multiple methods of the same name with different implementations. Different parameters could be required and/or different data types could be returned.
For example, the Manager class has a calculateBonus() method that returns a real number representing the amount of money to be paid as bonus. This is calculated using the Manager object's managementLevel. The Manager class also contains a second calculateBonus(additional : real) method that adds an additional percentage increase to the bonus if a parameter is included in the method call.
In Java, the substring method is an overloaded method: substring(3) and substring(3,7).
Constructor overloading
A class can have multiple constructors that differ in the number and/or type of their parameters.
For example the Employee class can have a constructor that requires a name parameter. It could have a second constructor that requires a name and employeeNumber parameter. In some cases an Employee might not have an employeeNumber yet.
Access to an object's data fields can be restricted by access modifiers (private, public, protected). This information hiding of data from the rest of the program protects the data integrity of objects and prevents other parts of the program from causing inconsistencies.
Modifier Class Package Sub World
class
public Y Y Y Y
protected Y Y Y N
default Y Y N N
private Y N N N
TBC
Note: Composition isn't necessarily a characteristic of OOP but it is an alternative to using inheritance. In fact programmer will often use both composition and inheritance.
Objects can contain other objects in their instance variables; this is known as object composition. For example, an object in the Employee class might contain an object in the Address class, in addition to its own instance variables like "name" and "contactNumber". This is a has-a relationship.
Advantage: promotes reusability of code and encapsulation
TBC
What is static
What is constant
Access modifiers
public +
private -
protected #
Static underlined
Constant ALLCAPS