Inheritance
Understand how one Java class can inherit properties and methods from another class.
Java Code
Study the example and understand what each part does.
class Animal {
void sound() {
System.out.println("Animal makes a sound");
}
}
class Dog extends Animal {
void bark() {
System.out.println("Dog barks");
}
}
public class Main {
public static void main(String[] args) {
Dog dog = new Dog();
dog.sound();
dog.bark();
}
}
Java code normally runs using the Java Development Kit on a computer, Android Studio or another Java development environment. This course page presents the code examples without pretending that the browser is running Java directly.
Expected Output / Result
Animal makes a sound Dog barks
Finished this lesson?
Mark it complete to update your course progress.