Concept
Object-Oriented Programming (OOP)
What is OOP?
OOP uses objects to design software. An object will contain attributes (data, properties, variables) and behaviors (methods, functions). Basically, we will define classes that will act as blueprints for creating objects.
Differences between Class and Object
Class | Object |
---|---|
A blueprint for creating objects. It defines the attributes and behaviours that the created objects will have. | An instance of a class. It contains data and methods defined by the class. |
Four Principles/Pillars of OOP
Pillar | Description |
---|---|
Encapsulation | Bundle data and methods into a single class. It restricts direct access to some of the object’s components, which can prevent the accidental modification of data. Mainly hiding internal state and implementation details. |
Inheritance | A mechanism to create a new class (subclasses) that inherits attributes and methods from an existing class (parent classes). It allows for code reuse and establishes a relationship between classes. |
Polymorphism | The ability to use a single interface to represent different underlying data types. It allows methods to do different things based on the object it is acting upon, even though they share the same name. Also, it allows methods to have the same name but behave differently based on the object type by using method overriding or method overloading. |
Abstraction | Hiding complex internal implementation details and showing only the essential features of the object. It allows the developer to focus on “what to do” (focus on what an object does) rather than “how to do it” (how it achieves its functionality). |
Method overriding vs Method overloading
Concept | Description |
---|---|
Method Overriding | A subclass provides a specific implementation of a method that is already defined in its superclass. It allows a subclass to modify or extend the behaviour of the inherited method. |
Method Overloading | The ability to define multiple methods with the same name but different parameter lists (different types or number of parameters) within the same class. It allows a method to perform different tasks based on the arguments passed to it. |
Last updated on