Java Interview Questions
Java
Web DevelopmentBackendQuestion 2
Explain the concept of Object-Oriented Programming.
Answer:
Object-Oriented Programming (OOP) is a programming paradigm that uses "objects" to design applications and computer programs. Hereβs an explanation of the concept with examples in Java:
Key Concepts of Object-Oriented Programming
-
Class:
- A class is a blueprint for objects. It defines a datatype by bundling data and methods that work on the data into one single unit.
- Example:
public class Car { // Fields private String color; private String model; // Constructor public Car(String color, String model) { this.color = color; this.model = model; } // Methods public void displayDetails() { System.out.println("Car Model: " + model + ", Color: " + color); } }
-
Object:
- An object is an instance of a class. It contains actual data and can perform tasks defined by its class.
- Example:
public class Main { public static void main(String[] args) { Car car1 = new Car("Red", "Toyota"); car1.displayDetails(); // Output: Car Model: Toyota, Color: Red } }
-
Inheritance:
- Inheritance is a mechanism where one class inherits the fields and methods of another class. It provides a way to reuse code.
- Example:
public class ElectricCar extends Car { private int batteryLife; public ElectricCar(String color, String model, int batteryLife) { super(color, model); this.batteryLife = batteryLife; } public void displayBatteryLife() { System.out.println("Battery Life: " + batteryLife + " hours"); } } public class Main { public static void main(String[] args) { ElectricCar eCar = new ElectricCar("Blue", "Tesla", 24); eCar.displayDetails(); // Output: Car Model: Tesla, Color: Blue eCar.displayBatteryLife(); // Output: Battery Life: 24 hours } }
-
Polymorphism:
- Polymorphism allows objects to be treated as instances of their parent class rather than their actual class. It can be achieved through method overriding and method overloading.
- Example:
public class Car { public void startEngine() { System.out.println("Car engine started"); } } public class ElectricCar extends Car { @Override public void startEngine() { System.out.println("Electric car engine started"); } } public class Main { public static void main(String[] args) { Car myCar = new ElectricCar(); myCar.startEngine(); // Output: Electric car engine started } }
-
Encapsulation:
- Encapsulation is the technique of wrapping data (variables) and code (methods) together as a single unit. It restricts direct access to some of the objectβs components.
- Example:
public class Car { private String color; public String getColor() { return color; } public void setColor(String color) { this.color = color; } } public class Main { public static void main(String[] args) { Car car = new Car(); car.setColor("Green"); System.out.println(car.getColor()); // Output: Green } }
-
Abstraction:
- Abstraction is the concept of hiding the complex implementation details and showing only the necessary features of an object.
- Example:
abstract class Car { abstract void startEngine(); public void displayInfo() { System.out.println("This is a car"); } } public class ElectricCar extends Car { @Override void startEngine() { System.out.println("Electric car engine started"); } } public class Main { public static void main(String[] args) { Car myCar = new ElectricCar(); myCar.displayInfo(); // Output: This is a car myCar.startEngine(); // Output: Electric car engine started } }
Conclusion
OOP is a powerful paradigm that makes it easier to manage and organize code by creating objects that represent real-world entities. Java, being an object-oriented language, leverages these concepts to provide a robust structure for developing software applications.