How to Call Parent Methods In Java/Groovy?

8 minutes read

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:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
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.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
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.

Best Groovy Books to Read in October 2024

1
Groovy Programming

Rating is 5 out of 5

Groovy Programming

2
Groovy in Action: Covers Groovy 2.4

Rating is 4.9 out of 5

Groovy in Action: Covers Groovy 2.4

3
Programming Groovy: Dynamic Productivity for the Java Developer (Pragmatic Programmers)

Rating is 4.8 out of 5

Programming Groovy: Dynamic Productivity for the Java Developer (Pragmatic Programmers)

4
Groovy Programming: An Introduction for Java Developers

Rating is 4.7 out of 5

Groovy Programming: An Introduction for Java Developers

5
Groovy Recipes: Greasing the Wheels of Java (Pragmatic Programmers)

Rating is 4.6 out of 5

Groovy Recipes: Greasing the Wheels of Java (Pragmatic Programmers)

6
Programming Groovy 2: Dynamic Productivity for the Java Developer (Pragmatic Programmers)

Rating is 4.5 out of 5

Programming Groovy 2: Dynamic Productivity for the Java Developer (Pragmatic Programmers)

7
Mastering GROOVY: A Comprehensive Guide To Learn Groovy Programming

Rating is 4.4 out of 5

Mastering GROOVY: A Comprehensive Guide To Learn Groovy Programming


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:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
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:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
// 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:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
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.

Facebook Twitter LinkedIn Telegram Whatsapp Pocket

Related Posts:

To call a groovy method using the command line, you can use the groovy command followed by the name of the Groovy script and the method you want to call. For example, if you have a Groovy script named MyScript.groovy with a method named myMethod, you can call ...
To correctly query parent and child documents in Solr, you need to use the Block Join Query Parser provided by Solr. This parser allows you to work with parent and child documents, which are documents that have a hierarchical relationship.To query parent docum...
Groovy GDK (Groovy Development Kit) provides a set of methods that can be used to enhance and simplify the coding experience in Groovy. These methods are built-in extensions to the existing classes and allow for more concise and readable code. To use GDK metho...
In Kotlin, you can call a static method of a parent class using the child class by simply referencing the parent class name followed by a dot and the method name. This is because static methods are not inherited in Kotlin, so you can directly access them using...
To use a plugin inside a Groovy plugin, you first need to ensure that the desired plugin is installed and available in your Groovy environment. Once the plugin is ready, you can import and utilize its functionalities in your Groovy script by referencing its cl...
In Groovy, you can use the @groovy.transform.Field annotation to change the value of a Java superclass read-only field. This annotation allows you to access and modify the field directly, bypassing the normal restrictions on read-only fields. Simply annotate t...