Wednesday, March 2, 2011

Inheritance and GridWorld Actors

Today we reviewed the basic concepts of inheritance and some of the actors in the GridWorld program.

An inheritance diagram is a lot like a family tree with ParentClasses (SuperClasses) and ChildClasses(SubClasses)
An example is a Car SuperClass with SubClasses RaceCar and Truck which are different types of cars (duh!)
This example is shown here:
The arrow from the SubClass to the SuperClass basically means "is a".
So you would read RaceCar is a Car and Truck is a Car.
You wouldn't say Car is a RaceCar because a Car isn't necessarily always a RaceCar (you're parents don't drive race cars!)
As a kid would from it's parent, the subclasses inherit the characteristics of the superclass (methods). However, if a subclass has a method with the same name as a method in its superclass it overrides the superclass' method completely for that specific subclass.

The actors that we've learned about in GridWorld have been the bug, flower, and rock.
They behave much like they do in real life:
rock- does nothing
flower- becomes darker
bug- moves around (takes a step in the direction of it's antennae unless there is something in the way such as a rock or border in which case it turns 45 degrees to the right)
We also learned about different variations of the bug class such as BoxBug, CircleBug, SpiralBug, and ZBug which is due tomorrow.

If you're having trouble understanding inheritance look inside chapter 3 of your barron's review book, it explains things nicely.

No comments:

Post a Comment