Pages

Translate

Wednesday, 23 January 2013

Lesson 2 :Inheritance



   
If you were creating an application to handle inventory at a car dealership,you would probably need classes such as Mini-Van,Motor and Race-car ,they would correspond to the same types of car in dealer's inventory. You application would not need only to show how many of these items you have ,but to report on the characteristics of these vehicles so that salespeople could give the information to customers.



    Inheritance enables you to define a base class, in this class :Car. You can say that other classes :MiniVna,Motorand RaceCarare also a type of class:Car.so that they will have all the same properties and methods that class:Car contains.



    The ability to reuse code is one benefit ,but there is a second major advantage to using inheritance. Suppose that you have a class called Customer with a method:buyCar. This method would take one parameter,an object:class Car,and its internal operations would print the paperwork needed to document the sale,and decrease the number of Car from inventory system. Because all Mini-Van,Motor and Race-car are Car,You can pass object of these classes to a function expecting an Car. Because the three specific types inherit from the more generic parent class,you know that they will all have the same base set of properties and methods.As long as you need only the methods and properties common to all Car,You can accept objects of any class that inherits from Car.



    In PHP specify that a class is a subset of another by using the keywordextends,which tells PHP that the class you are declaring should inherit all the properties and methods from its parent class,and that you are adding functionality or providing some additional specialization to that class.

No comments:

Post a Comment