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.