I wrote a couple of lines of code to experiment and differentiate between these two: interface
and abstract class
.
I found out that they have the same restriction.
interface IPerson {
name: string;
talk(): void;
}
interface IVIP {
code: number;
}
abstract class Person {
abstract name: string;
abstract talk(): void;
}
class ManagerType1 extends Person {
// The error I get is that I need to implement the talk() method
// and name property from its base class.
}
class ManagerType2 implements IPerson {
// The error I get is that I need to implement the talk() method
// and the name property from the interface.
}
class ManagerType3 implements IPerson, IVIP {
// Now the error I get is that I need to implement all the
// properties and methods of the implemented interfaces to the derived class
}
As what I found is, there are no clear differences between these two since they both implement the same restriction. The only thing I notice is inheritance and implementation.
- A class can only extend to a single base class
- A class can implement multiple interfaces.
Did I catch it right? If so when do I need to use one?
UPDATE
I do not know if this is the right answer but you can really use BOTH depending on your situation. OOP is really cool.
class ManagerType3 extends Person implements IPerson, IVIP {
// Now the restriction is that you need to implement all the abstract
// properties and methods in the base class and all
// the properties and methods from the interfaces
}