OOP Flashcards
what are the components of an object
- states: can be variables or data structures
- behaviors: can be functions, procedures or methods
what are the components of an object?
- states(data): can be variables or data structures
- behaviors(actions): can be functions, procedures or methods
what is an object?
particular instance of a class, where the object can be a combination of variables or data structures (called states) and functions, procedures and methods (called variables)
UML diagram components
- ClassName
- State (variables) e.g.:
- name: String
- age: int - behaviors (state) e.g.:
+ getAge() : String
- setName( name: String ) : void
- private
+ public
# protected
dependency
it increases maintenance overheads, meaning that changes that need to be made to the entire system if you make a change to a component
encapsulation
technique of making the states in a class private and providing access to those states via public behaviors (methods). data and actions are limited to the object in which they are created.
if a state is declared private, it cannot be accessed by any method outside the class. encapsulation is also referred as data hiding.
Advantages of encapsulation
- Data hiding
the user will have no idea about the inner implementation of the class - implies security.
it will not be visible to the user that how the class is storing values in the variables.
He/she only knows that we are passing the values to a setter method and variables are getting initialized with that value - Increased 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 - Reusability
Encapsulation also improves the reusability of code
methods can be copied to different/new classes and help meet new requirements - Testing code is easy
encapsulated code is easy to test with unit testing
it’s easier to fix larger programs if you know which method is returning the wrong example
Inheritance
process whereby one object inherits the properties (states and behaviors) of another object (pairs called super/sub or parent/child classes)
Java keyword inherits means extends
advantages of Inheritance
- Minimize the amount of duplicate code in an application
- better organization of code
- code more flexible change
Polymorphism
it means many forms; two methods can have the same name but different contents/functions
methods have the same name but different parameter lists and processes.
Polymorphism: overloading
allows different methods to have the same name, but different signatures where signature can differ by number of input parameters or type of input parameters or both.
we don’t have to create and remember different names for functions doing the same thing. e.g. if overloading was not supported by Java, we would have to create method names like sum1, sum2… or sum2int, sum3int…
Polymorphism: overriding
allows a sub class to provide a specific implementation of a method that is already provided by one of its super classes
when a method in a subclass has the same name, same parameters or signature and same return type as a method in its super-class, then the method in the subclass is said to override the method in the super-class.
“one interface, multiple methods”.
it allows us to call methods of any of the derived classes without even knowing the type of a class.
why is polymorphism essential to OOP?
it allows general class to specify methods that will be common to all of its derivatives, while allowing sub classes to define specific implementation of some or all of those methods
disadvantages of OOP
- increased complexity for small problems:
Involves more lines of code than procedural programs.
It’s slower than other procedural programs as it requires more instructions to be executed. - unsuited to particular classes of problems:
there are problems that lend themselves well to functional programming style, logic-programming style or procedure based programming style and applying OOP in those situations will not result efficient programs.
e.g. why make an object when using a string will do?
what is modularity?
Modularity is the property of a system that has been decomposed into a set of cohesive and loosely coupled modules. Modularity is supported by using different code files and classes. Codes are more efficient because of features of OOP.