Object-Oriented Programming (OOP) is a programming paradigm that emphasizes the use of objects to represent real-world entities and concepts. Java is an object-oriented programming language, which means that it provides features and constructs that allow developers to write code using an OOP approach.
Here are some of the key features of OOP in Java:
1. Classes and Objects
2. Inheritance
3. Polymorphism
4. Encapsulation
5. Abstraction
1. Classes and Objects:
In Java, a class is a blueprint for creating objects. It defines the properties and behaviors that an object of that class will have. An object is an instance of a class, and can be created using the new keyword. For example:
public class Car {
private String make;
private String model;
private int year;
public Car(String make, String model, int year) {
this.make = make;
this.model = model;
this.year = year;
}
public void start() {
// Code to start the car
}
public void stop() {
// Code to stop the car
}
}
Car myCar = new Car("Honda", "Civic", 2022);
In this example, the Car class defines the properties make, model, and year, as well as the methods start() and stop(). An object of the Car class is created using the new keyword, and the object's properties are set using the constructor.
2. InheritanceIn Java, a class can inherit properties and behaviors from another class. This is known as inheritance. The subclass (or child class) extends the superclass (or parent class), and inherits all of its public and protected methods and properties. For example:
public class SportsCar extends Car {
private boolean turbo;
public SportsCar(String make, String model, int year, boolean turbo) {
super(make, model, year);
this.turbo = turbo;
}
public void launch() {
// Code to launch the sports car
}
}
SportsCar mySportsCar = new SportsCar("Porsche", "911", 2022, true);
In this example, the SportsCar class extends the Car class, and adds a new property turbo and a new method launch(). The SportsCar constructor calls the Car constructor using the super keyword to set the make, model, and year properties.
3. Polymorphism:In Java, polymorphism allows objects of different classes to be used interchangeably. This is achieved through inheritance and interfaces. Polymorphism can be achieved in two ways:
Method Overriding: When a subclass provides its own implementation of a method that is already defined in its superclass. For example:
public class Car {
public void drive() {
// Code to drive the car
}
}
public class SportsCar extends Car {
@Override
public void drive() {
// Code to drive the sports car
}
}
Car myCar = new SportsCar();
myCar.drive(); // Calls the drive() method in SportsCar class
Method Overloading: When a class provides multiple methods with the same name but different parameters. For example:
public class Calculator {
public int add(int a, int b) {
return a + b;
}
public double add(double a, double b) {
return a + b;
}
}
Calculator myCalculator = new Calculator();
int result1 = myCalculator.add(3, 4); // Calls the int add(int a, int b) method
double result2 = myCalculator.add(3.5, 4.5); //
4. Encapsulation:
In Java, encapsulation is the practice of hiding the implementation details of a class from the outside world, and providing a public interface for accessing and modifying the object's properties. This is achieved through access modifiers such as private, public, protected, and default. For example:
public class BankAccount {
private double balance;
public void deposit(double amount) {
balance += amount;
}
public void withdraw(double amount) {
balance -= amount;
}
public double getBalance() {
return balance;
}
}
BankAccount myAccount = new BankAccount();
myAccount.deposit(1000);
myAccount.withdraw(500);
double balance = myAccount.getBalance();
In this example, the balance property is declared as private, which means that it can only be accessed from within the BankAccount class. The public methods deposit(), withdraw(), and getBalance() provide a way for external code to interact with the BankAccount object.
5. Abstraction:In Java, abstraction is the process of defining a class or interface in terms of its behavior, without specifying the implementation details. This allows code to be written at a higher level of abstraction, which makes it easier to understand and maintain. For example:
public interface Animal {
void eat();
void sleep();
}
public class Dog implements Animal {
public void eat() {
// Code to make the dog eat
}
public void sleep() {
// Code to make the dog sleep
}
}
Animal myAnimal = new Dog();
myAnimal.eat();
In this example, the Animal interface defines the behavior of an animal, without specifying any implementation details. The Dog class implements the Animal interface, and provides its own implementation of the eat() and sleep() methods.
These are just a few of the key features of object-oriented programming in Java. By using these features, developers can create code that is more modular, easier to maintain, and more closely reflects the real world entities and concepts that the code is meant to represent.