10.07.2015 Views

PGI Fortran Reference manual - The Portland Group

PGI Fortran Reference manual - The Portland Group

PGI Fortran Reference manual - The Portland Group

SHOW MORE
SHOW LESS
  • No tags were found...

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Object Oriented ProgrammingInheritance of Shape Componentstype shapeinteger :: colorlogical :: filledinteger :: xinteger :: yend type shapetype, EXTENDS ( shape ) :: rectangleinteger :: lengthinteger :: widthend type rectangletype, EXTENDS ( rectangle ) :: squareend type square<strong>The</strong> programmer indicates the inheritance relationship with the EXTENDS keyword followedby the name of the parent type in parentheses. A type that EXTENDS another type is known as atype extension (e.g., rectangle is a type extension of shape, square is a type extension of rectangleand shape). A type without any EXTENDS keyword is known as a base type (e.g., shape is a basetype).A type extension inherits all of the components of its parent (and ancestor) types. A typeextension can also define additional components as well. For example, rectangle has a lengthand width component in addition to the color, filled, x, and y components that were inheritedfrom shape. <strong>The</strong> square type, on the other hand, inherits all of the components from rectangle andshape, but does not define any components specific to square objects. <strong>The</strong> following exampleshows how to access the color component of square:type(square) :: sqsq%colorsq%rectangle%colorsq%reactangle%shape%color! declare sq as a square object! access color component for sq! access color component for sq! access color component for sq<strong>The</strong>re are three different ways for accessing the color component for sq. A type extensionincludes an implicit component with the same name and type as its parent type. This approachis handy when the programmer wants to operate on components specific to a parent type. It alsohelps illustrate an important relationship between the child and parent types.We often say the child and parent types have a "is a" relationship. In the shape example, "a squareis a rectangle", "a rectangle is a shape", "a square is a shape", and "a shape is a base type". Thisrelationship also applies to the type itself: "a shape is a shape", "a rectangle is a rectangle", and "asquare is a square".<strong>The</strong> "is a" relationship does not imply the converse. A rectangle is a shape, but a shape is not arectangle since there are components found in rectangle that are not found in shape. Furthermore,a rectangle is not a square because square has a component not found in rectangle; the implicitrectangle parent component.7.2. Polymorphic EntitiesPolymorphism permits code reusability in the Object-Oriented Programming paradigm becausethe programmer can write procedures and data structures that can operate on a variety of data<strong>PGI</strong> <strong>Fortran</strong> <strong>Reference</strong> Guide 122

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!