Oops Concepts In Java With Realtime Examples

OOPS Concept In Java

OOP (Object Oriented Programming) is a programming paradigm in which programs are organized around objects instead of actions and data rather than logic. It is a way of defining objects and classes that are used to represent real world objects.

Java is an object-oriented language and supports OOP concepts such as inheritance, polymorphism, encapsulation, and abstraction.

Inheritance: It is the mechanism by which one object can inherit the properties and behaviors of another object. In Java, inheritance is achieved through the use of the keyword “extends“.

Here is an example of inheritance in Java:

class Animal {
String name;
int age;

public void makeSound() {
System.out.println("The animal makes a sound");
}
}

class Dog extends Animal {
String breed;

public void bark() {
System.out.println("The dog barks");
}
}

public class Main {
public static void main(String[] args) {
Dog dog = new Dog();
dog.name = "Fido";
dog.age = 3;
dog.breed = "Labrador";
dog.makeSound();
dog.bark();
}
}

In this example, we have two classes: Animal and Dog. Dog extends Animal, meaning it inherits all the properties and methods of Animal. Additionally, Dog has its own property (breed) and method (bark()). When we create an instance of Dog, it has access to both the properties and methods of Animal and Dog.

Inheritance in Java is a mechanism that allows a new class to inherit properties and behavior from an existing class. This concept is similar to a parent-child relationship, where the parent class (super class) passes down its properties to the child class (subclass).

For example, consider a real-life scenario of a car. A car is a general category and can have several subclasses such as a sports car, sedan, or SUV. Each of these subclasses inherits properties such as wheels, engine, and doors from the car class, but they also have unique properties specific to their class.

Similarly, in Java, the car class can have properties such as numberOfWheels, engineSize, and numberOfDoors, which are inherited by the subclasses of sports car, sedan, and SUV. The subclasses can also have their own unique properties, such as topSpeed for the sports car and seatingCapacity for the sedan.

In summary, inheritance in Java allows for reusability of code and makes it easier to maintain and update code as the properties and behaviors of the parent class can be easily passed down to the child class.

Polymorphism: It is the ability of an object to take on many forms. Polymorphism in Java is achieved through method overloading and method overriding.

Here is an example of polymorphism in Java:

class Animal {
public void makeSound() {
System.out.println("The animal makes a sound");
}
}

class Dog extends Animal {
public void makeSound() {
System.out.println("The dog barks");
}
}

class Cat extends Animal {
public void makeSound() {
System.out.println("The cat meows");
}
}

public class PolymorphismExample {
public static void main(String[] args) {
Animal animal = new Animal();
Animal dog = new Dog();
Animal cat = new Cat();

    animal.makeSound();
    dog.makeSound();
    cat.makeSound();
}
}

The output will be:

The animal makes a sound
The dog barks
The cat meows

In this example, the class Animal has a method makeSound(). The classes Dog and Cat extend the Animal class and each have their own implementation of the makeSound() method.

This allows us to create objects of type Animal, Dog, and Cat and call the makeSound() method on each, but still get different outputs, as the method is being overridden in the subclasses. This is polymorphism.

An example of polymorphism in real life is the use of USB ports. USB ports are used to connect various devices such as flash drives, printers, smartphones, and many more. All these devices use the same USB port, but perform different functions. The USB port is a form of polymorphism because it can recognize and interact with different devices in the same way, even though each device has its own unique set of functions and capabilities. This allows for versatility and ease of use in connecting various devices to a computer or other device.

Encapsulation: It is the process of hiding the implementation details of a class from the rest of the world. In Java, encapsulation is achieved by using access modifiers such as private and protected.

Here is an example of encapsulation in Java:

public class Employee {

private int id;
private String name;
private String department;
private double salary;

//Getters and Setters
public int getId() {
return id;
}

public void setId(int id) {
this.id = id;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public String getDepartment() {
return department;
}

public void setDepartment(String department) {
this.department = department;
}

public double getSalary() {
return salary;
}

public void setSalary(double salary) {
this.salary = salary;
}
}

In the above example, the class Employee has four instance variables id, name, department, and salary which are defined as private. This means that these variables can only be accessed within the class itself. To access these variables from outside the class, getter and setter methods are used.

Getters are used to retrieve the values of instance variables and setters are used to set the values of instance variables. This way, the internal representation of the object is hidden from the outside world, providing data protection and making it easier to maintain the code.

Encapsulation can be seen in real life in the design of a car engine. The engine is made up of several parts such as the fuel injectors, spark plugs, pistons, and valves that all work together to create movement and power. Each part is encapsulated or enclosed within the engine, ensuring that it works properly and does not interfere with other parts. This also makes it easier to maintain and repair the engine, as each part can be replaced or fixed without affecting the rest of the engine. The encapsulation of the engine parts protects the overall functionality of the car and ensures its smooth operation.

Abstraction: It is the ability of an object to hide its complexity and present a simple interface to the rest of the world. In Java, abstraction is achieved through the use of abstract classes and interfaces.

Abstraction in Java refers to hiding the implementation details of a class and showing only the essential information to the users. Here’s an example:

public abstract class Shape {
// Abstract method
public abstract void draw();

// Non-abstract method
public void fillColor(String color) {
System.out.println("Filling color " + color + " to the shape");
}
}

public class Circle extends Shape {
@Override
public void draw() {
System.out.println("Drawing a circle");
}
}

public class Rectangle extends Shape {
@Override
public void draw() {
System.out.println("Drawing a rectangle");
}
}

public class AbstractionExample {
public static void main(String[] args) {
Shape circle = new Circle();
Shape rectangle = new Rectangle();

circle.draw();
circle.fillColor("red");

rectangle.draw();
rectangle.fillColor("blue");

}
}

Output:

// Drawing a circle
// Filling color red to the shape
// Drawing a rectangle
// Filling color blue to the shape

In this example, the Shape class is an abstract class that defines two methods – an abstract method draw() and a non-abstract method fillColor(). The Circle and Rectangle classes extend the Shape class and implement the abstract method draw(). The user can use the draw() and fillColor() methods of the Circle and Rectangle classes without knowing the implementation details.

An example of abstraction in real life is a computer mouse. A mouse is an abstraction of a human hand as it allows users to control their computer by clicking and dragging. The mouse does not represent the entire hand, but rather abstracts the functions of pointing and clicking into a small device that can be used to control a computer. Another example is a smartphone, which abstracts a variety of different devices and functions into one compact device.

Leave a Reply

Your email address will not be published. Required fields are marked *

*