C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML
And: The Cat scratch method itself calls its parent scratch method from the Animal class. We call a "super" class method.
Java program that uses super
class Animal {
public void scratch() {
System.out.println("Animal scratched");
}
}
class Cat extends Animal {
public void scratch() {
// Call super-class method.
super.scratch();
System.out.println("Cat scratched");
}
}
public class Program {
public static void main(String[] args) {
// Create cat and call method.
Cat cat = new Cat();
cat.scratch();
}
}
Output
Animal scratched
Cat scratched
Java program that uses super, Object
class Page {
public void test() {
// This class inherits from Object only.
// ... So it gains access to methods like hashCode on Object.
System.out.println(super.hashCode());
}
}
public class Program {
public static void main(String[] args) {
Page page = new Page();
page.test();
}
}
Output
366712642
Here: The Image class extends the Data class. We invoke the Data constructor from the Image constructor with super().
Java program that uses super constructor
class Data {
public Data() {
System.out.println("Data constructor");
}
}
class Image extends Data {
public Image() {
// Call the superclass constructor.
super();
}
}
public class Program {
public static void main(String[] args) {
Image value = new Image();
System.out.println("Done");
}
}
Output
Data constructor
Done