OOP real world example

0 votes

I have a simple shopping cart model with products that are based on productID, productName, Price, and Stock columns from a database. I then have a productID,ProductName,Price item class that represents the item a customer decides to order. The itemOrder class then refers to the item via productID, ProductName, Price, and quantity via a getPrice method (). The itemOrders are then stored in a Cart class, which contains methods for adding and deleting itemOrders.

Where do OOP principles come into play in any of these classes or in a conventional shopping cart system?

public class Product
{
    public int productID { get; set; }
    public string productName { get; set; }
    ...
 }
}

public class Item : Product
{

}

public class ItemOrder : Item
{
    public int itemQuantity { get; set; }
    public double getPrice(int quantity, double price)
    {
        return price = price * quantity;
    }

}
public class Cart
{

}
Jun 11, 2022 in C# by jyoti
• 1,240 points
754 views

1 answer to this question.

0 votes
This is what I believe.

OOP does not require inheritance. You have OOP if each of your classes effectively performs a single task while concealing any internal specifics from the outside world, providing a constant interface that clients cannot exploit to destroy the objects.

There's this principle called Liskov Substitution Principle and you should use inheritance if the relationship between two classes meets this principle. Read more about it here:https://en.wikipedia.org/wiki/Liskov_substitution_principle

Each of the three terms- item, product, and order are distinct and cannot be used in place of the others. All three classes must exist, but they must not share any common ancestors. Instead of using inheritance, Carts, Items, and Orders can refer to Products and to one another through references or confinement.
answered Jul 4, 2022 by krishna
• 2,820 points

Related Questions In C#

0 votes
1 answer

Encapsulation vs abstraction real world example

Encapsulation is a technique for "information concealment," ...READ MORE

answered Jun 21, 2022 in C# by jyoti
• 1,240 points
12,640 views
0 votes
0 answers

Use of interfaces, practical and real world example

I've been attempting to figure out what ...READ MORE

Jun 11, 2022 in C# by krishna
• 2,820 points
879 views
0 votes
1 answer

What are the differences between C, C# and C++ in terms of real-world applications?

C is a bare-bones, straightforward, and clean ...READ MORE

answered May 30, 2022 in C# by rajiv
• 1,620 points
327 views
0 votes
0 answers

C# Threading in real-world apps

Learning about threading is indeed fascinating, and ...READ MORE

Jun 11, 2022 in C# by pranav
• 2,590 points
260 views
0 votes
0 answers

Abstract Class:-Real Time Example

In a recent interview, I was asked ...READ MORE

Jun 11, 2022 in C# by pranav
• 2,590 points
320 views
0 votes
0 answers

What are the differences between C, C# and C++ in terms of real-world applications?

As the title states, what are the ...READ MORE

Sep 26, 2022 in C# by krishna
• 2,820 points
178 views
0 votes
1 answer

Object Oriented Programming Basic Concept(C#)

Use this link https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/classes-and-structs/access-modifiers protected internal: The type or ...READ MORE

answered Jun 17, 2022 in C# by jyoti
• 1,240 points
410 views
0 votes
0 answers

Object Oriented Programming Basic Concept(C#)

As We know The Default Modifier of Class, Struct, ...READ MORE

Dec 9, 2022 in Android by Edureka
• 13,620 points
270 views
0 votes
0 answers

Object Oriented Programming Basic Concept(C#)

We are aware that the default modifier ...READ MORE

Dec 12, 2022 in C# by Roshan
• 300 points
264 views
0 votes
1 answer

Python class inherits object

Python 3.x: class MyClass(object): = new-style class class MyClass: = new-style ...READ MORE

answered Aug 30, 2018 in Python by Priyaj
• 58,090 points
599 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP