Object-Oriented Programming (OOP) in Java: Exploring Concepts with Sample Code
Introduction:
Object-oriented programming (OOP) is a paradigm that allows developers to structure code around objects, which are instances of classes. Java is a powerful object-oriented programming language that fully embraces the principles of OOP. In this blog, we will explore key OOP concepts and demonstrate their implementation using sample code in Java.
1. Classes and Objects:
In Java, classes are the building blocks of objects. They define the structure and behavior of objects. Let's start by creating a simple class called "Car" with instance variables and methods:
public class Car {
// Instance variables
String brand;
String color;
// Methods
void startEngine() {
System.out.println("Engine started");
}
void stopEngine() {
System.out.println("Engine stopped");
}
}
2. Creating Objects:
Once we have a class, we can create objects from it. Let's create a few Car objects and invoke their methods:
public class Main {
public static void main(String[] args) {
// Creating objects
Car car1 = new Car();
Car car2 = new Car();
// Accessing instance variables
car1.brand = "Toyota";
car1.color = "Red";
car2.brand = "BMW";
car2.color = "Blue";
// Invoking methods
car1.startEngine(); // Output: Engine started
car2.startEngine(); // Output: Engine started
car1.stopEngine(); // Output: Engine stopped
}
}
3. Encapsulation:
Encapsulation is the process of hiding internal details of an object and providing a public interface for interaction. It is achieved through access modifiers. Let's modify the Car class to include private instance variables and public getter and setter methods:
public class Car {
private String brand;
private String color;
public String getBrand() {
return brand;
}
public void setBrand(String brand) {
this.brand = brand;
}
public String getColor() {
return color;
}
public void setColor(String color) {
this.color = color;
}
// Methods...
}
4. Inheritance:
Inheritance allows a class to inherit properties and methods from another class. Let's create a subclass called "SportsCar" that inherits from the Car class:
public class SportsCar extends Car {
void accelerate() {
System.out.println("Accelerating...");
}
}
Now, we can create a SportsCar object and invoke methods from both the Car and SportsCar classes:
public class Main {
public static void main(String[] args) {
SportsCar sportsCar = new SportsCar();
sportsCar.startEngine(); // Output: Engine started
sportsCar.accelerate(); // Output: Accelerating...
}
}
5. Polymorphism:
Polymorphism allows objects of different classes to be treated as objects of a common superclass. Let's modify our Car class and create a method that accepts Car objects:
public class Car {
// Instance variables...
void drive() {
System.out.println("Driving...");
}
}
public class Main {
public static void main(String[] args) {
Car car1 = new Car();
SportsCar car2 = new SportsCar();
driveCar(car1); // Output: Driving...
driveCar(car2); // Output: Driving...
}
static void driveCar(Car car) {
// Method body...
}
}
Guys I guess you are clear about the above topics. Let me know u need more topics on java spring
Comments
Post a Comment