5.2 Flashcards
Encapsulation
Encapsulation is the technique of making the states in a class private and providing access to those states via public behaviours
Encapsulation = Data Hiding *
*
If a state is declared private, it cannot be accessed by any method outside the class, thereby hiding the states (and their contents ) within the class.
Four OOP Fundamentals
Abstraction : Removing uncessaru complexity
Polymorphism
Inheritance: Process whereby one object inherits the properties (states and behaviours) of another object (pairs called super/sub or parent/child classes)
Encapsualtion :
Four OOP Fundamentals
Abstraction : Removing uncessaru complexity
Polymorphism:
Inheritance: Process whereby one object inherits the properties (states and behaviours) of another object (pairs called super/sub or parent/child classes)
Encapsualtion : Encapsulation is the technique of making the states in a class private and providing access to those states via public behaviours
polymorphism
polymorphsim means many forms, it means that two methods can have the same name but different parameter lists and processes
two types:
Overloading (same class, different parameters)
Overirding (different classes, same name)
Overloading allows different methods to have same name, but different signatures where signature can differ by number of input parameters or type of input parameters or both
Overriding allows a sub class to provide a specific implementation of a method that is already provided by one of its super classes.
Advantages of Ensapsulation
Data hiding, increased flexbility, reusuability, testing code is easy
t will not be visible to the user that how the class is storing values in the variables.
ncapsulation also improves the reusability of code (write once/use many times).
Methods can be copied to different/new classes and help meet new requirements.
Encapsulated code is easy to test with unit testing (a type of automated testing that tests many different types of data quickly).
It is easier to fix larger programs if you know which method is returning the wrong response.
Incfeased flexibility:
We can make the variables of the class as read-only or write-only depending on our requirement.
If we wish to make the variables as read-only then we can omit the setter methods
Or if we wish to make the variables as write-only then we have to omit the get methods
Advantages of inheritance:
Minimize the amount of duplicate code in an application
B. Better organization of code
C. Code more flexible change
If duplicate code (variables and methods) exists in two related classes, they can be refactored into a hierarchy by moving that common code up to a common superclass.
Moving common code to a super class results in better organization of code (better abstraction).
Inheritance can also make application code more flexible to change because classes that inherit from a common super class can be used interchangeably
Advnatges of polymorphism
Overridden methods allow us to call methods of any of the derived classes without even knowing the type of derived class object
Overloading
We don’t have to create and remember different names for functions doing the same thing
Advantages of libraires of objects
biggest advantage: Time saving!
Sorting algorithms do not have to be re-invented Complex algorithms and processes can be reused
Advantages of OOP and Disadvantages
- Modularity for easier troubleshooting
When working with object-oriented programming languages, you know exactly where to look when something goes wrong. “Oh, the car object broke down? The problem must be in the Car class!” You don’t have to go line-by-line through all your code.
That’s the beauty of encapsulation. Objects are self-contained, and each bit of functionality does its own thing while leaving the other bits alone. Also, this modularity allows an IT team to work on multiple objects simultaneously while minimizing the chance that one person might duplicate someone else’s functionality.
- Reuse of code through inheritance (Abstraction)
Suppose that in addition to your Car object, one colleague needs a RaceCar object, and another needs a Limousine object. Everyone builds their objects separately but discover commonalities between them. In fact, each object is just a different kind of Car. This is where the inheritance technique saves time: Create one generic class (Car), and then define the subclasses (RaceCar and Limousine) that are to inherit the generic class’s traits.
- Flexibility through polymorphism
Riffing on this example, you now need just a few drivers, or functions, like “driveCar,” driveRaceCar” and “DriveLimousine.” RaceCarDrivers share some traits with LimousineDrivers, but other things, like RaceHelmets and BeverageSponsorships, are unique.
Disadvnatges:
OOP typically involve more lines of code than procedural programs.
OOP are typically slower than procedure-based programs, as they typically require more instructions to be executed.
Because OOP is difficult to use, programmers must have strong design and programming skills, as well as sufficient planning.
Time consuming:
It takes some time to become used to OOPs. For some people, the thinking process involved with object-oriented programming is not natural.
Modularity:
Modular programming (also referred to as modular architecture) is a general programming concept. It involves separating a program’s functions into independent pieces or building blocks, each containing all the parts needed to execute a single aspect of the functionality.
Adv:
A. Easier debugging and testing
B. Speedier completion
C. Code blocks reusable
By having smaller modules to test, it is easier to find and fix bugs
By splitting the task into different modules, each module can be worked on concurrently (except for particular circumstances) thus leading to a speedier completion of the overall project
*
Some problems/functions are very common and possibly occur in multiple different programs (for example the need to make a text box or a clickable button to start a task)
By reusing blocks of code, the development time is slashed and the project is completed sooner.