PTS_CCPROG3 Recap of OOP
- this recap leads up to the other parts/rest of the discussion
Recap
The four pillars of OOP are
- abstraction
- encapsulation
- inheritance
- polymorphism
Sources
- FreeCodeCamp: The Four Pillars of Object-Oriented Programming
- Scaler: Four Pillars of OOPs (Object Oriented Programming)
- the old PPT slides from sir leif like 2 years ago
- Programiz: Java Polymorphism
Other Sources - Dev: Object-Oriented Programming (OOP): Understand the 4 Pillars with Clear Examples
Object Oriented Programming
- this is a paradigm; so this is the main influence on the programming language
- OOP is a philosophy behind the programming language, in our case, Java.
- It gives the rationale behind the programming language. Rationale is like underlying reasons, principles, and justification behind a decision.
- on the other hand, programming languages can be influenced by multiple paradigms (procedural, functional, logical, etc.)
- Classes are templates or blueprints, they serve as the foundation of an object.
- For example, you have a recipe for a torch or a piece of cooked food. That recipe is your class. It contains everything you have to know.
- Let's say we have a Food class. It will have the number of hearts it can restore, and the number of hunger points it will restore. Those are the attributes of that class.
- Objects are instances of classes, has its own copy of attributes.
- For example, if I have a piece of meat in Minecraft. It could be an instance of a Food class. There are different types of food in Minecraft, for meat there's beef and pork.
- They both come from the Food class but for example their "origin" attribute is different. Beef comes from cows, pork comes from pigs.
Abstraction
- Only relevant data and behavior are exposed at any given time, the rest are abstracted away.
- Abstraction is "about hiding away the implementation details inside something – sometimes a prototype, sometimes a function." (FCC)
- "So when you call the function you don't have to understand what it's doing" (FCC)
- "Finding things that are similar in your code and providing a generic function or object to serve multiple places/with multiple concerns." (FCC)
- "Abstraction in OOP is accomplished through the usage of abstract classes and interfaces." (Scaler)
How to achieve abstraction?
- Interfaces: "In object oriented programming, an interface generally defines the set of methods (or messages) that an instance of a class that has that interface could respond to." (StackOverflow) It can contain abstract methods and constants but contains no method bodies. (Similar to an outline or skeleton of a class.) Uses
implements. - Abstract Classes: declared using the abstract keyword, classes have to
extendthis class and it cannot be instantiated directly (GeeksForGeeks)
Encapsulation
- "The action of enclosing something in or as if in a capsule. Each object in your code should control its own State" (FCC)
- Objects must be the only entity responsible for maintaining its own states.
- Involves binding data members and methods into a single unit (G4G)
How to achieve encapsulation?
- "Limit what pieces of your code can access. Make more things inaccessible, if they aren't needed." (FCC)
- Use getters and setters (GeeksForGeeks)
- Use getters to give read access to private data
- Use setters to give edit access to private data
Encapsulation vs Abstraction?
- Encapsulation focuses more on the bundling of data and methods together and protecting the data, focusing more on implementation and security.
- for data hiding and data integrity
- Abstraction is more about hiding the more complex details, focusing more on the design and interface.
- for reducing complexity and improving maintainability
Inheritance
- Objects may acquire some, if not all, of the properties and behaviors of another Object, typically their parent Object
- "Inheritance lets one object acquire the properties and methods of another object." (FCC)
- It "allows a class (subclass) to inherit properties or methods from another class (superclass)"
Why do we need this?
- to avoid repeating code
- more organized and maintainable code
- this forms the basis for polymorphism which allows flexible code
For example:
- Mob Class (superclass)
- attributes: health, is_hostile, movement_speed
- methods: takeDamage(), move(), spawn()
- Creeper extends Mob
- attributes: health, is_hostile, movement_speed, is_tamed, owner
- methods: takeDamage(), move(), spawn(), blowUp()
- Wolf extends Mob
- attributes: health, is_hostile, movement_speed,
- methods: takeDamage(), move(), spawn(),
Polymorphism
- Poly means "many", morph means "shape" (Greek). This refers to an "object's ability to take on numerous forms or behaviors" (Scaler)
- Objects may have multiple types; Apart from their Object type, they also have the Object type of their parent Objects.
- It "allows Objects of different kinds to be treated consistently when they share a common superclass" (Scaler)
Why do we need this?
- allows us to create consistent code (Programiz)
How to achieve polymorphism?
- Method overriding: (inheritance), the same method is present in both the superclass and subclass but the implementation/code inside are different
- Method overloading: create methods with the same name but they differ in parameters
- Operator overloading: An example is concatenation + and addition +. They both use the same symbol, but serve different functions.
Wrapping it all up
- Abstraction is for hiding complexity, achieved through interfaces and abstract classes, and simplifies the user's interaction with the code.
- Encapsulation is about data security and integrity, achieved by using getters and setters, and ensures that an object's state can only be modified in a predictable and controlled way.
- Inheritance promotes code reusability and organization. It allows a subclass to acquire the properties and behaviors of an existing superclass. The "is-a" relationship helps avoid redundant code and creates a hierarchical structure.
- Polymorphism provides flexibility and consistency. It allows objects of different classes that share a common superclass to be treated interchangeably. This can be done through method overriding and method overloading.
These principles work together to help create a system containing modular code that is easy to understand, secure, and adaptable. OOP is a design philosophy that leads to more efficient and scalable software development.