Skip to main content
ubuntuask.com

Back to all posts

How to Call Parent Methods In Java/Groovy?

Published on
4 min read

Table of Contents

Show more
How to Call Parent Methods In Java/Groovy? image

In Java and Groovy, you can call a parent method from a child class by using the keyword super. To call a parent method, you need to use the super keyword followed by a dot (.) and the name of the method you want to call. This allows you to access and execute the parent class's method from the child class.

For example, in Java:

public class Parent { public void method() { System.out.println("Parent method"); } }

public class Child extends Parent { public void method() { super.method(); // calling parent method System.out.println("Child method"); } }

In this example, the Child class extends the Parent class and overrides the method() method. Inside the method() method of the Child class, we use super.method() to call the method() method of the parent class.

In Groovy, the same concept applies. You can use the super keyword to call a parent method in a child class.

class Parent { def method() { println "Parent method" } }

class Child extends Parent { def method() { super.method() // calling parent method println "Child method" } }

In both Java and Groovy, using the super keyword allows you to call and execute parent methods from a child class.

How to call methods of superclass in a subclass in Java?

In Java, you can call methods of a superclass in a subclass using the super keyword.

Here is an example:

class Animal { void eat() { System.out.println("Animal is eating"); } }

class Dog extends Animal { void eat() { super.eat(); // calling the eat method of the superclass Animal System.out.println("Dog is eating"); } }

public class Main { public static void main(String[] args) { Dog dog = new Dog(); dog.eat(); } }

In this example, the Dog class extends the Animal class. Inside the eat method of the Dog class, super.eat() is used to call the eat method of the superclass Animal. This allows you to execute the code of the superclass method before executing the code in the subclass method.

How to extend a superclass and call its methods in Java?

To extend a superclass and call its methods in Java, you need to create a subclass that inherits from the superclass and then use the super keyword to call the superclass methods. Here is an example:

// Superclass class Animal { public void makeSound() { System.out.println("Animal makes a sound"); } }

// Subclass extending superclass Animal class Dog extends Animal { public void makeSound() { // Call superclass method using super keyword super.makeSound(); System.out.println("Dog barks"); } }

public class Main { public static void main(String[] args) { Dog dog = new Dog(); dog.makeSound(); // Output: Animal makes a sound // Dog barks } }

In this example, we have a superclass Animal with a method makeSound(). We then create a subclass Dog that extends Animal and overrides the makeSound() method to call the superclass method using the super keyword before adding additional functionality.

When we create an instance of Dog and call the makeSound() method, both the superclass method and the subclass method are executed in order.

How to call a superclass method from a subclass in Java?

To call a superclass method from a subclass in Java, you can use the super keyword followed by the method you want to call. Here is an example:

public class Superclass { public void display() { System.out.println("Superclass method"); } }

public class Subclass extends Superclass { @Override public void display() { super.display(); // calling superclass method System.out.println("Subclass method"); } }

public class Main { public static void main(String[] args) { Subclass obj = new Subclass(); obj.display(); } }

In the above example, the display method in the Subclass class calls the display method of the Superclass using the super keyword. This allows you to access and execute the superclass method from the subclass.

What is the outcome of calling a superclass constructor in a subclass in Java?

Calling a superclass constructor in a subclass in Java allows the subclass to initialize the superclass fields. This means that the superclass constructor is executed before the subclass constructor when an object of the subclass is created. This ensures that the superclass fields are initialized properly before the subclass fields are initialized.