Question

In Fortran 2003, if an variable is declared as PRIVATE in a superclass, the subclasses will not be able to access it. But if all the variables are declared as PUBLIC, the program will lose the property of 'information hidding'.

Is there any way to take both 'data inheritance' and 'information hidding' in Fortran object-oriented programming? If not in 2003, is there any improvement in Fortran 2008?

Was it helpful?

Solution

My understanding is that, in Fortran 2003, any private components of a derived type defined in a module are directly accessible within that module. One implication (which I have not tested) is that those private components are directly accessible from sub-types defined in the same module but not from sub-types declared in another module. As I say I have not tested this and stand to be corrected.

If you have a compiler which implements sub-modules then the private components are also directly accessible in descendant submodules. I think sub-modules were introduced in a technical report after the publication of the 2003 standard and are now included in the 2008 standard but that not many of the widely-used compilers implement the feature.

Notwithstanding the standards it's not clear to me how much one loses by not having direct access to private components of super-types. In the object-oriented world it seems to be very common to make all components private (though other languages use other words to express the idea) and to provide access only through accessor methods -- in Fortran terms they would be procedures for assigning to or for returning the value of private components. This strict separation of internal data representation from external access permits changing the internal representations without changing how other procedures access the data, and this separation is generally thought to be a good idea.

The use of accessor procedures also permits a more fine-grained and subtle application of access control, for example by providing a procedure which sets the value of a component when an instance of a derived type is constructed but no procedure to modify the value of the component thereafter.

Private components with public accessor procedures seem to me to provide most of both data inheritance and information hiding but I'll be interested to read other answers to the question.

OTHER TIPS

Unfortunately, there is no direct way in Fortran to define components of a derived type being accessible from type bound procedures of extending derived types but hidden otherwise. As High Performance Mark pointed out, the access of the derived type components is based on modules: Private components of a derived type can only be accessed from within that module.

The pragmatic solution is to define everything public which should be accessible from outside, irrespective whether you want to use those components in extending derived types only or also elsewhere. Private and public in this concept only decides, whether you can change internal details (private) or not (public) without having to be worried about consequences for other parts of your or somebody elses code.

Also, please note, that even having a language feature like the protected members in C++, would not solve the basic problem: Once you declare something not being private, changing its behaviour will cause side effects in other codes, which you eventually are not even aware of (e.g. somebody extending your derived type and relying on the behaviour of some members being accessable from extending types.)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top