Lesson 29 of 34

Polymorphism, Abstract Classes, Interfaces

11 min 35 XP

Abstract class-ന്റെ objects ഉണ്ടാക്കാനാവില്ല; child classes അതിന്റെ abstract methods എഴുതണം. Interface ഒരു കരാറാണ്: അത് implement ചെയ്യുന്ന class അതിന്റെ എല്ലാ 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.