Friday, March 4, 2011

Polymorphism and abstract classes

Thursday's lesson was a bit easy to comprehend. Mr. Jacoby taught us a brief concept of polymorphism and abstract classes. Polymorphism is the ability of the Java virtual machine to determine at runtime which version of a method should be used based on the actual class of the object (instead of the declared type of the variable). In other words, it is the ability of a reference variable to change behavior according to the object instance it is holding. The java virtual machine determines the proper method to call the object. The example we did in class was:

Animal class declares speck: it says "hi".
Dog class extends animal, it overrides speck: it says "woof".
Cow class extends animal, it overrides speck: it says "wassup?".

public void talkToAnimal(Animal a) {
a.speak();

}


a will be an object. The object can be Animal, Dog, or Cow. So if the object is cow, it will therefore call out "wassup?" If the Java virtual machine did not know the object, it wouldn't have done anything.

.....

An abstract class declares fields and abstract methods so that its subclasses can use them, but it represents an abstract idea that can't itself be instantiated. An abstract class need concrete subclasses to implement abstract methods. Concrete subclasses are specific. For example, an abstract class would be Car and a concrete subclass would be Move.


- I tried to explain as best as i could and i hope this helped.


Be sure to do Homework 18 (due Monday): GridWorld pg. 15 # 4 (dancing bug!!)

Reminder: We have REACH tomorrow!!....Note: The GridWorld assignment will be bonus for students who attend REACH.




No comments:

Post a Comment