Object-Oriented Programming System (OOPS) is a programming paradigm based on the concept of objects. These objects encapsulate data (attributes) and behavior (methods) into a single entity, promoting reusability, scalability, and modularity in software design. Java is a fully object-oriented programming language and follows the principles of OOPS.
Basic Concepts of OOPS
1. Class
2. Object
3. Encapsulation
4. Inheritance
5. Polymorphism
6. Abstraction
1. Class
A class is a blueprint or template for creating objects. It defines the properties (fields) and methods (functions) that the objects will have.
Syntax:
class ClassName {
// Fields
// Methods
}
Example:
class Car {
String brand; // Field
int speed; // Field
void displayInfo() { // Method
System.out.println(“Brand: ” + brand + “, Speed: ” + speed);
}
}
2. Object
An object is an instance of a class. It represents a real-world entity with state (fields) and behavior (methods).
Syntax:
ClassName obj = new ClassName();
Example:
java
Copy code
public class Main {
public static void main(String[] args) {
Car car = new Car(); // Object creation
car.brand = “Toyota”; // Setting field values
car.speed = 120;
car.displayInfo(); // Calling a method
}
}
Output:
Brand: Toyota, Speed: 120
3. Encapsulation
Definition: Encapsulation is the process of wrapping data (fields) and methods into a single unit (class) and restricting direct access to them using access modifiers.
Advantages:
Protects the integrity of data.
Provides control over how data is accessed or modified.
Implementation:
Make fields private.
Provide public getter and setter methods to access and modify private fields.
Example:
class Employee {
private String name; // Private field
// Getter
public String getName() {
return name;
}
// Setter
public void setName(String name) {
this.name = name;
}
}
public class Main {
public static void main(String[] args) {
Employee emp = new Employee();
emp.setName(“Alice”); // Setting value using setter
System.out.println(“Employee Name: ” + emp.getName()); // Accessing value using getter
}
}
4. Inheritance
Definition: Inheritance allows a class (child or subclass) to inherit fields and methods from another class (parent or superclass).
Advantages:
Promotes code reuse.
Supports hierarchical classification.
Types in Java:
Single Inheritance
Multilevel Inheritance
Hierarchical Inheritance
Example:
class Animal {
void eat() {
System.out.println(“This animal eats food.”);
}
}
class Dog extends Animal {
void bark() {
System.out.println(“The dog barks.”);
}
}
public class Main {
public static void main(String[] args) {
Dog dog = new Dog();
dog.eat(); // Inherited method
dog.bark(); // Dog’s own method
}
}
Output:
This animal eats food.
The dog barks.
5. Polymorphism
Definition: Polymorphism allows the same method or operator to perform different tasks based on the context.
Types:
Compile-Time Polymorphism (Method Overloading):
Same method name with different parameter lists in the same class.
Run-Time Polymorphism (Method Overriding):
Redefining a method in a subclass that is already defined in the parent class.
Method Overloading Example:
class Calculator {
int add(int a, int b) {
return a + b;
}
double add(double a, double b) {
return a + b;
}
}
public class Main {
public static void main(String[] args) {
Calculator calc = new Calculator();
System.out.println(calc.add(5, 10)); // Calls int method
System.out.println(calc.add(5.5, 10.5)); // Calls double method
}
}
Method Overriding Example:
class Animal {
void sound() {
System.out.println(“Some generic animal sound.”);
}
}
class Cat extends Animal {
@Override
void sound() {
System.out.println(“The cat meows.”);
}
}
public class Main {
public static void main(String[] args) {
Animal myCat = new Cat();
myCat.sound(); // Calls the overridden method in Cat
}
}
Output:
The cat meows.
6. Abstraction
Definition: Abstraction is the process of hiding implementation details and showing only the essential features of an object.
Implementation:
Abstract classes (abstract keyword).
Interfaces.
Advantages:
Improves code modularity.
Enhances flexibility in design.
Abstract Class Example:
abstract class Shape {
abstract void draw(); // Abstract method
}
class Circle extends Shape {
void draw() {
System.out.println(“Drawing a circle.”);
}
}
public class Main {
public static void main(String[] args) {
Shape shape = new Circle();
shape.draw(); // Calls the implemented method in Circle
}
}
Key Advantages of OOPS
Reusability: Inheritance and modularity reduce code duplication.
Modularity: Encapsulation and abstraction make programs easier to manage and debug.
Flexibility: Polymorphism allows dynamic method invocation.
Scalability: Promotes the design of extensible and maintainable systems.