Hey, I'm quite experienced with PHP but I have no idea what the keyword abstract does when it comes down to object orientated programming. Can anyone explain in plain english what it can be used for?
What situations would I use the abstract keyword in? How does it change the class/interface?
Abstract classes are used to an actual a-kind-of-model relationship. This allows for example a database driver to map the hierarchy, in which aims to provide a common base class, the signatures for the methods of the actual driver classes. The implementation is then carried out in accordance with the predetermined signatures in the actual driver classes.
here is code example
(Hope this is simple enough -- I don't think I can do better ^^ )
An
abstract
class can not be instanciated : you can only create another class that inherits from theabstract
class, and instanciate that child class.And if you declare some methods as
abstract
, those must be defined in the child class, for that one to be instanciable.Though you cannot instantiate an abstract class, you can declare concrete methods/properties/variables (in C#, AFAIK) which will be available to the derived class
Declaring a class abstract means that it must be subclassed in order to be used. An abstract class can not be instantiated. One can see it as an extended interface that might include implementation code (as opposed to an interface).
By declaring a method abstract, you force the sub class to implement the method.
The definition is mentioned above, now I will try to give you an example:
"abstract" ensures that you follow a specific logic, e.g. a ticket's material is ALWAYS "paper", or a creditcard must always have a "code". This is important if you work in a big company which has strict standardisation or if you want to 'force' your developers to follow a specific structure, so their code won't end up in a mess.
If we do not define the "setCode" method the parser will return an error on "
new anotherKey
"