Abstract Classes
After we've discussed inheritance and associated concepts, we can dive into more complicated ideas.
In previous examples, we've discussed classes of very tangible objects, such as cars and colored points (like on a graph). Sometimes, the occasion arises such that in order to factor out code from several classes, we need to define a superclass where it makes no sense to have an object of just that class, (the class represents an abstract concept rather than an object). For instance, let's say that we have a couple of classes that are all vehicles. We might have a sportsCar class, a Minivan class, and a truck class. In order to factor out some common code or define a common interface, we define a Vehicle class. But it makes little sense to instantiate objects of this Vehicle class (the objects should be one of sportsCar, Minivan, or Truck). And because we should have no objects of this class, it shouldn't be required to implement code for certain methods of this class if all three of the subclasses will have different implementations of the shared method anyway (All of the cars might have a different get_info method implementation). In these cases, we could use an abstract or virtual class that has virtual methods.
Abstract classes cannot be instantiated. In abstract classes, some methods can be declared without an implementation and only a type signature. To implement an abstract class, we will use the virtual keyword.
In previous examples, we've discussed classes of very tangible objects, such as cars and colored points (like on a graph). Sometimes, the occasion arises such that in order to factor out code from several classes, we need to define a superclass where it makes no sense to have an object of just that class, (the class represents an abstract concept rather than an object). For instance, let's say that we have a couple of classes that are all vehicles. We might have a sportsCar class, a Minivan class, and a truck class. In order to factor out some common code or define a common interface, we define a Vehicle class. But it makes little sense to instantiate objects of this Vehicle class (the objects should be one of sportsCar, Minivan, or Truck). And because we should have no objects of this class, it shouldn't be required to implement code for certain methods of this class if all three of the subclasses will have different implementations of the shared method anyway (All of the cars might have a different get_info method implementation). In these cases, we could use an abstract or virtual class that has virtual methods.
Abstract classes cannot be instantiated. In abstract classes, some methods can be declared without an implementation and only a type signature. To implement an abstract class, we will use the virtual keyword.
class virtual abstract_class_example () =
object (self)
method virtual get_info : unit -> string
method print_hello () = Printf.printf "Hello world!\n"
end
;;
class virtual abstract_class_example :
unit ->
object
method virtual get_info : unit -> string
method print_hello : unit -> unit
end
As we can see, when we implement a virtual method, the class must also be a virtual class. (To have a virtual class does not mean that you need any virtual methods, however). When we implement a virtual method, we just write out the type signature. To define a subclass of this class, we need to actually implement get_info and any other virtual methods according to the declared type signature.
class example_subclass () =
object (self)
inherit abstract_class_example () as super
method get_info () = "Example"
end
;;
class example_subclass :
unit ->
object
method get_info : unit -> string
method print_hello : unit -> unit
end
And just for fun and a review of what we've cover previously, let's make a subclass that inherits from example_subclass.
class example_subclass_2 () =
object (self)
inherit example_subclass () as super
method get_info () = (super#get_info ()) ^ " sub(sub)class"
method additional_method number =
match number with
| 4 -> number + self#additional_method (number - 1)
| _ -> 3
method additional_method_2 () = ()
end
;;
class example_subclass_2 :
unit ->
object
method additional_method : int -> int
method additional_method_2 : unit -> unit
method get_info : unit -> string
method print_hello : unit -> unit
end
We will not be covering open types for objects. For more information on open types look here: