Lesson 29 of 34

Polymorphism, Abstract Classes, InterfacesPolymorphism, Abstract Classes and Interfaces

11 min 35 XP

Abstract class-லிருந்து நேரடியாக object உருவாக்க முடியாது; அதன் abstract methods-ஐ child classes எழுத வேண்டும். Interface என்பது ஒப்பந்தம்: implements செய்யும் class அதன் எல்லா methods-ஐயும் எழுத வேண்டும். ஒரு class பல interfaces-ஐ implement செய்யலாம்.

You can't create objects of an abstract class; child classes must write its abstract methods. An interface is a contract: a class that implements it must write all its methods, and a class can implement many interfaces.

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, overloading = compile-time.

Shape s = new Circle(...) is runtime polymorphism: the variable's type is Shape but Circle's area() runs. Overriding is runtime polymorphism; overloading is 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

Write interface Discount { int apply(int price); }. FlatDiscount takes 100 off (never below 0) and PercentDiscount takes 10% off (int division); both implement it. In main(), read a price and print both results. Input: 750 Output: Flat: 650 Percent: 675

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