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.