Encapsulation
Encapsulation is the 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.
It is a form of data hiding. If a state is declared private it cannot be accessed by any method outside the class.
Inheritance
A process whereby one object inherits the properties of another object (called super / sub or parent / child classes).
This is called an “is-a” relationship.
In java you can only inherit from one class at a time.
Keyword Inheritance
The keyword used is extends.
In programming inheritance is implemented by using the keyword extends in the subclass to connect it to its super class.
Polymorphism
It means that two methods can have the same name but different contents / functions.
Methods have the same name but different parameter lists and processes.
Types of Polymorphism
Overloading
Overriding
Overloading
The same class
Allows different methods to have the same name, but different signatures where signature can differ by the number of input parameters or type of input parameters or both.
Overriding
Different classes / inheritance
Allows a subclass 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 overrides the method in the superclass.
Overloading versus Overriding
Overloading is about the same function having different signatures (usually in the same class).
Overriding is about the same function, same signature, but different classes connected through inheritance.
Advantages of Encapsulation
Data hiding
Reusability
Testing code ease
Increased flexibility
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 how the class is storing values in the variables.
They only know that we are passing the values to a setter method and variables are getting initialized with that value.
Reusability (Encapsulation)
Encapsulation also improves the reusability of the code (write once / use many times)
Methods can be copied to different / new classes and help meet new requirements.
Ease of testing code (Encapsulation)
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 problems if you know which method is returning the wrong response.
Increased Flexibility (Encapsulation)
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
Minimizes the amount of duplicate code
Common code becomes the superclass
Organizes code better
Using a superclass has better abstraction
Makes code more flexible to change
Can make application code more flexible to change as classes that inherit from a common super class can be used interchangeably.
a parent object holds common data and
actions, which enhances reuse and reduces maintenance overheads
Advantages of Polymorphism
Overriding
It allows a general class to specify methods that will be common to all of its derivatives while allowing sub classes to define the specific implementation of some or all of those methods.
One interface, multiple methods - overridden methods
Allow you to call methods of any of the derived classes without even knowing the type of derived class object.
Overloading
Do not need different names for functions doing the same thing
Advantages of Libraries of Objects
Time saving:
Sorting algorithms do not have to be re-invented
Complex algorithms and processes can be reused
Libraries of Objects
JUnit (64% of top Java projects)
Testing framework for java programs
SLF4J 22%
Allow the end-user to plug-in the desired logging system
Log4j 16.76%
A tool to help the programmer output log statements to a variety of output targets
Google Guava 15.6%
Common libraries for Java, mainly developed by Google engineers.
Apache-commons 12.63%
Commons proper is dedicated to one goal: create and maintain reusable Java components.
Disadvantages of OOP
Increased complexity of small problems
OOP typically involves more lines of code than procedural programs.
OOP are typically slower than procedure-based programs, as they typically require more instructions to be executed.
Unsuited to particular classes of problems
Programs may lend themselves well to functional-programming style, logic-programming style, or procedural-based programming, and using object oriented will not create efficient programs.
Problems that tend to be small and involve only one data source.
Programming Teams Advantages
Speed of completion (concurrent working)
Information hiding to reduce module dependencies
Developers only need to know the name and required parameters to use a behavior, they can use code without needing to understand how it functions
Security benefit as coders do not have access to the actual data themselves
Expertise in a narrow field
Assembling a complex project requires many different skills.
By allowing each part of the program to be programmed by an expert in that field, the quality of the final product is higher than one by a jack of all trades.
Programming Teams Disadvantage
You need a common language to resolve problems
Advantages of Modularity
Easier debugging and testing
By having smaller modules to test, it is easier to find and fix bugs
The number of tests that have to be run to confirm a module is fully operational is reduced according to the number of functions in a particular module.
Speedier completion
By breaking the project into smaller modules, you could save time by finding modules that already exist in libraries that do the function you are looking for
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
Code blocks are reusable
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 resusing blocks of code, the development time is slashed and the project is completed sooner.