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-ഉം "..." തിരികെ നൽകുന്ന String sound() method-ഉം ഉള്ള 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.