Object Oriented Programming abbreviated as OOPs i dont know why some people put s there but who cares it sounds much better
Object Oriented Programming is a paradigm of programming which is the way we write our code more systematically using and following certain Rules and Regulations Few important terms which we come across OOP are classes and objects. Classes are Blueprint or templates for creating objects.object are instances of classes
OOP has 4 important and terms which rule over this entire paradigm (style)
Encapsulation
Abstraction
Inheritance
Polymorphism
Encapsulation
Encapsulation is the principle of bundling data (attributes) and methods (behaviors) that operate on that data into a single unit, known as a class. It involves restricting access to certain parts of the object, typically by making attributes private and providing public methods to access and modify them. This helps to protect the integrity of the data and ensures controlled access and manipulation of object state.
Abstraction
Abstraction is the process of hiding the implementation details of a class and showing only the essential features to the user. It involves focusing on what an object does rather than how it achieves it. By defining abstract classes and interfaces, Java allows developers to create abstract representations of real-world objects, enabling code reuse, modularity, and flexibility in software design.
Inheritance
Inheritance is a mechanism in object-oriented programming where a new class (subclass or derived class) can inherit properties and behaviors (methods) from an existing class (superclass or base class). It promotes code reuse and establishes a hierarchical relationship between classes. Subclasses can extend the functionality of their superclass by adding new methods or overriding existing ones. This facilitates code organization, enhances maintainability, and promotes the principle of "is-a" relationship among objects.
Polymorphism
Polymorphism is the ability of different objects to respond in different ways to the same message or method invocation. It allows objects of different types to be treated as objects of a common superclass through inheritance, enabling code to be written that operates on objects of various types without needing to know their specific implementations. Polymorphism can be achieved through method overriding (runtime polymorphism) and method overloading (compile-time polymorphism), providing flexibility, extensibility, and code reuse in object-oriented systems.
///////////////////////// COMPILE TIME POLYMORPHISM ///////////////////////
package Polymorphism;
class MathOperations{
public int add(int a, int b){
return a + b;
}
public int add(int a, int b, int c){
return a + b + c;
}
public double add(double a, double b){
return a + b;
}
}
public class MethodOverloading {
public static void main(String[] args) {
MathOperations math = new MathOperations();
System.out.println("sum of 5 and 10: " + math.add(5,10));
System.out.println("sum of 5,10 and 15" + math.add(5,10,15));
System.out.println("sum pf 5.5 and 10.5" + math.add(5.5, 10.5));
}
}
////////////////////////// RUN TIME POLYMORPHISM //////////////////////////////
package Polymorphism;
class Animal{
public void makeSound(){
System.out.println("Animal makes a sound");
}
}
class Dog extends Animal{
@Override
public void makeSound(){
System.out.println("Dog Barks");
}
}
class Cat extends Animal{
@Override
public void makeSound(){
System.out.println("Cat meows");
}
}
public class Main {
public static void main(String[] args){
//Polymorphism
Animal myDog = new Dog();
Animal myCat = new Cat();
myCat.makeSound();
myDog.makeSound();
}
}
Check out this Repo for better Understanding and Different types of inheritance
What are this
and super
in OOP?
this: In Object-Oriented Programming (OOP),
this
refers to the current object. It's like saying "myself" or "the object I am currently working with". You usethis
to access or modify the attributes and methods of the current object.super: In OOP,
super
refers to the superclass of the current object. It's like saying "my parent" or "the class I'm extending from". You usesuper
to access or invoke methods from the superclass, or to call the superclass constructor from the subclass.
Interfaces
Interfaces in Object-Oriented Programming (OOP) define a contract for classes to follow. They specify a set of methods that implementing classes must provide, without specifying how those methods are implemented.
Difference Between Interfaces and Abstraction?
Abstraction:
Abstraction is a broader concept that involves hiding the implementation details of a class and showing only the essential features to the user.
It focuses on what an object does rather than how it does it, allowing users to interact with objects at a higher level without needing to know the internal workings.
Abstraction can be achieved through abstract classes and interfaces, as well as other mechanisms like encapsulation and inheritance.
Interfaces:
An interface is a specific type of abstraction that defines a contract for classes to follow.
It specifies a set of methods that implementing classes must provide, without specifying how those methods are implemented.
Interfaces facilitate loose coupling and polymorphism, as classes implementing the same interface can be used interchangeably.