Narrowing cast means copying an instance of sub class to an instance of super class.
For example: Car is super class and race car is a sub class of car. Race car has all the parameters as car and some more parameters. Think of a scenario were we have an instance of race car, but we don’t need all the parameters of race car, only some basic parameters are required. In this case we will create an instance of car from the race car instance through narrowing cast. So car instance is made with all the basic parameters from race car.
i.e.
race_car instance of class race car is already available.
Data : new_car type ref to car.
new_car ?= race_car. Here “?=” is the syntax for narrowing cast.
Widening cast means copying an instance of super class to an instance of sub class.
This is the exact opposite of narrowing cast. For example: We have an instance of super class car, but we need to create an instance of sub class race car with same parameters and we will add the remaining parameters later. Here we will use the widening cast to create an instance of race car from car with all basic parameters from car. Remaining parameters could be added to race car instance through other methods of race car.
i.e.
car_old instance of class car is already available.
Data : new_racecar type ref to race car.
new_racecar = car_old. Here “=” is the syntax for widening cast.
Monday, November 17, 2008
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment