Lesson 28 of 34

Inheritance: extends आणि super

10 min 35 XP

extends ने child class parent चे fields आणि methods inherit करतो. super(...) parent constructor call करतो. Child एखादी parent method override करून ती स्वतःच्या पद्धतीने करू शकतो.

Example: edit me and press Run

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

Java class फक्त एकाच class ला extend करू शकतो (classes चे multiple inheritance नाही). Interfaces हा त्यावरचा उपाय, पुढे येत आहेत.

Challenge

+35 XP

String name आणि "..." परत देणारी method String sound() असलेला class Animal लिहा. Dog आणि Cat, Animal ला extend करून sound() override करतील आणि "Woof" व "Meow" परत देतील. main() मध्ये एक type (dog/cat) आणि एक name वाचा, मग print करा: Input: dog Tommy Output: Tommy says Woof

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