As a LotusScript programmer you use objected-oriented programming (OOP) every day whether you realize it or not. The built-in LotusScript classes (NotesSession, NotesDatabase etc.) are classes based on OOP, written by IBM Lotus Software to make it easier for you to program Lotus Notes using abstractions you understand (such as databases, views, documents etc.) instead of dealing with a lot of low-level details.
In OOP parlance a class is a prototype object (it is a template) instructing the computer how to construct an object of a given type. The NotesDocument class creates NotesDocument objects.
OOP is built upon two core concepts:
- Inheritance
- Polymorphism
Inheritance means that one class can inherit its functionality (e.g. subs, functions and properties) from a parent class. If you recall the class hierarchy of the built-in LotusScript classes, NotesRichTextItem inherits from the NotesItem class making a NotesRichTextItem have all the same functionality of a NotesItem. Apart from the NotesItem functionality the NotesRichTextItem class also knows how to handle rich text formatting, doc-links etc.
Making NotesRichTextItem inherit from NotesItem means that the developer does not have to repeat all the code of the NotesItem class in the NotesRichTextItem class (it’s inherited).
Additionally inheritance also affords polymorphism. This means, in our NotesItem/NotesRichTextItem discussion, that every place I use a NotesItem In my code I can use a NotesRichTextItem instead since it is guaranteed to have all the same functionality since it is inherited. This means I can write code as follows:
Dim item As NotesItem
Set item = doc.GetFirstItem("MyNormalItem")
Msgbox item.Name
Set item = doc.GetFirstItem("MyRichTextItem")
Msgbox item.Name
This is a very powerful concept though it can take some time to really understand the benefits and be able to fully exploit it. To help clarify let me use an analogy.
Suppose you are out walking in the woods with your friend and his 5 year old son. You are a passionate about birds and know all about the different species, how and where they feed, breed etc. Your friend isn’t knowledgeable about birds his son even less so. When spotting a specific species of bird, say a Red-tailed hawk, you can tell your friend to look at the hawk. Not being into birds himself your friend doesn’t know all the details about that specific species he only knows that it is a predatory bird. It still means, however, that even without knowing any specifics about hawks your friend can talk intelligently about it since he knows it’s a predatory bird and it hence inherits many of its traits from predatory birds in general. In a sense he’s thinking “predatory bird” when you say “Red-tailed hawk”. Your friends’ son doesn’t need to distinguish between the different species and hence only sees a “bird”. He does know some general traits about birds though (they lay eggs, they can fly etc.).
Both are using polymorphism since they’re using a more general term (predatory bird / bird) instead of your specific term (Red-tailed hawk). They may now know specifics but they know enough to talk about the bird in decreasing detail.
Returning to our LotusScript example suppose you are writing a function returning a NotesItem (“bird”). You colleague doesn’t need to know that you sometimes return a rich text item (“hawk”) and sometimes a normal item (“bird”) since they can be used interchangeably as long as you only need access to common functionality (“it can fly”, “it lay eggs” etc.)
Thus you are leveraging polymorphism to help shield your colleague from the details he maybe does not need to know.