OOP principles Nabiyev Abdulaziz Contents - What is an Object?
- What are Classes?
- What is Inheritance?
- What is Polymorphism?
- What is Abstraction?
- What is Encapsulation?
What is an Object? - Let us understand this with an example. Consider your cell phone as an object. There can be different properties for your cell phone like its model, software version, and memory in it. This object can also have functions like switch on the camera, turn off Bluetooth, restart, etc. In simple words, each object contains data and instructions to act on that data.
What are Classes? - A class is like a template from which new objects are created.
- Collection of objects is called class. It is a logical entity.
- A class can also be defined as a blueprint from which you can create an individual object. Class doesn't consume any space.
What is Inheritance? - It is the ability to acquire the properties of existing classes and create new ones. Inheritance allows you to reuse code without having to rewrite it in a program. One of the best features of Inheritance is the ability to shorten the code in a program. You can use this principle to inherit codes from another class and reuse it in a new class.
What is Polymorphism? - If one task is performed in different ways, it is known as polymorphism. For example: to convince the customer differently, to draw something, for example, shape, triangle, rectangle, etc.
- In Java, we use method overloading and method overriding to achieve polymorphism.
- Another example can be to speak something; for example, a cat speaks meow, dog barks woof, etc.
What is abstraction? - Applying abstraction means that each object should only expose a high-level mechanism for using it.
- This mechanism should hide internal implementation details. It should only reveal operations relevant for the other objects.
What is encapsulation? - Encapsulation is achieved when each object keeps its state private, inside a class. Other objects don’t have direct access to this state. Instead, they can only call a list of public functions — called methods.
- So, the object manages its own state via methods — and no other class can touch it unless explicitly allowed. If you want to communicate with the object, you should use the methods provided. But (by default), you can’t change the state.
Reference - https://www.partech.nl/en/publications/2020/10/basic-principles-of-object-oriented-programming#
- https://medium.com/@cancerian0684/what-are-four-basic-principles-of-object-oriented-programming-645af8b43727
- https://www.freecodecamp.org/news/object-oriented-programming-concepts-21bb035f7260/
- https://www.javatpoint.com/java-oops-concepts
Thank you
Do'stlaringiz bilan baham: |