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.