- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram20_Product.java
More file actions
Latest commit
83 lines (67 loc) · 2.23 KB
/
Copy pathProgram20_Product.java
File metadata and controls
83 lines (67 loc) · 2.23 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
/* Develop a simple online shopping system.
Create a base class Product with attributes productId, productName, and price.
Derive classes Electronics, Clothing, and Books from Product. Add specific attributes for each product type.
Create objects for each class and demonstrate polymorphism by displaying product details. */
// Date : 11/01/2024, Author : Yash Wadhvani
classProduct {
intproductID;
StringproductName;
doubleprice;
Product(intproductID, StringproductName, doubleprice) {
this.productID = productID;
this.productName = productName;
this.price = price;
}
voiddisplayInfo() {
System.out.println("\n");
System.out.println("Product ID = " + productID);
System.out.println("Product Name = " + productName);
System.out.println(String.format("Product Price = %.2f", price));
}
}
classElectronicsextendsProduct {
Stringbrand;
Electronics(intproductID, StringproductName, doubleprice, Stringbrand) {
super(productID, productName, price);
this.brand = brand;
}
@Override
voiddisplayInfo() {
super.displayInfo();
System.out.println("Product Brand = " + brand);
}
}
classClothingextendsProduct {
Stringsize;
Clothing(intproductID, StringproductName, doubleprice, Stringsize) {
super(productID, productName, price);
this.size = size;
}
@Override
voiddisplayInfo() {
super.displayInfo();
System.out.println("Product Size = " + size);
}
}
classBooksextendsProduct {
Stringauthor;
Books(intproductID, StringproductName, doubleprice, Stringauthor) {
super(productID, productName, price);
this.author = author;
}
@Override
voiddisplayInfo() {
super.displayInfo();
System.out.println("Author = " + author);
}
}
publicclassProgram20_Product {
publicstaticvoidmain(String[] args) {
ClothingTee = newClothing(1, "T Shirt", 999, "S");
Tee.displayInfo();
ElectronicsLaptop = newElectronics(2, "Laptop", 89990, "HP");
Laptop.displayInfo();
BooksTLove = newBooks(3, "Twisted Love", 300, "Ana Huang");
TLove.displayInfo();
}
}