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.