Trt Trt
Trt Trt

Reputation: 5532

Call groovy parent class method - Grails

I have a Grails service,

Parent Class:

class BarService{

  def fetchData(params) {

    return fooData.toString()

  }

}

Child Class:

class FooService extends BarService{

  def fetchData(params) {

    def fooData =  super.fetchData(params) //this will call the BarService method

    return fooData 
  }

}

Is this the right groovy way of doing it? Because to me this looks like Java

Thanks

Upvotes: 6

Views: 10388

Answers (2)

Carvo
Carvo

Reputation: 51

The "return" keyword isn't the problem (as U can see here - https://groovyconsole.appspot.com/edit/5158539458772992). If You're getting a "null", the problem is the code at:

return fooData.toString()

Should be like the @WillP said (with "return" keywork optionally):

return param.fooData.toString()

Upvotes: 1

Will
Will

Reputation: 14529

As per your example, there is not much that can be done, except maybe removing the optional return keyword:

// Parent Class:

class BarService{
  def fetchData(params) {
    params.fooData.toString()
  }
}

// Child Class:

class FooService extends BarService{
  def fetchData(params) {
    super.fetchData params
  }
}


assert new FooService().fetchData([fooData: 900]) == "900"

Upvotes: 10

Related Questions