Lesson 29 of 34

Polymorphism, Abstract Classes এবং Interfaces

11 min 35 XP

Abstract class-এর object তৈরি করা যায় না; child classes-কে তার abstract methods লিখতে হয়। Interface একটি চুক্তি: যে class একে implement করে তাকে সব methods লিখতে হয়, এবং একটি class অনেক interfaces implement করতে পারে।

Example: edit me and press Run

Output Loading Java (first time takes a few seconds)
Press Run to see the result here.

Example: edit me and press Run

Output Loading Java (first time takes a few seconds)
Press Run to see the result here.

Shape s = new Circle(...) হলো runtime polymorphism: variable-এর type Shape কিন্তু Circle-এর area() চলে। Overriding runtime polymorphism; overloading compile-time।

Challenge

+35 XP

interface Discount { int apply(int price); } লিখুন। FlatDiscount 100 কমায় (0-এর নিচে নয়) এবং PercentDiscount 10% কমায় (int division); দুটোই একে implement করে। main()-এ একটি price পড়ে দুটি ফলাফলই print করুন। Input: 750 Output: Flat: 650 Percent: 675

Output Loading Java (first time takes a few seconds)
Press Run to see the result here.