Inheritance (object-oriented programming)In object-oriented programming languages, inheritance is a way to add functionality. OverviewObject-oriented programming has the notion of classes (and perhaps interfaces). A derived class inherits most fields and methods from its parent class. It can modify the behaviour of the parent, by adding new fields and methods, or by modifying existing ones. Depending on the programming language used, there may be certain restrictions when extending a class. ApplicationsInheritance can be used to solve different types of problems:
ExamplesAn example can be a Car class. A Ferrari class could inherit from Car. A Toyota class which could also inherit from Car. All the fields and methods (drive method, brake method, color field...) would also be inside of Ferrari and Toyota. There would be no need to copy the code twice. Inside of Car there could be a brand field. This brand field would be equal to nothing, but inside of Ferrari the field might be equal to "ferrari" and in Toyota to "toyota". Related pages |
Portal di Ensiklopedia Dunia