Object-Oriented Programming System (OOPS) in Java is a programming paradigm that uses "objects" to design applications and computer programs. It leverages several fundamental principles to organize software design and development. The main concepts of OOPS in Java include:
- Class and Object
- Inheritance
- Polymorphism
- Encapsulation
- Abstraction
1. Class and Object
Class: A blueprint or template for creating objects. It defines a type by bundling data and methods that work on the data into a single unit.
public class Car {
// Fields (variables)
private String color;
private String model;
// Constructor
public Car(String color, String model) {
this.color = color;
this.model = model;
}
// Methods
public void displayInfo() {
System.out.println("Car model: " + model + ", Color: " + color);
}
}
- Object: An instance of a class. When a class is defined, no memory is allocated until an object of that class is created.public class Main {public static void main(String[] args) {// Creating an object of the Car classCar myCar = new Car("Red", "Toyota");myCar.displayInfo(); // Output: Car model: Toyota, Color: Red}}
2. Inheritance
Inheritance is a mechanism where one class (subclass/child class) inherits the fields and methods of another class (superclass/parent class). It promotes code reusability and establishes a relationship between different classes.
// Superclass
public class Vehicle {
public void start() {
System.out.println("Vehicle started");
}
}
// Subclass
public class Bike extends Vehicle {
public void ride() {
System.out.println("Bike is being ridden");
}
}
public class Main {
public static void main(String[] args) {
Bike myBike = new Bike();
myBike.start(); // Inherited method
myBike.ride(); // Subclass-specific method
}
}
3. Polymorphism
Polymorphism allows objects to be treated as instances of their parent class rather than their actual class. The two types of polymorphism in Java are compile-time (method overloading) and runtime (method overriding).
Method Overloading: Multiple methods with the same name but different parameters.
public class MathUtils {
// Method overloading
public int add(int a, int b) {
return a + b;
}
public double add(double a, double b) {
return a + b;
}
}
- Method Overriding: A subclass provides a specific implementation of a method that is already defined in its superclass.class Animal {public void sound() {System.out.println("Animal makes a sound");}}class Dog extends Animal {@Overridepublic void sound() {System.out.println("Dog barks");}}public class Main {public static void main(String[] args) {Animal myDog = new Dog();myDog.sound(); // Output: Dog barks (runtime polymorphism)}}
4. Encapsulation
Encapsulation is the technique of wrapping the data (variables) and code (methods) together as a single unit. It restricts direct access to some of an object's components, which can prevent the accidental modification of data.
public class Account {
// Private data member
private double balance;
// Public getter method
public double getBalance() {
return balance;
}
// Public setter method
public void setBalance(double balance) {
if (balance >= 0) {
this.balance = balance;
}
}
}
public class Main {
public static void main(String[] args) {
Account myAccount = new Account();
myAccount.setBalance(1000.0);
System.out.println("Account balance: " + myAccount.getBalance()); // Output: Account balance: 1000.0
}
}
5. Abstraction
Abstraction is the concept of hiding the complex implementation details and showing only the necessary features of an object. It can be achieved using abstract classes and interfaces.
Abstract Class: Cannot be instantiated and may contain abstract methods (without implementation).
abstract class Shape {
// Abstract method
abstract void draw();
// Regular method
public void display() {
System.out.println("Displaying shape");
}
}
class Circle extends Shape {
@Override
void draw() {
System.out.println("Drawing circle");
}
}
public class Main {
public static void main(String[] args) {
Shape myShape = new Circle();
myShape.draw(); // Output: Drawing circle
myShape.display(); // Output: Displaying shape
}
}
- Interface: A reference type in Java, it is similar to a class and is a collection of abstract methods. A class implements an interface, thereby inheriting the abstract methods of the interface.interface Animal {void eat();void sleep();}class Cat implements Animal {@Overridepublic void eat() {System.out.println("Cat eats");}@Overridepublic void sleep() {System.out.println("Cat sleeps");}}public class Main {public static void main(String[] args) {Animal myCat = new Cat();myCat.eat(); // Output: Cat eatsmyCat.sleep(); // Output: Cat sleeps}}
Summary
OOPS in Java provides a framework for creating modular, reusable, and maintainable code. By leveraging classes and objects, inheritance, polymorphism, encapsulation, and abstraction, Java enables developers to design applications that are both efficient and easy to understand.
No comments:
Write comments