17.12.2009
Abstract classes concrete
>>Author: Thomas Bahn
>>Ort: Schwentinental (Kiel)
URL: http://www.assono.de/blog/d6plinks/Abstract-classes-concreteCategory: OOP, Entwicklung, LotusScript
If you have some knowledge about other object-oriented programming languages, like Java, you probably like abstract classes. These are classes you cannot instanciate directly, i.e. create new objects from them. But you can derive other classes from them and create objects from these subclasses. In an abstract class you can have attributes and methods like in any other class.
Normally, abstract classes model some kind of concept or abstraction, for example Animal. There are dogs and cats, but no creature is merely an animal, its always of a concrete kind of animal.
In the abstract class Animal you put those attributes and members, which all animals have in common. Then you build subclasses like Dog or Cat, which add new or overwrite inherited members.
Other classes are abstract, because they have methods, which cannot be implemented at this level. Only concrete subclasses "know", how to implement them.
A typical example of this is a generic Sorter class with a Sort method. The concrete subclasses BubbleSorter, HeapSorter and QuickSorter overwrite the abstract Sort method with different implementations.
Unfortunately, in LotusScript there are no abstract classes. But there is a workaround, which can kind of simulate them:
Class
AbstractClass
Public Sub New()
If TypeName(Me) = UCase("AbstractClass") Then
Error 1, |Abstract class: … | & TypeName(Me)
End If
End Sub ' AbstractClass.New
Public Sub Method()
Error 2, |Abstract method: ...|
End Sub ' AbstractClass.Method
End Class
Class ConcreteSubclass As AbstractClass
Public Sub New()
' might be empty or not...
End Sub ' ConcreteSubclass.New
Public Sub Method()
' the implementation...
End Sub ' ConcreteSubclass.Method
End Class ' ConcreteSubclass
Public Sub New()
If TypeName(Me) = UCase("AbstractClass") Then
Error 1, |Abstract class: … | & TypeName(Me)
End If
End Sub ' AbstractClass.New
Public Sub Method()
Error 2, |Abstract method: ...|
End Sub ' AbstractClass.Method
End Class
Class ConcreteSubclass As AbstractClass
Public Sub New()
' might be empty or not...
End Sub ' ConcreteSubclass.New
Public Sub Method()
' the implementation...
End Sub ' ConcreteSubclass.Method
End Class ' ConcreteSubclass
Attention: The class name string in the if clause of the New method in the abstract class must match the class's name.
You can create an object from AbstractClass - the LotusScript compiler doesn't warn you like in Java - but when the code gets executed, an error is thrown.
When you build a subclass like the ConcreteSubclass, TypeName(Me) doesn't match UCase("AbstractClass") anymore and the constructor New is processed successfully.
If you don't overwrite the abstract method Method in the subclass and call it, the superclass's method is executed and aborts with a corresponding error.
