DOC PREVIEW
CU-Boulder CSCI 5448 - Object Fundamentals, Part One

This preview shows page 1-2-3-26-27-28 out of 28 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 28 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 28 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 28 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 28 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 28 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 28 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 28 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

Object Fundamentals, Part OneKenneth M. AndersonUniversity of Colorado, BoulderCSCI 4448/5448 — Lecture 2 — 08/27/20091Thursday, August 27, 2009Lecture 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 throughout2Thursday, August 27, 2009Big 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 messages3Thursday, August 27, 2009Welcome to Objectville• What were the major concepts discussed in Appendix!II of the textbook?• Unified Modeling Language (UML)• Class Diagrams• Inheritance• Polymorphism• Encapsulation4Thursday, August 27, 2009UML•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 more5Thursday, August 27, 2009Class 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 it6Thursday, August 27, 2009Translation 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 object7Thursday, August 27, 2009Airplane 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}178Thursday, August 27, 2009Airplane 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 = speed109Thursday, August 27, 2009Airplane in Rubyclass Airplane12 attr_accessor :speed34 def initialize(speed)5 @speed = speed6 end7 8end910Thursday, August 27, 2009Using 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)11Thursday, August 27, 2009Inheritance•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 superclass12Thursday, August 27, 2009Inheriting 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()13Thursday, August 27, 2009Inheritance 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);121314Thursday, August 27, 2009Inheritance 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 end1516end171815Thursday, August 27, 2009Polymorphism: “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 created16Thursday, August 27, 2009Encapsulation• 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!!17Thursday, August 27, 2009Encapsulation 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


View Full Document

CU-Boulder CSCI 5448 - Object Fundamentals, Part One

Documents in this Course
Django

Django

42 pages

ENRS

ENRS

30 pages

PhoneGap

PhoneGap

22 pages

Load more
Download Object Fundamentals, Part One
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, Part One 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, Part One 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?