OOPs Concepts in Java with Examples
What are OOPs in Java?
- Object-Oriented Programming (OOP) in Java is a programming paradigm that organizes code around objects, classes, and interactions.
- OOP comprises several principles such as abstraction, encapsulation, inheritance, and polymorphism.
OOPs concepts in Java
- Objects
- Classes
- Abstraction
- Encapsulation
- Inheritance
- Polymorphism
Objects
- In Java, everything is an object. An object represents a real-world entity and can have attributes (characteristics) and methods (actions).
- Objects are instances of classes, which serve as blueprints for creating objects.
Classes
- To work with objects, you must create a class that defines the blueprint for those objects.
- Let's create a simple class called Car to represent a car:
public class Car {
// Attributes (Characteristics)
String color;
String model;
// Methods (Actions)
public void start() {
System.out.println("The car is starting.");
}
public void stop() {
System.out.println("The car is stopping.");
}
}
In this class, we have attributes (color and model) and methods (start() and stop()) that a car can have.
Creating an Object
Now that we have our class, we can create an object based on it. In Java, we use the new keyword to create an instance (object) of a class. For example:
Car myCar = new Car();
Here, myCar is an object of the Car class. It's created based on the Car blueprint.
Setting Object Attributes
You can set the attributes of an object using dot notation. For instance, to set the color and model of myCar:
myCar.color = "black";
myCar.model = "Toyota";
Using Object Methods
You can call the methods of an object using dot notation as well. For example, to start and stop the car:
myCar.start();
myCar.stop();
Here's the complete Java Code combining all the steps:
public class Car {
// Attributes (Characteristics)
String color;
String model;
// Methods (Actions)
public void start() {
System.out.println("The car is starting.");
}
public void stop() {
System.out.println("The car is stopping.");
}
public static void main(String[] args) {
// Creating an instance (object) of the Car class
Car myCar = new Car();
// Setting attributes for the car
myCar.color = "blue";
myCar.model = "Toyota";
// Using methods to perform actions
myCar.start();
myCar.stop();
}
}
Abstraction in Java
- Abstraction involves showing only the necessary features of an object while hiding the unnecessary details.
- Think of a TV remote control - you don't need to know how it works internally. You just need to know the buttons to operate it.
- In Java, you achieve abstraction using abstract classes and interfaces.
Encapsulation in Java
- Encapsulation is the practice of hiding the internal state of an object and providing controlled access to it.
- It's like putting your data in a protective capsule.
- You use private variables and public methods to achieve encapsulation.
Inheritance in Java
- Inheritance is a concept that allows you to create new classes based on existing classes.
- It allows one class to inherit properties and behaviors from another class.
- It facilitates code reusability.
Polymorphism in Java
- Polymorphism in Java means one name, many forms.
- For example, both a "Car" and a "SUV" can be referred to as a "Vehicle" if they are inherited from a common class.
- Polymorphism in Java can be classified into two primary types compiler-time (or static) polymorphism and runtime (or dynamic) polymorphism. Let's explore these two types:
Compile-time Polymorphism in Java
- Static time polymorphism is also known as method overloading.
- It occurs when multiple methods are in the same class with the same name but different parameters (method signature).
- The correct method to be executed is determined at compile time-based on the method's parameters.
Runtime Polymorphism in Java
- Dynamic time polymorphism is also known as method overriding.
- It occurs when a subclass offers a unique implementation of a method already in its superclass.
Advantages of OOPs
Reusability
- OOP encourages code reusability through inheritance and composition.
- You can create new classes by extending or combining existing ones, reducing redundant code and development time.
Maintenance and Debugging
- OOP makes code maintenance and debugging more straightforward.
- Classes and objects encapsulate functionality, making locating and fixing issues within specific components easier without affecting the entire application.
Scalability
- OOP supports the development of scalable systems.
- As the component of a project grows, OOP principles help manage that complexity by breaking it down into manageable, interconnected components.
Conclusion
We simplified Java OOPs concepts: objects, classes, inheritance, polymorphism, abstraction, and encapsulation, with real-world examples.