4

Is it possible to access the variable of a parent class via a child class?. Code example as follows:

public class ParentClass {
    private Date creationDate = Date.today();

    private class ChildClass {
        private Date getCreationDate() {
            return creationDate;
        }

    }
}

3 Answers 3

7

Yes, but not like that. As written, you're treating the outer class like a closure, which Apex does not have.

Your instance of ChildClass would need to be holding a reference to an instance of ParentClass to access its member variables like that, and the member variable would have to be declared protected:

public class ParentClass {
    protected Date creationDate = Date.today();

    private class ChildClass {
        private ParentClass myParent;

        private Date getCreationDate() {
            return myParent.creationDate;
        }
    }
}

See Access Modifiers:

private

This is the default, and means that the method or variable is accessible only within the Apex class in which it is defined. If you do not specify an access modifier, the method or variable is private.

protected

This means that the method or variable is visible to any inner classes in the defining Apex class, and to the classes that extend the defining Apex class. You can only use this access modifier for instance methods and member variables. Note that it is strictly more permissive than the default (private) setting, just like Java.

4

You can access a parent's protected or private variables if those variables are declared as static. Without the static keyword, you'll need a reference to an instance of the class, instead of a static reference to the class itself.

public class ParentClass {
    private static Date creationDate = Date.today();

    private class ChildClass {
        private Date getCreationDate() {
            return ParentClass.creationDate;
        }

    }
}

You could also try doing some layering with interfaces. Children of an virtual class can access private or protected variables inside the parent class.

public virtual class ParentClass {
    protected Date creationDate = Date.today();

    private class ChildClass extends ParentClass {
        private Date getCreationDate() {
            return creationDate;
        }
    }
}
1

It is also possible to "hardwire" the relationship between classes that are not nested. This is especially useful for scenarios where you want to have several levels of nesting, such as a serializable DTO structure. But Apex only allows one level. Doing something like this will predictably result in a null reference exception:

public class ParentClass {
    public Date creationDate = Date.today();
}

public class ChildClass {
    private ParentClass myParent;

    public Date getCreationDate() {
        return myParent.creationDate;
        // ^^ NULL reference exception here
    }
}

A workable solution is to pass the parent in the constructor of the child, which seems janky but definitely works:

public class ParentClass {
    public Date creationDate = Date.today();
}

public class ChildClass {
    private ParentClass myParent;

    public ChildClass(ParentClass parent) {
        myParent = parent;
    }

    public Date getCreationDate() {
        return myParent.creationDate;
    }
}

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .