I am new to the Java language and I am learning about the super
keyword.
W3Schools says that "[In Java], The most common use of the super keyword is to eliminate the confusion between superclasses and subclasses that have methods with the same name."
What would be the reason to name them the same? What are other uses for the super keyword?
class Animal { // Superclass (parent)
public void animalSound() {
System.out.println("The animal makes a sound");
}
}
class Dog extends Animal { // Subclass (child)
public void animalSound() {
super.animalSound(); // Call the superclass method
System.out.println("The dog says: bow wow");
}
}
public class Main {
public static void main(String args[]) {
Animal myDog = new Dog(); // Create a Dog object
myDog.animalSound(); // Call the method on the Dog object
}
}
I am hoping to learn all the uses of the super
keyword in Java.