Object-Oriented Programming (OOP) is a programming paradigm based on the concept of objects, which can contain data, in the form of fields (otherwise known as attributes or properties) and code, in the form of procedures (which are also known as methods).
There are 4 principles of object-oriented programming. They are:
Encapsulation
Abstraction
Inheritance
Polymorphism
Encapsulation
If there is a program that contains logically dissimilar objects that need to communicate with each other, encapsulation is used. It is achieved when each object keeps its state private and within a class. Other objects do not have access to this state directly, but can only call a list of public functions known as methods. The object manages its own state using methods and no other class can access it unless permission is granted. If communication with the object needs to take place, methods may be used but the state cannot be changed.
Abstraction
Abstraction can be considered to be the natural extension of encapsulation. In OOP design, programs are usually very lengthy, where many different objects communicate with each other. So, in order to maintain a large codebase that needs to accommodate changes, abstraction can be used. Applying abstraction means that each object should only allow a higher level mechanism for using it. This mechanism should hide the information on implementation and only show the operations that are necessary for other objects. The mechanism should be easy to use and rarely change.
Inheritance
Objects are often very similar and share common logic, but are not entirely the same. To reuse common logic and extract the unique logic into a separate class, we use inheritance. Inheritance is when a child class is created by deriving from another parent class, which forms a hierarchy. This way, each class only adds what is necessary for it while reusing the common logic with the parent class.
Polymorphism
Assume there is a parent class and a few child classes which inherit from it, and the user wants to use a collection, such as a list, that contains a mix of the classes or have a method implemented from the parent class but would also like for it to be used on the children. This problem can be solved using polymorphism. Polymorphism means ''many shapes'' in Greek. It gives a way to use a class like its parent so there is no confusion in mixing the types and allows the child class to retain its methods as they are. This happens by defining a parent class’ interface to be reused. A number of common methods are outlined, and then, each child class is allowed to implement its own version of the methods. Any time a collection or a method expects an instance of the parent class, the language takes care of evaluating the right implementation of the common method, regardless of which child class is passed.
Uses of OOP
OOP can be used in multiple situations such as:
Client-server systems
Object-oriented databases
The internet
Real-time system design
Simulation and modelling systems
Hypertext and hypermedia
Neural networking
Parallel programming
Office automation systems
Email
Word processing
Web calendars
Desktop publishing
CIM/CAD/CAM systems
Article was written for CyberClubNPSi
Comments