Tuesday, November 07, 2006

ICA-CP Info: Classes, inheritance, and polymorphism

We talked about IS-A and HAS-A relationships in Java today using ICA as an example.

ICA is a superclass (or parent) for Class07, Class08, and Class2011 (which are subclasses or child classes). Each of the classes has attributes which describe the class. For example, the parent class of ICA has attributes of Teachers, Uniform, Mascot Sports, Classes, Colors, and Students. The child classes inherit those attributes. However, the child classes may have attributes which override the inherited classes as well as additional attributes. For example, the seniors (Class07) and juniors (Class06) each have attributes of class sweater which override the regular burgundy sweater inherited as part of the uniform from the parent, ICA. They also have class ring attributes which do not exist in the parent, ICA. The relationship between parent and child classes is called an IS-A relationship. For example, Class06 IS-A ICA, and Class07 IS-A ICA. The classes have attributes which describe the class. The relationship between a class and its attributes is a HAS-A relationship. For example, we would say that Class06 HAS-A class sweater, and Class07 HAS-A class ring. To summarize: HAS-A refers to attributes; IS-A refers to the relationship of subclass to superclass. Having an IS-A relationship implies that the subclass inherits attributes from the superclass. In our Java programs, attributes often become variable names. Attributes may also become names of methods.

We can talk about next school year's incoming freshman, Class2011. We know they will inherit attributes from the parent class, ICA, such as Teachers, Uniform, Mascot, Sports, Students, Colors, and Cafeteria. We know that they will have additional attributes of Big Sisters, Orientation Day, Frosh Folly, Wel Dinner, Colors, Song, and Welcome Dinner. However, we don't yet have any students in the Class of 2011. We don't have "real" students yet. When we do, we instantiate an object in Class2011. Until that time when we have an actual person in Class2011, the Java class of Class2011 is just a template. Another example is a recipe for Brownies. The recipe is the template, the description of the brownie. Once we make and bake the brownies, we instantiate them. We make them "real." Just like you can't eat a brownie until you actually make the recipe, we cannot instantiate a class until we have a real object in the class created by using the new Java command. An example of using the new command to instantiate an object is the following line we used in our Parrot class:

KeyboardReader new rr;

The keyword new creates space in the memory of a computer. So, a brief survey of computer components:
Memory - is like our memory
Central Processor Unit or Microprocessor - is like our brain
Input/Out Devices - are like our senses. Examples of Input devices are webcam, keyboard, mouse, and microphone. Examples of Output devices are speakers and monitors. Hard drives (which is extra storage in addition to Memory), touch screens, and flash disks are example of Input and Output devices. When Java programs run, space is created for them in memory. When we use the Java command new to instantiate an object, a spot in the space reserved for Java programs is created in the computer's memory.

0 Comments:

Post a Comment

<< Home