Unformatted text preview:

Object FundamentalsKenneth M. AndersonUniversity of Colorado, BoulderCSCI 4448/6448 — Lecture 2 — 08/28/20081Friday, August 29, 2008Lecture Goals• Introduce basic concepts, terminology, and notations for object-oriented analysis, design, and programming• A benefit of the OO approach is that the same concepts appear in all three stages of development• Start with material presented in Appendix II of your textbook• Continue (in lecture 3) with additional material from previous versions of this class as well as from Head First Java by Sierra & Bates, © O'Reilly, 2003• Will present examples and code throughout2Friday, August 29, 2008Big Picture View• OO techniques view software systems as•systems of communicating objects•Each object is an instance of a class•All objects of a class share similar features•attributes•methods•Classes can be specialized by subclasses•Objects communicate by sending messages3Friday, August 29, 2008Welcome to Objectville• What were the major concepts discussed in Appendix!II of the textbook?• Unified Modeling Language (UML)• Class Diagrams• Inheritance• Polymorphism• Encapsulation4Friday, August 29, 2008UML•UML stands for Unified Modeling Language•UML defines a standard set of notations for use in modeling object-oriented systems• Throughout the semester we will encounter UML in the form of• class diagrams• sequence/collaboration diagrams• state diagrams• activity diagrams, use case diagrams, and more5Friday, August 29, 2008Class DiagramsgetSpeed(): intsetSpeed(int)speed: intAirplaneA class is represented as a rectangleClass NameAttributes(member variables)MethodsAll parts are optionalexcept the class nameThis rectangle says that there is a class called Airplane that could potentially have many instances, each with its own speed variable and methods to access it6Friday, August 29, 2008Translation to Code• Class diagrams can be translated into code in a fairly straightforward manner• Define the class with the specified name•Define specified attributes (assume private access)•Define specified method skeletons (assume public)• May have to deal with unspecified information• Types are optional in class diagrams•Class diagrams typically do not specify constructors•constructors are used to initialize an object7Friday, August 29, 2008Airplane in Javapublic class Airplane {1 2 private int speed;3 4 public Airplane(int speed) {5 this.speed = speed;6 }7 8 public int getSpeed() {9 return speed;10 }11 12 public void setSpeed(int speed) {13 this.speed = speed;14 }1516}178Friday, August 29, 2008Airplane in Pythonclass Airplane(object):1 2 def __init__(self, speed):3 self.speed = speed4 5 def getSpeed(self):6 return self.speed;7 8 def setSpeed(self, speed):9 self.speed = speed109Friday, August 29, 2008Airplane in Rubyclass Airplane12 attr_accessor :speed34 def initialize(speed)5 @speed = speed6 end7 8end910Friday, August 29, 2008Using these Classes?• The materials for this lecture contains source code that shows how to use these classes• Demonstration•Airplane.java, Airplane.py, Airplane.rb• Be sure to attempt to run these examples on your own• It will be good experience to learn how to run Java, Python, and Ruby programs on your personal machine or on a Lab machine (either ITS or CSEL)11Friday, August 29, 2008Inheritance•Inheritance refers to the ability of one class to inherit behavior from another class• and change that behavior if neededgetSpeed(): intsetSpeed(int)speed: intAirplaneaccelerate()MULTIPLIER: intJetInheritance lets you build classes based on other classes and avoid duplicating and repeating codeNote: UML notation to indicate inheritance is a line between two classes with a triangle pointing at the base class or superclass12Friday, August 29, 2008Inheriting From Airplane (in Java)public class Jet extends Airplane {12 private static final int MULTIPLIER = 2;3 4 public Jet(int id, int speed) {5 super(id, speed);6 }7 8 public void setSpeed(int speed) {9 super.setSpeed(speed * MULTIPLIER);10 }11 12 public void accelerate() {13 super.setSpeed(getSpeed() * 2);14 }1516}1718Note:extends keyword indicates inheritancesuper() and super keyword is used to refer to superclassNo need to define getSpeed() method; its inherited!setSpeed() methodoverrides behavior of setSpeed() in Airplanesubclass can define new behaviors, such as accelerate()13Friday, August 29, 2008Inheritance in Pythonclass Jet(Airplane):1 2 MULTIPLIER = 234 def __init__(self, id, speed):5 super(Jet, self).__init__(id, speed)67 def setSpeed(self, speed):8 super(Jet, self).setSpeed(speed * Jet.MULTIPLIER)910 def accelerate(self):11 super(Jet, self).setSpeed(self.getSpeed() * 2);121314Friday, August 29, 2008Inheritance in Rubyclass Jet < Airplane12 @@MULTIPLIER = 234 def initialize(id, speed)5 super(id, speed)6 end78 def speed=(speed)9 super(speed * @@MULTIPLIER)10 end1112 def accelerate()13 @speed = @speed * 214 end1516end171815Friday, August 29, 2008Polymorphism: “Many Forms”• From the textbook: “When one class inherits from another, then polymorphism allows a subclass to stand in for the superclass.”• Implication: both of these are legal statements• Airplane plane = new Airplane()• Airplane plane = new Jet()• Any code that uses the “plane” variable will treat it as an Airplane… this provides flexibility, since that code will run unchanged, indeed it doesn’t even need to be recompiled, when new Airplane subclasses are created16Friday, August 29, 2008Encapsulation• Encapsulation is• when you hide parts of your data from the rest of your application• and limit the ability for other parts of your code to access that data• Encapsulation lets you protect information in your objects from being used incorrectly• Closely Related Concept: Abstraction•What features does a class provide to its users?• What services can it perform?• Indeed, abstraction is the MOST IMPORTANT concern in OO A&D!!17Friday, August 29, 2008Encapsulation Example• The “speed” instance variable is private in Airplane. That means that Jet doesn’t have direct access to it.• Nor does any client of Airplane or Jet objects• Imagine if we changed speed’s visibility to public• The encapsulation of Jet’s setSpeed() method would be destroyedAirplane12...3public void setSpeed(int speed) {4 this.speed = speed;5}6...78Jet910...11public


View Full Document

CU-Boulder CSCI 6448 - Object Fundamentals

Documents in this Course
Struts

Struts

12 pages

Adapter

Adapter

23 pages

Prototype

Prototype

16 pages

Weka

Weka

15 pages

qooxdoo

qooxdoo

16 pages

Django

Django

12 pages

Overview

Overview

22 pages

XNA

XNA

5 pages

Load more
Download Object Fundamentals
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Object Fundamentals and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Object Fundamentals 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?