Object Oriented Programming and their concepts
What is Object Oriented Programming(OOP)?
Object-oriented programming is a programming paradigm based on the concept of “objects,” which can include both data and code in the form of fields (also known as attributes or properties) and “classes”(often known as methods).
It’s utilized to break down a software program into reusable code blueprints (called classes) that may be used to build specific instances of things. Object-oriented programming languages include JavaScript, C++, Java, and Python.
Advantages of Object Oriented Programming
- Code reusability
- Code Maintenance
- Data Redundancy
- Flexibility through polymorphism
- Security
- Easy troubleshooting
- Better Productivity
Building blocks of OOP
The fundamental building blocks of OOP
- Classes
- Object
- Methods
- Attributes
Classes
They are user defined data types. Class is a blueprint of an object.The structure of methods and properties is defined by classes. This blueprint is used to build or instantiate individual objects.
Example: Employee class has employee number, name, address and salary as attributes while calculate salary, calculate OT amount as methods
Objects
Objects are considered as instances of classes created using specific data.
Example: A new object is created named ‘John’ to the Employee class
Attributes
The information that is stored is referred to as attributes. Individual objects have data stored in the Attributes field when they are instantiated.
Example: employee number, name, address and salary in Employee class
Methods
Behaviors are represented through methods. Methods execute activities, such as returning information about an object or updating the contents of an object.
Example: calculate salary, calculate OT amount in Employee class
What are the four concepts of OOP?
- Encapsulation
- Abstraction
- Inheritance
- Polymorphism
Encapsulation
Encapsulation is the process of enclosing all critical information within an object and only revealing a subset of it to the outside world.
“It is the process of gathering together similar properties and methods, naming the unit, and providing an interface (public functions) for outsiders to communicate with the unit.”
Encapsulation requires designating some fields as private while others are made public.
Private/Internal interface: Methods and properties that are only accessible from other methods in the same class
public / external interface: Methods and properties that are accessible from outside the class
variable “name” is kept private or “encapsulated”
Employee.java
package com.javatpoint;
public class Employee {
private String name;
public String getName(){
return name;
}
public void SetName(String name){
this.name = name
}
}
Department.java
package com.javatpoint;
class Department {
public static void main(String[] args) {
Employee emp = new Employee();
emp.setName("John");
System.out.println(emp.getName());
}
}
Output: John
Abstraction
Abstraction refers to the user’s interaction with only a subset of an object’s attributes and operations. To access a complex object, abstraction use simple, high-level techniques.
- Simple items are used to show complexity.
- Keep complicated details hidden from the user.
Simple classes are used to indicate complexity in abstraction. Encapsulation is an extension of abstraction.
Inheritance
Classes can inherit features from other classes through inheritance. The inheriting class is referred to as a subclass or a child class. The initial class is frequently referred to as the parent. The keyword extends is used to create a new class that inherits properties from an existing one. Inheritance supports reusability.
class Animal{}
class Cat extends Animal {}
Polymorphism
Polymorphism refer to the designing objects to share behaviours. Objects can override shared parent behaviors with specific child behaviors through inheritance. Method overriding and method overloading are two ways that polymorphism allows the same method to perform various actions.
Method Overriding ; To override a method of its parent class, the child class can use OOP polymorphism concept. This allows a programmer to use the same method in different ways depending on whether the method is called by a parent class object or a child class object.
Thank you!