OOPs concept

  • OOP stands for Object Oriented programming
  • Before OOPs there were procedural programming where it was all about the functions and variables written into a single file. Where variables stored data and functions performed operations on that data.
  • After oops concept came up we divided the functions and variables into logical groups called class and started calling functions and variables as methods and properties respectively.

List of OOPs Concept in Java with Example

  • Baiscs
    • Classes – Logically Grouping of Similar Entities
    • Objects – Instance of classes
  • 4 Pillars
    • Encapsulation – Bundling data and methods into single unit (class/enums)
    • Abstraction – Looking at something without being concerned about the inner details
    • Polymorphism – one interface and have multiple implementations
    • Inheritance – Inheriting the properties of parent class is called inheritance
  • Relational Concept
    • Association
    • Aggregation
    • Composition
    • Coupling and cohesion

Encapsulation

  • Encapsulation is the idea of bundling data and methods that work on that data within one unit. Eg class in java
  • Note : Lesser that number of arguments to the methods, better the code.
    ” The best functions are those with no parameters!” – Robert C Martin

Abstraction

  • Looking at something without being concerned about the inner details or mechanics.
  • Example : While Driving a car, person need not be concerned about what happens internally when you press a accelerator or a break but what he needs to know is which function to call to actually perform those operations.

Polymorphism (Many forms)

  • Polymorphism refers to a programming language’s ability to process objects differently depending on their data type or class.
  • Polymorphism allows you to define one interface and have multiple implementations
  • There are two types of polymorphism in java
    • Compile time polymorphism
    • Run time polymorphism

Inheritance

  • Inheritance is a mechanism in which one class acquires the property of another class.
  • For example, a child inherits the traits of his/her parents. With inheritance, we can reuse the fields and methods of the existing class.
  • Hence, inheritance facilitates Reusability and is an important concept of OOPs

Association (can live without each other)(loosly coupled)

  • Association is a semantically weak relationship (a semantic dependency) between otherwise unrelated objects.
  • An association is a “using” relationship between two or more objects in which the objects have their own lifetime and there is no owner.
  • As an example, imagine the relationship between a Manager object and SwipeCard object. SwipeCard Object is independent of Manager object. Swipecard only needs an object of
public class Employee{
   private String userName;
   private String privateKey;
}
public class Manager extends Employee{
}
public class SwipeCard{
   public boolean isValid(Employee e){
       return true or false;
   }
}

//SwipeCard  object would be independent of Manager object

Aggregation (Single owner of the object)

  • In this technique, all objects have their separate lifecycle. However, there is ownership such that child object can’t belong to another parent object.
  • For example consider class/objects department and employee. Here, a single employee can’t belong to multiple departments, but even if we delete the department, the employee object will never be destroyed.
public class Department {
   private List<Employee> = new ArrayList<>();
}

Composition (Tightly coupled)

  • Composition is a specialized form of aggregation. 
  • In composition, if the parent object is destroyed, then the child objects also cease to exist.
  • Composition is actually a strong type of aggregation
  •  As an example, a house may be composed of one or more rooms. If the house is destroyed, then all of the rooms that are part of the house are also destroyed.
public class House
{
   private Room room;
   public House()
   {
       room = new Room();
   }
}
Class diagram - Wikipedia

Others Benefits of java : Robust

  • One of the major advantages of Java over C++ is that it prevents memory leaks.
  • Java manages memory on its own and does garbage collection automatically.
  • Bad memory management in C++ is a big source of errors in programs.

Others Benefits of java : Secure

  • Java code runs inside the JVM.
  • This creates a sandbox that makes it hard for Java code to do evil things to the computer it is running on.

Reference :

Leave a Comment