1 / 63

Programming Fundamentals

Neal Stublen nstublen@jccc.edu. Programming Fundamentals. Object-Oriented Programming Concepts. Key Concepts. Classes Objects Inheritance Polymorphism Encapsulation. Classes and Objects. A class describes what an object looks like Attributes Behavior

phong
Download Presentation

Programming Fundamentals

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Neal Stublen nstublen@jccc.edu Programming Fundamentals

  2. Object-Oriented Programming Concepts

  3. Key Concepts • Classes • Objects • Inheritance • Polymorphism • Encapsulation

  4. Classes and Objects • A class describes what an object looks like • Attributes • Behavior • An object is an instance of a particular class • A class is like the definition for “mouse” in the dictionary. • Object instances are like a dozen of them sitting on desks in the room.

  5. Attributes • An object’s attributes are represented by a class’ instance variables (or member variables, or fields) • The content of all of an object’s instance variables define its state class Door bool opened bool locked endClass • Each instance of Door can have one of four states

  6. Behaviors • An object’s behaviors are represented by its methods class Door void closeDoor return void lockDoor return void openDoor return void unlockDoor return endClass

  7. Similar Yet Different • Consider two objects • Movie • Song • What attributes do they have in common? • What behaviors do they have in common? • What attributes and behaviors are unique to each one?

  8. Inheritance • If we consider a Movie and a Song to both be types of a MediaItem, we can define a MediaItem and say Movie and Song both inherit certain attributes and behaviors from MediaItem • Heirarchy: • MediaItem • Song • Movie

  9. Polymorphism • We know a MediaItem has behaviors to play and pause, but the behavior is different depending on whether we are referring to a Movie or Song • Defining play and pause methods on a Movie or Song class is an example of polymorphism • The method call looks the same for both objects, but the specific behavior is different • A MediaItem can take on many (poly) shapes (morph)

  10. Encapsulation • Every class encapsulates internal information by hiding it from other objects (information hiding) • Hiding an object’s internal information makes it possible to modify the internal structure of an object without affecting other objects that may use it

  11. Defining Classes • Every class has a unique name • Most classes contain data (fields) • Most classes contain methods • A class that only contains simple data values may not define any methods, but this is rare • Our Program classes have always contained methods, but no data – again, this is rare

  12. An Employee Class class Employee string name string getName() return name void setName(string newName) name = newName return endClass • The class has a unique name

  13. An Employee Class class Employee string name string getName() return name void setName(string newName) name = newName return endClass • The class contains a field

  14. An Employee Class class Employee string name string getName() return name void setName(string newName) name = newName return endClass • The class contains methods

  15. A CardStack Class class CardStack num cards[52] num draw() num card = the next card in the stack return card void shuffle() rearrange the cards in the stack return endClass

  16. A Card Class class Card num value // 0 – 51 num getRank() return (value % 13) + 1 // 1 – 13 num getSuit() return value / 13 // 0 – 3 boolisFaceCard return getRank() > 10 // true or false endClass

  17. Declaring Instances • Since a class is like a definition in a dictionary, we need to create an instance of the class to actually do anything • Creating an instance allocates memory for the class variables

  18. Declaring Instances • Declaring variables uses the computer’s memory • Declaring class instances works the same way int int int string int CardStack . . . int int int int int int int int int int int int int int int int

  19. Declaring Instances • Each instance declaration creates a new object num counter = 0 string name = “John Smith” Employee assistant Employee manager Employee president int string Employee Employee Employee

  20. An Employee with More Fields class Employee int id string name string hireDate …Methods for the class… endClass

  21. Memory for Each Instance • Each instance of Employee has its own allocation of class variables int string Employee Employee Employee int string string int string string int string string

  22. Using Methods • Methods are accessed using a “dot” notation on an instance of the class // Create an instance of the Employee class Employee manager // We now have an employee object. // Set and get the name for an instance of // the Employee class manager.setName(“John Smith”) output manager.getName() // John Smith

  23. Using Multiple Instances Employee manager manager.setName(“John Smith”) Employee assistant assistant.setName(“Fred Jones”) output manager.getName() // John Smith output assistant.getName() // Fred Jones

  24. Review Which of the following is the same as object? a) class b) field c) instance d) category

  25. Review Which of the following is the same as object? a) class b) field c) instance d) category

  26. Common Types of Methods • “Set” methods are used to set fields in the class class Employee string name void setName(string newName) name = newName return endClass

  27. Common Types of Methods • “Get” methods are used to retrieve data from the class class Employee string name string getName() return name endClass

  28. Common Types of Methods • “Get” methods may combine fields class Employee string firstName string lastName string getFullName() string name = lastName + ", " + firstName return name endClass

  29. Common Types of Methods • “Work” methods may calculate a return value class Circle num radius num calcArea() num area = PI * radius * radius return area num calcCircumference() num circumference = 2 * PI * radius return circumference endClass

  30. Public and Private Access • We may want to control access to some fields and methods • Fields and methods can be declared as public or private • Public fields and methods can be accessed from outside the class • Private fields and methods can only be accessed from within the class

  31. Back to the CardStack • Which fields and methods do we want to protect? class CardStack num cards[52] num draw() num card = the next card in the stack return card void shuffle() swap random cards in the stack return void swap(num cardIndex1, num cardIndex2) switch the card at index 1 with the card at index 2 return endClass

  32. Back to the CardStack • We don’t want free access to any card in the stack class CardStack private num cards[52] num draw() num card = the next card in the stack return card void shuffle() swap random cards in the stack return void swap(num cardIndex1, num cardIndex2) switch the card at index 1 with the card at index 2 return endClass

  33. Back to the CardStack • We do want access to the card on top of the stack class CardStack private num cards[52] public num draw() num card = the next card in the stack return card void shuffle() swap random cards in the stack return void swap(num cardIndex1, num cardIndex2) switch the card at index 1 with the card at index 2 return endClass

  34. Back to the CardStack • We do want ability to shuffle the stack class CardStack private num cards[52] public num draw() num card = the next card in the stack return card public void shuffle() swap random cards in the stack return void swap(num cardIndex1, num cardIndex2) switch the card at index 1 with the card at index 2 return endClass

  35. Back to the CardStack • We don’t want the ability to swap any two cards in the stack class CardStack private num cards[52] public num draw() num card = the next card in the stack return card public void shuffle() swap random cards in the stack return private void swap(num cardIndex1, num cardIndex2) switch the card at index 1 with the card at index 2 return endClass

  36. Back to the Card Class • Which fields and methods do we want to protect? class Card num value // 0 – 51 num getRank() return (value % 13) + 1 // 1 – 13 num getSuit() return value / 13 // 0 – 3 boolisFaceCard return getRank() > 10 // true or false endClass

  37. Back to the Card Class • We don’t want the card to be changed class Card private num value // 0 – 51 num getRank() return (value % 13) + 1 // 1 – 13 num getSuit() return value / 13 // 0 – 3 boolisFaceCard return getRank() > 10 // true or false endClass

  38. Back to the Card Class • We do want to access the card’s rank class Card private num value // 0 – 51 public num getRank() return (value % 13) + 1 // 1 – 13 num getSuit() return value / 13 // 0 – 3 boolisFaceCard return getRank() > 10 // true or false endClass

  39. Back to the Card Class • We do want to access the card’s suit class Card private num value // 0 – 51 public num getRank() return (value % 13) + 1 // 1 – 13 public num getSuit() return value / 13 // 0 – 3 boolisFaceCard return getRank() > 10 // true or false endClass

  40. Back to the Card Class • We do want to know if it’s a face card class Card private num value // 0 – 51 public num getRank() return (value % 13) + 1 // 1 – 13 public num getSuit() return value / 13 // 0 – 3 publicboolisFaceCard return getRank() > 10 // true or false endClass

  41. Review Assume that a working program contains the following statements: Dog myDog myDog.setName(“Bowser”) Which of the following do you know? a) setName() is a public method of the Dog class b) setName() accepts a string parameter c) both of the above d) none of the above

  42. Review Assume that a working program contains the following statements: Dog myDog myDog.setName(“Bowser”) Which of the following do you know? a) setName() is a public method of the Dog class b) setName() accepts a string parameter c) both of the above d) none of the above

  43. Instance Methods • The methods we’ve seen so far are called instance methods • They operate on a particular instance of a class // Get the name from a specific instance // of the Employee class Employee manager manager.setName(newName) output manager.getName()

  44. But which instance? • From outside a method we reference an instance of a class using an identifier • The identifier determines the instance Employee manager output manager.getName()

  45. But which instance? class Employee private string name public string getName() return name endClass output manager.getName() output assistant.getName() • The compiler generates object code that knows which instance of name should be used when calling getName() manager - name assistant - name

  46. But which instance? • What if we want an instance to reference itself? • There’s no identifier defined within the class and there may be many instances of the class

  47. Use “this” one… • From within a method, you can reference the instance that is “in scope” using the “this” keyword class Employee private string name public string getName() return this.name void setName(string newName) this.name = newName return endClass

  48. Why do I care? • One example… class Employee private string name // What if a parameter identifier overlaps // a field identifier? void setName(string name) name = name return endClass

  49. “this” Resolves Ambiguity • One example… class Employee private string name // What if a parameter identifier overlaps // a field identifier? void setName(string name) this.name = name return endClass

  50. You’re right… • Why not just avoid using the same identifier? class Employee private string mName // What if a parameter identifier overlaps // a field identifier? void setName(string name) mName = name return endClass

More Related