What is Object Oriented Programming?
Object oriented programming (OOP) is a programming paradigm that focuses on objects rather than actions. An object is a collection of data (properties) and functions (methods) that relate to a specific concept or entity. For example, a car object can have properties like color, model, speed, and methods like start, stop, accelerate, brake.
OOP has several benefits over other paradigms like procedural or functional programming. Some of these benefits are:
It makes code more organized and modular by grouping related data and behavior into objects.
It makes code more reusable by allowing objects to inherit properties and methods from other objects.
It makes code more flexible by allowing objects to change their behavior at runtime through polymorphism.
It makes code more abstract by hiding the implementation details of objects from other parts of the program.
What are the four principles of OOP?
OOP is based on four core principles that define how objects interact with each other. These principles are encapsulation, inheritance, polymorphism and abstraction.
Encapsulation is the principle that objects should hide their internal state and expose only the necessary interface to other objects. This means that an object should only reveal what it can do (methods) and not how it does it (properties). This way, an object can maintain its integrity and prevent unwanted access or modification of its data.
For example, a bank account object can have properties like balance, interest rate, owner name, and methods like deposit, withdraw, transfer. The object can encapsulate its properties by making them private or protected, so that only the object itself or its subclasses can access them. The object can expose its methods as public, so that other objects can use them to interact with the bank account.
Inheritance is the principle that objects can inherit properties and methods from other objects, forming a hierarchical relationship. This means that an object can reuse the code of another object without having to rewrite it. This way, an object can extend the functionality of another object and avoid code duplication.
For example, a savings account object can inherit from the bank account object, and have additional properties like minimum balance, withdrawal limit, and methods like add interest, check balance. The savings account object can use the properties and methods of the bank account object, as well as define its own properties and methods.
Polymorphism is the principle that objects can have different behavior depending on their type or context. This means that an object can change its behavior at runtime based on the situation or the input. This way, an object can be more dynamic and adaptable to different scenarios.
For example, a shape object can have a method called draw that displays the shape on the screen. However, the draw method can have different implementations depending on the type of the shape object. A circle object can draw a circle, a square object can draw a square, and so on. The draw method can also have different implementations depending on the context. For example, if the shape object is selected, it can draw a border around it.
Abstraction is the principle that objects should provide a simplified and generalized view of their functionality to other objects. This means that an object should only reveal what is relevant and essential to other objects, and hide the unnecessary and complex details. This way, an object can reduce the complexity and increase the readability of the code.
For example, a calculator object can have methods like add, subtract, multiply, divide that perform arithmetic operations on numbers. The calculator object can abstract away the details of how these operations are performed internally, and only expose the methods that other objects can use to perform calculations.
A class is a blueprint for creating objects that share the same properties and methods. A class can have a constructor function that initializes the properties of the objects, and can also have instance methods that are available to all objects of the class. A class can also have static methods that are available to the class itself, not to its instances.
For example, we can create a class called Animal that has a constructor function that takes two arguments: name and sound. The constructor function assigns these arguments to two properties: this.name and this.sound. The class also has an instance method called speak that logs the name and sound of the animal to the console. The class also has a static method called count that keeps track of how many animals have been created.
To create an object with a specific prototype, we can use Object.create() , which takes an argument for the prototype object and returns a new object with that prototype. To add properties or methods to an object's prototype, we can use Object.prototype.propertyName or Object.prototype.methodName . To access an object 71b2f0854b