3 Comparison with a real stub method

What is the big deal? Instead of the definition of listing 1, I can define a concrete class A1 that can be used just like A in listing 4.


Listing 4:concretesuperclass
 
1class A1 
2{ 
3  public
4    virtual void m1(void{} 
5    void concrete(void{ m1(); } 
6};

You are absolutely correct that class A1 serves the same purposes as A, it has method m1 defined virtual, therefore it can be overriden by subclasses, just like m1 of A needs to be implemented.

However, there are some major differences.

In the case of class A, if a subclass D omits the implementation of m1, then the compiler complains when there is any code to create an object of class D. In other words, the abstract method m1 is saying: “this is just a stub, as a superclass, I have no idea of how to implement this method.”

However, in the case of A1, if a subclass D omits the overriding of m1, no error is generated. Instead, when m1 is called from a class D object, nothing happens. This is because method m1 of A1 is saying “this is the default way of m1, unless a subclass has a better idea of how to do this, do it my way.”