RU beehive logo ITEC dept promo banner
ITEC 380
2013fall
ibarland
aaray

homelecturesrecipeexamshwsD2Lbreeze (snow day)

lect41-oo-class-vs-instance
class- vs instance-based object systems
i.e. java vs javascript

(Explain Java objects, memory layout; how inheritance works w/ the layout.) When a field or method doesn't reside in the derived class specially, it looks for it in the super view of itself. When a method is overridden, it gets added to the derived class, so you're still able to call the superclass's version.

  class Animal {
      String sound;
      String speak() { return sound + "."; };
      Animal( String _sound ) { this.sound = _sound; }
      }

  class Cat extends Animal {
      double clawSharpness;
      Cat() { super("meeeeeow");  this.clawSharpness = 1.0; }
      }

  class AngryCat extends Cat {
      String speak() { clawSharpness *= 1.1; return super.speak(); }
      }
  

What's the difference between a method and a field? In java, it a big difference. What about, in a racket struct? No real difference -- just a field whose value is a function. This means that different objects of the class could have different values(code) for a given 'method'!

In Javascript, there are objects — but really, they are nothing but hash tables! You just look up a field-name, and get back a value (which might be a function-value). js objects as hashes

But in javascript, there aren't "classes" per se. Instead, you might make one special Animal object, and declare it as a "prototype": If a lookup in the hash-table fails, the language chains the lookup via its prototype object.
Another way of thinking about it: instead of each object haveing a superclass it has a superobject.

This means that you can dynamically ("retroactively"?) add methods to a whole bunch of existing objects, by just addint to the prototype object: example of changing prototype.

homelecturesrecipeexamshwsD2Lbreeze (snow day)


©2013, Ian Barland, Radford University
Last modified 2013.Dec.02 (Mon)
Please mail any suggestions
(incl. typos, broken links)
to ibarlandradford.edu
Powered by PLT Scheme