Object-Oriented Programming (OOP) is a programming paradigm that organizes code into objects, which represent real-world entities. Java is an object-oriented programming language, and it implements various OOPs concepts. Here is a list of important OOPs concepts in Java along with examples:
1. Class and Object:
A class is a blueprint for creating objects, and an object is an instance of a class.
// Example:
class Car {
String brand;
String model;
void start() {
System.out.println("Car is starting...");
}
}
public class Main {
public static void main(String[] args) {
Car myCar = new Car();
myCar.brand = "Toyota";
myCar.model = "Camry";
myCar.start();
}
}
2. Encapsulation:
Encapsulation is the process of binding data (attributes) and methods (behaviors) together in a single unit (class) to control access.
// Example:
class BankAccount {
private double balance;
public void deposit(double amount) {
if (amount > 0) {
balance += amount;
}
}
public double getBalance() {
return balance;
}
}
public class Main {
public static void main(String[] args) {
BankAccount account = new BankAccount();
account.deposit(1000);
double balance = account.getBalance();
System.out.println("Balance: $" + balance);
}
}
3. Inheritance:
Inheritance allows a class (subclass) to inherit properties and behaviors from another class (superclass).
// Example:
class Animal {
void makeSound() {
System.out.println("Animal makes a sound");
}
}
class Dog extends Animal {
void makeSound() {
System.out.println("Dog barks");
}
}
public class Main {
public static void main(String[] args) {
Animal animal = new Animal();
animal.makeSound();
Dog dog = new Dog();
dog.makeSound();
}
}
4. Polymorphism:
Polymorphism allows objects to be treated as instances of their superclass, enabling multiple classes to be used interchangeably.
// Example:
class Shape {
void draw() {
System.out.println("Drawing a shape");
}
}
class Circle extends Shape {
void draw() {
System.out.println("Drawing a circle");
}
}
class Square extends Shape {
void draw() {
System.out.println("Drawing a square");
}
}
public class Main {
public static void main(String[] args) {
Shape shape1 = new Circle();
Shape shape2 = new Square();
shape1.draw();
shape2.draw();
}
}
5. Abstraction:
Abstraction allows you to define the structure and behavior of objects without exposing their implementation details.
// Example:
abstract class Shape {
abstract void draw();
}
class Circle extends Shape {
void draw() {
System.out.println("Drawing a circle");
}
}
class Square extends Shape {
void draw() {
System.out.println("Drawing a square");
}
}
public class Main {
public static void main(String[] args) {
Shape shape1 = new Circle();
Shape shape2 = new Square();
shape1.draw();
shape2.draw();
}
}
6. Interface:
An interface is a contract that defines a set of abstract methods. Classes can implement interfaces to provide specific implementations for those methods.
// Example:
interface Drawable {
void draw();
}
class Circle implements Drawable {
public void draw() {
System.out.println("Drawing a circle");
}
}
class Square implements Drawable {
public void draw() {
System.out.println("Drawing a square");
}
}
public class Main {
public static void main(String[] args) {
Drawable shape1 = new Circle();
Drawable shape2 = new Square();
shape1.draw();
shape2.draw();
}
}
These are some of the key OOPs concepts in Java, and they provide a foundation for creating well-organized, maintainable, and extensible code.