Write an example to show the need of object interaction.


Q.) Write an example to show the need of object interaction.

Subject: Object Oriented Technology

I. Introduction

Object Oriented Technology (OOT) is a programming paradigm that uses "objects" – data structures consisting of data fields and methods together with their interactions – to design applications and computer programs. It is based on several techniques, including inheritance, modularity, polymorphism, and encapsulation. Object interaction is a fundamental aspect of OOT, as it allows objects to exchange information and delegate tasks, thereby facilitating complex behaviors and processes.

II. Definition of Object Interaction

In Object Oriented Technology, object interaction refers to the communication between objects, which occurs when one object sends a message to another object. The receiving object can then respond to the message, leading to a change in its state. This interaction is typically achieved through methods, which are procedures associated with a particular class of objects.

III. Need for Object Interaction

Object interaction is crucial in OOT for several reasons. Firstly, it enables encapsulation, which is the bundling of data and methods that operate on that data within one unit, i.e., the object. This encapsulation allows an object to hide its internal state and only expose its behavior, ensuring data integrity and security.

Secondly, object interaction facilitates inheritance, a mechanism that allows one class to inherit the properties and methods of another class. This inheritance promotes code reusability and a hierarchical classification.

Lastly, object interaction supports polymorphism, which allows one interface to be used for a general class of actions. This polymorphism enables objects to behave differently depending on their data types or classes.

IV. Example of Object Interaction

Consider a shopping cart system in an e-commerce application. This system could involve several objects, such as User, ShoppingCart, and Product.

The User object might have methods like login, logout, and purchase. The ShoppingCart object could have methods like addItem, removeItem, and calculateTotal. The Product object might have methods like getPrice and getDetails.

These objects interact with each other to provide the desired functionality. For instance, a User can add a Product to the ShoppingCart by calling the addItem method of the ShoppingCart object. The ShoppingCart object, in turn, interacts with the Product object to get the price of the product.

V. Diagram of Object Interaction

Drawing a diagram is necessary to visually represent the interaction between the objects. The diagram would show the User, ShoppingCart, and Product objects, along with the methods they expose and how they interact with each other.

VI. Code Example

Here is a simple Java program that demonstrates the object interaction in the given scenario:

class User {
    ShoppingCart cart = new ShoppingCart();

    void purchase(Product product) {
        cart.addItem(product);
    }
}

class ShoppingCart {
    List products = new ArrayList<>();

    void addItem(Product product) {
        products.add(product);
    }

    double calculateTotal() {
        double total = 0;
        for (Product product : products) {
            total += product.getPrice();
        }
        return total;
    }
}

class Product {
    double price;

    double getPrice() {
        return price;
    }
}

In this code, the User object interacts with the ShoppingCart object by calling its addItem method. The ShoppingCart object, in turn, interacts with the Product object by calling its getPrice method.

VII. Conclusion

In conclusion, object interaction is a fundamental aspect of Object Oriented Technology. It enables objects to exchange information and delegate tasks, thereby facilitating complex behaviors and processes. The shopping cart system example illustrates the need for object interaction, as the system wouldn't function without the interaction between the User, ShoppingCart, and Product objects.

Summary

Object interaction is a fundamental aspect of Object Oriented Technology (OOT) that allows objects to exchange information and delegate tasks. It enables encapsulation, inheritance, and polymorphism. An example of object interaction is a shopping cart system in an e-commerce application.

Analogy

Object interaction is like a conversation between different people. Each person has their own role and communicates with others to achieve a common goal.

Quizzes
Flashcards
Viva Question and Answers

Quizzes

What is object interaction?
  • The communication between objects in Object Oriented Technology
  • The process of creating objects in a programming language
  • The way objects store data and methods
  • The interaction between a user and a computer system