Classes and Encupsulation Flashcards
3 Fundamental Principles of Object-Oriented Programming
- Encapsulation.
- Inheritance.
- Polymorphism.
Encapsulation
Concept of hiding values or state of data within a class, limiting the points of access.
Inheritance
The practice of creating a hierarchy for classes in which descendants obtain the attributes and behaviors from other classes.
Polymorphism
The ability for a code to take on different forms. In other words, it’s the ability to treat classes generically and get specific results.
Benefits of Object-Oriented Programming
- A natural way of expressing real-world objects in code.
- Modular and reliable, allowing changes to be made in one part of the code without affecting another.
- Discrete units of reusable code.
- Units of code can communicate with each other by sending and receiving messages and processing data.
Encapsulation characteristics
- The packaging of data and functions into a single component.
- Hiding the implementation details of a class to prevent other parties from setting the data to an invalid or inconsistent state and also to reduce coupling.
- Enables classes to be highly cohesive, meaning to have a clearly defined purpose.
- Implemented by using access modifiers that let the compiler know which data members and methods can be accessed and modified by others.
Coupling
Coupling refers to the degree of direct knowledge each component of a system or application has of elements that it needs to use.
Loose Coupling
An approach to connect components that depend on each other with the least knowledge possible.
Access modifiers
Visibility of classes, member methods, and variables
Defines who can use a class, method, or variable.
- public
- private
Variables
Should always be private. If the others need access to them, then they should be given access via public getters and setters.
Methods
Should be public only if they are meant for use by the users of our class.
Classes
Is a blueprint or model that defines state with fields (variables) and behavior with methods.
Methods
Reusable block of code. Define object’s behaviors.
OOP
Object-Oriented Programming
Overloaded methods
Methods with the same name and return type, and a different set of parameters.