The short answer to your question is, you have to implement consoleGivenMessage(text: string)
in FirstClass
so that you can call it on instances of both FirstClass
and SecondClass
.
However, there is more--
Most of the time, you call an inherited method from the derived class instead of the other way round. But, you can also have a base class that depends on an abstract method that is implemented in a derived class.
Say, you have a class A
that depends on a method DoIt()
which is implemented only in derived class B
, you would have to declare A
as an abstract class and DoIt()
as an abstract method; then, in B
(which is not abstract--that is, it is concrete) you would implement the method DoIt
().
This also means that you cannot instantiate an object of A
because it is not complete without a full implementation of DoIt
, but you can instantiate an object of B
. However, you can define an object of A
, like this: const a: A = new B()
. And, you can call a.DoIt()
. In this case, the implementation of B.DoIt()
would actually be called.
This technique is used in the Template Method design pattern.
TypeScript classes, inheritance, and abstract classes are well documented.