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.