top of page
hand-businesswoman-touching-hand-artificial-intelligence-meaning-technology-connection-go-

Concept of OOP in Java:

OOP or Object Oriented Programming is a very important concept in Java as it uses Objects to design applications and Computer programs. It simplifies different software development and maintenance by providing some concepts


There are four different OOPs concept in Java which are:

  1. Abstraction

  2. Encapsulation

  3. Inheritance

  4. Polymorphism 

i. Abstraction: It is the process of hiding the implementation details from the user ,only necessary functionalities will be provided to the user. In UI automation this an be achieved using Page Object Model(POM) design pattern where each web page will have their own methods and classes and test script can use the methods but we can’t see the implementation by this we hide implementation details in our main test script. 

//Abstract class

public abstract class SBI {

public abstract void carLoan();

public abstract void homeLoan();

public abstract void educationLoan();

}


// US Bank inherits from the SBI class and implements the methods

public class USBank extends SBI {

@Override

public void carLoan() {

System.out.println("Display the carLoan");

}


@Override

public void homeLoan() {

System.out.println("Display the homeLoan");

}


@Override

public void educationLoan() {

System.out.println("Display the educationLoan");

}

}


// Main class calling the methods

public class Main {

public static void main(String args[]) {

SBI myBank = new USBank();

myBank.carLoan();

myBank.homeLoan();

myBank.educationLoan();

}

}



ii.Encapsulation: Encapsulation is one of the OOPs concept which means binding the fields and methods together. So in a class all the instance variables are declared as private and to allow outside access to the instance variables, public methods called getters and setters are defined, which are used to retrieve and modify the values of the instance variables, respectively. Encapsulation is also called data hiding. Here is the sample code to understand the how encapsulation works:


public class EmployeeData {

private String empName;

private int empAge;

private int empId;


// Getter and Setter methods

public String getEmpName() {

return empName;

}


public void setEmpName(String empName) {

this.empName = empName;

}


public int getEmpAge() {

return empAge;

}


public void setEmpAge(int empAge) {

this.empAge = empAge;

}


public int getEmpId() {

return empId;

}


public void setEmpId(int empId) {

this.empId = empId;

}

}


public class Main {

public static void main(String args[]) {

EmployeeData emp = new EmployeeData();

emp.setEmpName("Steven Cheng");

emp.setEmpAge(30);

emp.setEmpId(2482);


System.out.println("Employee name is: " + emp.getEmpName());

System.out.println("Employee Age is: " + emp.getEmpAge());

System.out.println("Employee Id is: " + emp.getEmpId());

}

}


iii.Polymorphism: Polymorphism is a core concept in object-oriented programming that allows objects to be treated as instances of their parent class rather than their actual class. This can be achieved through method overriding (runtime polymorphism) and method overloading (compile-time polymorphism).

There are two types of Polymorphism:

-Compile -time Polymorphism :It is also know n as Method Overloading with multiple methods with the same name but different parameters

-Run -time Polymorphism: Also known as Method Overriding when a subclass provides specific implementation of a method that is already defined in the superclass


Example of Method Overloading:


public class Calculator {

// Method to add two integers

public int add(int a, int b) {

return a + b;

}


// Overloaded method to add three integers

public int add(int a, int b, int c) {

return a + b + c;

}


// Overloaded method to add two double values

public double add(double a, double b) {

return a + b;

}


public static void main(String[] args) {

Calculator calculator = new Calculator();


// Using the add method with two integers

System.out.println("Sum of 10 and 20: " + calculator.add(10, 20));


// Using the add method with three integers

System.out.println("Sum of 10, 20, and 30: " + calculator.add(10, 20, 30));


// Using the add method with two doubles

System.out.println("Sum of 10.5 and 20.5: " + calculator.add(10.5, 20.5));

}

}


Example of Method Overriding:


class ParentClass {

public void displayInfo() {

System.out.println("I am from ParentClass");

}

}


class ChildClass extends ParentClass {

@Override

public void displayInfo() {

System.out.println("I am from ChildClass");

}

}


public class Main {

public static void main(String[] args) {

// Create an Object of ChildClass

ChildClass objFirst = new ChildClass();

objFirst.displayInfo();


// Create an Object of ParentClass

ParentClass objSecond = new ParentClass();

objSecond.displayInfo();

}

}

iv. Inheritance: Inheritance is an concept of OOPs where it allows the Child Class to inherit properties and behaviors (methods) from another class (known as a parent class or superclass).Inheritance are of three types:

-Single Level Inheritance: A Class inheriting from its Super Class

-Multi-level Inheritance: A class inheriting from another subclass forming a chain

-Hierarchical Inheritance: Multiple class inherits from one Superclass

Let’s see an example of a Single Level Inheritance:



// Parent class

class Dog {

// Method in the parent class

public void pet() {

System.out.println("Dog is a pet");

}

}


// Child class inheriting from the parent class

class Doberman extends Dog {

// Method in the child class

public void breed() {

System.out.println("Doberman is a breed of Dog");

}

}


// Main class to demonstrate inheritance

public class SinglelevelInheritance {

public static void main(String[] args) {

// Creating an instance of the child class

Doberman myDog = new Doberman();


// Calling the method from the parent class

myDog.pet();


// Calling the method from the child class

myDog.breed();

}

}



24 views0 comments

Comments

Rated 0 out of 5 stars.
No ratings yet

Add a rating
bottom of page