E Commerce System Polymorphism
This program models polymorphism in an e commerce system.
Problem Statement
Write a Java program where Product is superclass and Electronics and Clothing override getDiscount().
Source Code
| 1 | class Product { void getDiscount(){ System.out.println("Eduinq Generic Discount"); } } |
| 2 | class Electronics extends Product { void getDiscount(){ System.out.println("Eduinq Electronics Discount Applied"); } } |
| 3 | class Clothing extends Product { void getDiscount(){ System.out.println("Eduinq Clothing Discount Applied"); } } |
| 4 | public class EcommercePolymorphism { |
| 5 | public static void main(String[] args) { |
| 6 | System.out.println("Eduinq E-Commerce Polymorphism"); |
| 7 | Product p1=new Electronics(); |
| 8 | Product p2=new Clothing(); |
| 9 | p1.getDiscount(); |
| 10 | p2.getDiscount(); |
| 11 | } |
| 12 | } |
Program Output
Eduinq E-Commerce Polymorphism Eduinq Electronics Discount Applied Eduinq Clothing Discount Applied
Explanation
Product defines a generic discount method. - Subclasses override the method to provide specific discount logic for electronics and clothing. - Polymorphism ensures correct discount calculation at runtime based on product type. - This models real world e commerce systems where different product categories require unique discount strategies, ensuring flexibility and scalability in large distributed applications. Conclusion: Shows real world polymorphism in e commerce system.