Class-Based Object Oriented Programming A class specifies what members and methods its objects have. For example a vehicle class might have members for its name, top speed, a number, and propulsion mechanism: wheel, propeller, or rocket. It might have members which return its name and top speed. Class and Constructor A constructor is a function which creates an instance of a class. Here is a constructor for the example vehicle class above. The constructor creates a function which represents the object. Even after the constructor returns, the object has access to the local variables which were created in the constructor when the object was created. Since the local variables in the constructor are created anew each time it is called, each object has its own members. The function which represents an object takes a message. If that message corresponds to one of its methods, it returns a function corresponding to that method. The caller can then call that function to interact with the object. Constructor & Class Questions 1. What is the name of the constructor function? 2. What is the difference between 3. Why is called ‘my name’? 4. What are the members? and ? 5. What are the methods? Here is an example of the constructor being used to create an instance of the vehicle class. We pass messages ‘get name’, ‘set speed’, and ‘move current sprite’ to the instance. These activate the corresponding methods in the object. Method Questions 1. 2. 3. 4. Why does the constructor return a function? Why is call always used to pass a message to the object? How do you know if call or run should be used to invoke the method returned from an object? The set speed method can be used to change the speed of the vehicle. From outside the object, is there any way of seeing what the speed currently is? Inheritance Not all vehicles are reliable. To represent this, let’s make a subclass of vehicle which has a percentage chance of not moving when asked to. The subclass will override the superclass’s move current sprite method to make it unreliable. Here’s the constructor for the unreliable vehicle: Subclass Questions: 1) When we pass the move current sprite message to an unreliable vehicle and call the returned function, the behavior is different than reliable vehicle. Why? 2) We can pass get name to an unreliable vehicle and get back its name. Yet there is no code in make unreliable vehicle to explicitly set the name. How is this possible? 3) Bonus: Why do we have the object return a function in response to a message instead of simply running the action itself? Application Questions 1) How does class-based object-oriented programming allow objects to have different properties while sharing as much code as possible? 2) If you found a problem with an object-oriented program, would it be easier to debug if it were class-based or prototype based? 3) You are working on a multi-person program. How might class-based object-oriented programming make it easier to divide and then integrate the work? 4) What aspects of functional programming do you see in this implementation of class-based object-oriented programming? Concluding Notes This worksheet walked through an implementation of class-based object oriented programming. Many class-based object-oriented programs optimize their syntax so that their programs weren’t so convoluted.