Java OOPs Concept
What is oops in java?
Java uses the term OOPS, or object-oriented programming system. It is a programming approach that is based on the concepts of classes and objects. Java helps developers put OOP principles into effect and is designed to be an object-oriented language.
The main goal of an object-oriented strategy is to better reflect real entities by representing them as objects having state (attributes/fields) and behaviour (methods). Both messages and actions can be called between objects.
Concept of OOPs
There are some concept of oops that are present in java:
Classes and object
A class in Java is a blueprint or template that specifies the attributes, fields, and methods that will be present in instances of that class. It acts as a guide for building things.
On the other hand, objects are instance of classes that stand in for distinct things with their own state and behaviour.
For example:
Lets take an example to understand the class and object in java: // Define a class called "Car" class Car { // Define attributes String brand; String color; int year; // Define a method void drive() { System.out.println("The " + brand + " car is driving."); } } // Main class public class Main { public static void main(String[] args) { // Create objects of the "Car" class Car car1 = new Car(); Car car2 = new Car(); // Set object attributes car1.brand = "Toyota"; car1.color = "Red"; car1.year = 2020; car2.brand = "BMW"; car2.color = "Blue"; car2.year = 2022; // Call object methods car1.drive(); // Output: The Toyota car is driving. car2.drive(); // Output: The BMW car is driving. } }
Output:
The Toyota car is driving. The BMW car is driving.
Encapsulation
In java or object oriented programming the idea of merging data and behaviour into a single unit is known as Encapsulation. It provide a clear interface for dealing with the class while it shield user from the implementation specifics of the class.
In java, encapsulation is accomplished in java by controlling the visibility of a class data and methods with access modifier like public, private and protected.
Overall, encapsulation is an important concept in java that helps to improve the robustness, maintainability, and security of our code by hiding the implementation detail.
For example
Let’s take a program to understand implementation of encapsulation in java:
public class Main { public static void main(String[] args) { Encapsulate emp = new Encapsulate(); emp.setName("Rohit"); emp.setAge(19); emp.setSalary(40000); // Insted of direct calling variables we are calling getter and setters System.out.println("Name :"+emp.getName()); System.out.println("Age :"+emp.getAge()); System.out.println("Salary :"+emp.getSalary()); } } class Encapsulate { private String name; private int age; private int salary; public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public int getSalary() { return salary; } public void setSalary(int salary) { this.salary = salary; } }
Output:
Name: Rohit Age: 19 Salary: 40000
Inheritance
A key concept in object oriented programming is inheritance that helps us to enable a class to inherit properties from another class. With the use of inheritance classes in java can be arranged in a hierarchical structure, with one class inheriting the fields and method of another class.
Because the subclass extends the superclass, it has access to and can utilise all of the superclass' non-private members. This encourages code reuse and contributes to the development of a more modular and structured codebase.
For example:
// Superclass class Animal { String name; void eat() { System.out.println(name + " is eating."); } } // Subclass class Dog extends Animal { void bark() { System.out.println(name + " is barking."); } } // Main class public class Main { public static void main(String[] args) { Dog dog = new Dog(); dog.name = "Max"; dog.eat(); // Output: Max is eating. dog.bark(); // Output: Max is barking. } }
Output:
Max is eating. Max is barking.
Abstraction
Abstraction is a programming concept used in Java that enables you to build sophisticated systems by keeping implementation details hidden from the user and just exposing the functionality that is required. By developing abstract classes or interfaces that specify the overall behaviour of a group of related classes, it is a method for controlling complexity.
To specify common behaviours and traits that can be inherited or implemented by other classes, abstract classes and interfaces are created in Java. It enables you to design a shared interface without mentioning the specifics of implementation for a collection of classes.
One of the guiding principles of object-oriented programming (OOP) is abstraction, which is employed to accomplish modularity, encapsulation, and code reuse. You can concentrate on the key characteristics and actions of a single object or a collection of related objects by abstracting away the implementation specifics.
For example:
//Abstract class abstract class Animal { // Abstract method public abstract void makeSound(); } //Subclass of the abstract class class Dog extends Animal { public void makeSound() { System.out.println("Woof!"); } } //Another subclass of the abstract class class Cat extends Animal { public void makeSound() { System.out.println("Meow!"); } } //Main class to demonstrate the usage of the abstract class public class Main { public static void main(String[] args) { Animal dog = new Dog(); dog.makeSound(); // Output: Woof! Animal cat = new Cat(); cat.makeSound(); // Output: Meow! } }
Explanation:
Basically, in this program Animal is a abstract class that defines an abstract method makeSound (), Dog and Cat are two subclasses of Animal that implement makeSound () method.
And in the main class we create instances of Dog and Cat and call their makeSound()
Methods. Since Dog and Cat are subclasses of Animal we can use an Animal reference to point to either a Dog or a Cat object.
Output:
Woof! Meow!
Polymorphism
In java polymorphism is an important concept that help in object oriented programming it allow us to treat the different object of classes as a common object of superclass. With the help of polymorphism we can work with various types of object and it will provide us flexibility and extensibility.
Java implements polymorphism primarily through method overloading and overriding.
Method overriding:
In java, method overriding means when a subclass allow us to implement our on method and the method definition from superclass is overridden. In method overriding the name, return type and parameter of superclass must match with the method in the subclass. With the use of the @Override keyword e can allow the complier to verify that the overriding was done correctly.
Example:
class Animal { void makeSound() { System.out.println("Generic animal sound"); } } class Dog extends Animal { @Override void makeSound() { System.out.println("Woof woof!"); } } class Cat extends Animal { @Override void makeSound() { System.out.println("Meow!"); } } public class Main { public static void main(String[] args) { Animal animal1 = new Dog(); Animal animal2 = new Cat(); animal1.makeSound(); // Output: Woof woof! animal2.makeSound(); // Output: Meow! } }
Output:
Woof! Woof! Meow!
Method overloading:
When multiple methods in the same class have the same name but different parameter lists, this is known as method overloading. Based on the supplied arguments, the compiler chooses the right method to call.
Example:
class Calculator { int add(int a, int b) { return a + b; } double add(double a, double b) { return a + b; } } public class Main { public static void main(String[] args) { Calculator calculator = new Calculator(); int sum1 = calculator.add(2, 3); // Output: 5 double sum2 = calculator.add(2.5, 3.5); // Output: 6.0 } }
Output:
5 6.0