I usually see developers who are learning the concepts of OOP and are having really hard time understanding it.
Let me try to put it in a simpler way so it might help.
Basically OOP or object oriented programming is a methodology or idea about simplifying any complex application by following certain principles such as Encapsulation, Inheritance, Abstraction & Polymorphism.
So, let's try understanding the principles of OOP in a simpler way.
1) Encapsulation: Is nothing but a process of wrapping the code or data as a single unit. i.e. as an object. Basically when we create a class we don't want anyone to access any properties directly so we have access modifiers such as getters and setters to access the private method in a specific class.
Eg:
class Encapsulation {
private int id;
private String name;
public int getId() { return this.id }
public void setId(int id) { this.id = id}
public String getName() { return this.name }
public void setName(String name) { this.name = name}
}
2) Inheritance : Is nothing but a concept of passing on the attributes and functions from one class to another by using the keyword extends. If we have a common class whose values or methods might be needed in other class as well and we don't want to rewrite it. in that case we use inheritance.
Eg:
class Human {
private String name;
private int age;
private float height;
// Getters and setters for the encapsulated class
}
class Women extends Human {
private boolean canReproduce;
// can have their own method or use the parent class methods
public Women () {
super(); // super calls the default constructor in the parent class so we have access to all the methods and values. }
// getters and setters for own attributes
}
class Men extends Human {
// can have their own method or use the parent class methods
public Men () {
super();
// getters and setters for own attributes
}
}
3) Abstraction : Is nothing but a way of highlighting the rules and hiding the internal implementation in a application. There are two ways of achieving abstraction i.e. by using abstract keyword and by creating interface for a class.
Eg using abstract keyword
abstract Class AbstractEg{
public void printName(){//code to print name }
public abstract Int getAge(int id); // have methods definition and no implementation.
}
class AbstractEgImplementation implements AbstractEg {
@Override
public int getAge(int id) {// logic implementation}
}
(Note : wherever we create an abstract method the class need to be declared as abstract as well.)
There is another way of achieving abstraction and it is by using interfaces. By default interface is abstract so we don't need to define abstract keyword in it and should have implementation for the interface.
[NOTE: We cannot create instance of an interface ]
Eg:
public interface Demo{
void doSomething();
}
public class DemoImpl implements Demo {
@Override
public void doSomething() {// logic implementation}
}
Why is it needed ?
-> Basically when we don't want to show the internal process what is happening but instead just highlight the rules we use abstraction and also the program execution gets faster.
How to use it ?
-> So from the client we create the instance of the implementation but return the Object of interface to hide the process or other methods inside the method making it non accessible using the instance instead only from the impl file where it is needed.
Eg:
Demo demo = new DemoImpl();
demo.methodsYouWantToAccess
4) PolyMorphism : Is nothing but a way of defining more forms of how we represents the data. where ploy means many and morphism means forms.
There are two ways to achieve polymorphism. I.e. method overloading and method overriding.
- Method Overloading is the way of writing the same method with multiple arguments depending upon the nature of the task and necessity. The same methods can have multiple Arguments or different data types to make them unique although the reference name is same.
Eg:
public int getName(){//code here}
public int getName(int Id){//code here to get name based on id}
public int getName(String grade){//code here to get name based on the grade}
Method overriding:
It is a process of overriding or writing your own implementation of the function coming from the super or parent class. to achieve it we use @Override annotation.
Eg:
@Override
public int getAge(String name) { // own logic and implementation here}
in general we can say OOP is a widely used methodology to structure you app in a better way by mainly focusing on the code reusuability, promoting modular design, avoiding code duplication, restricting access of private fields improving security, along with focusing more on what an object does instead of how it does it.