w3hello.com logo
Home PHP C# C++ Android Java Javascript Python IOS SQL HTML videos Categories
Abstract parent class needs class variable of child class

Groovy and Java don't have any sort of inheritance of static class members. When you say this.collectionName in a groovy class called Domain, you are referencing Domain.collectionName, even if you're calling the method through a subclass like Test.collection().

Even so, you can achieve a similar effect with a bit of meta-programming magic. You can add the static methods at runtime, resolving the static properties through the correct class. Unfortunately, it's not as simple as the python equivalent. Here's a small example:

class DomainHelper {
    static collection(collectionName) {
        def db = Server.getDB()
        return db.getCollection(collectionName)
    static apply(domainClass) {
        domainClass.metaClass.'static'.collection =
class Test {
    static collectionName = "test"

© Copyright 2018 w3hello.com Publishing Limited. All rights reserved.