Pier
Pier

Reputation: 359

Method override in Groovy

How can one override a method from the superclass in a class that extends the superclass, in Groovy? The Java way is not working, as both methods (the one in the superclass and the one in the subclass) are being executed. For example:

class SuperClass {

  SuperClass(){

    println("This is the superclass")
  }

  def awaitServer(){
    println("awaiting server in the superclass")
  }

}


class SubClass extends SuperClass{


  SubClass(){
    println("This is the subclass")
  }

  @Override
  def awaitServer(){
    println("awaiting server in the subclass")
  }

}

//////
SubClass sb = new SubClass()
sb.awaitServer()

The output i get is:

awaiting server in the superclass
awaiting server in the subclass

As you can see both methods are executed, when i'm overriding the method of the super class in the subclass. Why is this happening? How is the method overriding done in Groovy?

I've searched on the web, but I'm not able to figure it out. Can someone provide a sample or a simple example?

Thank you in advance,

Upvotes: 0

Views: 22484

Answers (1)

Opal
Opal

Reputation: 84756

You forgot to add def. This code works correctly:

class SuperClass {
   SuperClass(){
      println("This is the superclass")
   }

   def awaitServer() {
      println("awaiting server in the superclass")
   }
}

class SubClass extends SuperClass{

   SubClass() {
      println("This is the subclass")
   }

   @Override
   def awaitServer() {
      println("awaiting server in the subclass")
   }
}

SubClass sb = new SubClass()
sb.awaitServer()

It outputs:

This is the superclass 
This is the subclass 
awaiting server in the subclass

Have a look at the output below:

[opal@opal-mac-2]/tmp % cat lol.groovy 
class SuperClass {
   SuperClass(){
      println("This is the superclass")
   }

   def awaitServer() {
      println("awaiting server in the superclass")
   }
}

class SubClass extends SuperClass{

   SubClass() {
      println("This is the subclass")
   }

   @Override
   def awaitServer() {
      println("awaiting server in the subclass")
   }
}

SubClass sb = new SubClass()
sb.awaitServer()

[opal@opal-mac-2]/tmp % groovy -v
Groovy Version: 2.4.0 JVM: 1.8.0_05 Vendor: Oracle Corporation OS: Mac OS X
[opal@opal-mac-2]/tmp % groovy lol.groovy 
This is the superclass
This is the subclass
awaiting server in the subclass

With groovy 1.8.6:

[opal@opal-mac-2]/tmp % gvm use groovy 1.8.6
==== BROADCAST =================================================================
* 27/02/15: Springboot 1.1.11.RELEASE has been released on GVM. #spring
* 27/02/15: Springboot 1.2.2.RELEASE has been released on GVM. #spring
* 26/02/15: Grails 3.0.0.M2 has been released on GVM. #grailsframework
================================================================================

Stop! groovy 1.8.6 is not installed.
Do you want to install it now? (Y/n): Y

Downloading: groovy 1.8.6

  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
  0     0    0     0    0     0      0      0 --:--:-- --:--:-- --:--:--     0
100 15.5M  100 15.5M    0     0  2666k      0  0:00:05  0:00:05 --:--:-- 3716k

Installing: groovy 1.8.6
/Users/opal/.gvm/tmp/groovy-1.8.6 -> /Users/opal/.gvm/groovy/1.8.6
Done installing!


Using groovy version 1.8.6 in this shell.
[opal@opal-mac-2]/tmp % groovy lol.groovy 
This is the superclass
This is the subclass
awaiting server in the subclass

Upvotes: 4

Related Questions