SlideShare una empresa de Scribd logo
1 de 16
How Objects Can Change Your Life. LIS4930 © PIC ,[object Object],SPEC There will be shapes on a GUI, a square, a circle, and a triangle. When the user clicks on a shape, the shape will rotate clockwise 360° and play an AIF sound file specific to that particular shape.
LIS4930 © PIC Larry’s Cube (procedural approach) Brad’s Café (OOP approach) rotate (shapeNum) { // make the shape rotate  // 360° } playSound (shapeNum) { //use shapeNum to lookup  // which AIF sound to play,  //and play it } Square rotate ( ) { // code to rotate a  //square } playSound ( ) { // code to play the  // AIF file for a  // square } rotate ( ) { // code to // rotate a triangle } playSound ( ) { // code to play the  // AIF file for a triangle } Triangle rotate ( ) { // code to rotate a  //circle } playSound ( ) { // code to play the  // AIF file for a  // circle } Circle Which do you think is better?
Hold the Press!  There’s a spec change. LIS4930 © PIC SPEC Change There will be an amoeba shape on the screen, with the others. When the user clicks on the amoeba, it will rotate like the others, and play a .hif sound file.
LIS4930 © PIC Larry’s Cube (procedural approach) Brad’s Café (OOP approach) rotate (shapeNum) { // make the shape rotate  // 360° } playSound (shapeNum) { //if the shape is not an  //amoeba, use shapeNum  //to lookup which AIF //sound to play, and play it  //else //play amoeba .hif sound } Amoeba Square rotate ( ) { // code to rotate a  //square } playSound ( ) { // code to play the  // AIF file for a  // square } rotate ( ) { // code to // rotate a triangle } playSound ( ) { // code to play the  // AIF file for a triangle } Triangle rotate ( ) { // code to rotate a  //circle } playSound ( ) { // code to play the  // AIF file for a  // circle } Circle rotate ( ) { // code to rotate a  //circle } playSound ( ) { // code to play the  // new .hif file for an // amoeba } Why is it not a good idea to edit previously tested code?
What the Spec Forgot to Mention… LIS4930 © PIC Amoeba rotation point in Larry and Brad’s version. Where the amoeba rotation point should be. SPEC Change
LIS4930 © PIC Larry’s Cube (procedural approach) Brad’s Café (OOP approach) rotate (shapeNum, xPt, yPt) { //if the shape is not an amoeba,  //calculate the center point  //based on a rectangle, then  //rotate. //else  //use the xPt and yPt as the  //rotation point offset and  //then rotate. } playSound (shapeNum) { //if the shape is not an  //amoeba, use shapeNum  //to lookup which AIF //sound to play, and play it  //else //play amoeba .hif sound } int xPoint; int yPoint; rotate ( ) { //code to rotate an amoeba  //using amoeba’s x and y } playSound ( ) { // code to play the  // new .hif file for an // amoeba } Amoeba Which approach is more efficient? Changing one class or editing the entire file?
SO did Brad win the vacation?  LIS4930 © PIC Well Larry didn’t go down without a fight and pointed out that Brad’s code had a lot of duplicated code in it…??? Ahaa but did Larry see Brad’s final design? This is what Brad did… Square rotate ( )  playSound ( ) rotate ( )  playSound ( ) Triangle rotate ( )  playSound ( ) Circle rotate ( ) playSound ( ) Amoeba Larry looked at what all four classes had in common. 1 2 They’re all shapes, and they all rotate and playSound. So Larry abstracted out the common features and put them into a new class called Shape. Shape rotate ( ) playSound ( )
LIS4930 © PIC Then Larry linked the other four classes to the new Shape class, in a relationship called  inheritance . Shape rotate ( ) playSound ( ) superclass You can read this as, “Square inherits from Shape”, “Circle inherits from Shape”, and so on. rotate() and playSound() have been taken out of all other shapes and replaced by one copy in a  superclass  called Shape.   The other four are the  subclasses   of Shape. The subclasses  inherit  the  methods   of the superclass.  Square Triangle Circle Amoeba 3 subclasses
What about the Amoeba rotate()? LIS4930 © PIC Wasn’t that the whole problem here – that the amoeba shape had a completely different rotate and playSound procedure? How can Amoeba do something different if it “inherits” its functionality from the Shape class??? Shape rotate ( ) playSound ( ) Square Triangle Circle rotate ( ) { // code to rotate a  //circle } playSound ( ) { // code to play the  // new .hif file for an // amoeba } Amoeba 4 The Amoeba class  overrides  the methods of the Shape class. Then at runtime, the JVM knows exactly which rotate() method to run when someone tells the Amoeba to rotate. Overriding just means that a subclass redefines one of its inherited methods when it needs to change or extend the behavior of that method. Overriding methods
Back to Classes LIS4930 © PIC ,[object Object],[object Object],[object Object],Shopping Cart cartContents addToCart() removeFromCart() checkCart() knows does Button label color setColor() setLabel() pressOn() pressOff() knows does Alarm alarmTime alarmMode getAlarmTime() setAlarmTime() isAlarmSet() snooze() knows does Remember a class is the  blueprint   for an object!
LIS4930 © PIC ,[object Object],[object Object],[object Object],[object Object],instance variables  (state) methods  (behavior) Song knows does What could go here? What could go here? Why are they called “instance” variables? What does “state” mean?
Making Your First Object LIS4930 © PIC Write your class 1 class Dog { int size; String breed; String name; void bark() { System.out.println(“Woof! Woof!”); } } Dog size breed name bark() instance variables methods Write a tester (TestDrive) class 2 class DogTestDrive { public static void main (String [ ] args) { //Dog test code goes here } }
Making Your First Object LIS4930 © PIC In your test, make an object and access the object’s variables and methods 3 class DogTestDrive { public static void main (String [ ] args) { Dog rufus = new Dog(); rufus.size = 40; rufus.breed = “cockapoo”; rufus.name = “Rufus”; rufus.bark(); } } rufus size = 40 breed = cockapoo name = Rufus bark() Make a new Dog object named “rufus” Use the dot operator (.) to set the size, breed, and name of rufus. Use the dot operator (.) to call the rufus’ bark() method. If you already have some OOP experience, you might notice we are not using encapsulation. We’ll get to that in chapter 4 of the text.
Another Example of Building a Class and Testing it. LIS4930 © PIC class Movie { String title; String genre; int rating; void playIt() { System.out.println( “ Playing the movie”); } } public class MovieTestDrive { public static void main(String [ ] args) { Movie one = new Movie(); one.title = “Gone with the Stock”; one.genre = “Tragic”; one.rating =  - 2; Movie two = new Movie(); two.title = “Lost in Cubicle Space”; two.title = “Comedy”; two.rating = 5; two.playIt(); Movie three = new Movie(); three.title = “Byte Club”; three.genre = “Tragic but ultimately  uplifting”; three.rating = 100; } } class tester Why is there no main()?
Escape from Main(e)!?! LIS4930 © PIC As long as you are still in main() you are not truly using OOP design. It’s fine for a test program to run within the main method, but in a true OO application, you need objects talking to other objects, as opposed to a static main() methods creating and testing objects. The two uses of main: ,[object Object],[object Object],A real Java application is nothing but objects talking to other objects. In this case,  talking  means objects calling methods on one another. As a ‘sneak preview’, though, of how a real Java application might behave, here’s a little example.  The Guessing Game Summary The guessing game involves a ‘game’ object and three ‘player’ objects. The game generates a random number between 0 and 9, and the three player objects try to guess it.  Classes GuessGame.class Player.class GameLauncher.class
Java Takes Out The LIS4930 © PIC Trash Each time an object is created in Java, it goes into an area of memory known as  The Heap.  All objects – no matter when, where, or how they’re created – live on the heap. But it’s not just any old memory heap; the java heap is actually called the  Garbage-Collection Heap.  When you create an object, Java allocates memory space on the heap according to how much that particular object needs. An object with, say, 15 instance variables, will probably need more space than an object with only two instance variables.  But what happens when you need to reclaim that space?  How do you get an object out of the heap when you’re done with it? Java manages that memory for you!

Más contenido relacionado

Similar a 03 objects

Introduction to Coding
Introduction to CodingIntroduction to Coding
Introduction to CodingFabio506452
 
Introduction to humanoid robot iCub, YARP and simulator
Introduction to humanoid robot iCub, YARP and simulatorIntroduction to humanoid robot iCub, YARP and simulator
Introduction to humanoid robot iCub, YARP and simulatorMartin Peniak
 
Gdc09 Minigames
Gdc09 MinigamesGdc09 Minigames
Gdc09 MinigamesSusan Gold
 
05 - Bypassing DEP, or why ASLR matters
05 - Bypassing DEP, or why ASLR matters05 - Bypassing DEP, or why ASLR matters
05 - Bypassing DEP, or why ASLR mattersAlexandre Moneger
 
What's New in ES6 for Web Devs
What's New in ES6 for Web DevsWhat's New in ES6 for Web Devs
What's New in ES6 for Web DevsRami Sayar
 
Explaining ES6: JavaScript History and What is to Come
Explaining ES6: JavaScript History and What is to ComeExplaining ES6: JavaScript History and What is to Come
Explaining ES6: JavaScript History and What is to ComeCory Forsyth
 
Fitc whats new in es6 for web devs
Fitc   whats new in es6 for web devsFitc   whats new in es6 for web devs
Fitc whats new in es6 for web devsFITC
 
Intro to Open Sim for Second Lifers
Intro to Open Sim for Second LifersIntro to Open Sim for Second Lifers
Intro to Open Sim for Second Liferspabloatlansingcom
 
[EN] Ada Lovelace Day 2014 - Tampon run
[EN] Ada Lovelace Day 2014  - Tampon run[EN] Ada Lovelace Day 2014  - Tampon run
[EN] Ada Lovelace Day 2014 - Tampon runMaja Kraljič
 
03 introduction to graph databases
03   introduction to graph databases03   introduction to graph databases
03 introduction to graph databasesNeo4j
 
JavaScript - Programming Languages course
JavaScript - Programming Languages course JavaScript - Programming Languages course
JavaScript - Programming Languages course yoavrubin
 
The things we don't see – stories of Software, Scala and Akka
The things we don't see – stories of Software, Scala and AkkaThe things we don't see – stories of Software, Scala and Akka
The things we don't see – stories of Software, Scala and AkkaKonrad Malawski
 
Building native Android applications with Mirah and Pindah
Building native Android applications with Mirah and PindahBuilding native Android applications with Mirah and Pindah
Building native Android applications with Mirah and PindahNick Plante
 

Similar a 03 objects (20)

12 constructors
12 constructors12 constructors
12 constructors
 
Introduction to Coding
Introduction to CodingIntroduction to Coding
Introduction to Coding
 
Introduction to humanoid robot iCub, YARP and simulator
Introduction to humanoid robot iCub, YARP and simulatorIntroduction to humanoid robot iCub, YARP and simulator
Introduction to humanoid robot iCub, YARP and simulator
 
Go Java, Go!
Go Java, Go!Go Java, Go!
Go Java, Go!
 
Go Java, Go!
Go Java, Go!Go Java, Go!
Go Java, Go!
 
Gdc09 Minigames
Gdc09 MinigamesGdc09 Minigames
Gdc09 Minigames
 
05 - Bypassing DEP, or why ASLR matters
05 - Bypassing DEP, or why ASLR matters05 - Bypassing DEP, or why ASLR matters
05 - Bypassing DEP, or why ASLR matters
 
Ontopia tutorial
Ontopia tutorialOntopia tutorial
Ontopia tutorial
 
The_Borrow_Checker.pdf
The_Borrow_Checker.pdfThe_Borrow_Checker.pdf
The_Borrow_Checker.pdf
 
What's New in ES6 for Web Devs
What's New in ES6 for Web DevsWhat's New in ES6 for Web Devs
What's New in ES6 for Web Devs
 
Explaining ES6: JavaScript History and What is to Come
Explaining ES6: JavaScript History and What is to ComeExplaining ES6: JavaScript History and What is to Come
Explaining ES6: JavaScript History and What is to Come
 
Symfony 4 & Flex news
Symfony 4 & Flex newsSymfony 4 & Flex news
Symfony 4 & Flex news
 
Fitc whats new in es6 for web devs
Fitc   whats new in es6 for web devsFitc   whats new in es6 for web devs
Fitc whats new in es6 for web devs
 
Intro to Open Sim for Second Lifers
Intro to Open Sim for Second LifersIntro to Open Sim for Second Lifers
Intro to Open Sim for Second Lifers
 
[EN] Ada Lovelace Day 2014 - Tampon run
[EN] Ada Lovelace Day 2014  - Tampon run[EN] Ada Lovelace Day 2014  - Tampon run
[EN] Ada Lovelace Day 2014 - Tampon run
 
03 introduction to graph databases
03   introduction to graph databases03   introduction to graph databases
03 introduction to graph databases
 
JavaScript - Programming Languages course
JavaScript - Programming Languages course JavaScript - Programming Languages course
JavaScript - Programming Languages course
 
Dependency Injection
Dependency InjectionDependency Injection
Dependency Injection
 
The things we don't see – stories of Software, Scala and Akka
The things we don't see – stories of Software, Scala and AkkaThe things we don't see – stories of Software, Scala and Akka
The things we don't see – stories of Software, Scala and Akka
 
Building native Android applications with Mirah and Pindah
Building native Android applications with Mirah and PindahBuilding native Android applications with Mirah and Pindah
Building native Android applications with Mirah and Pindah
 

Más de Program in Interdisciplinary Computing (20)

Phpmysqlcoding
PhpmysqlcodingPhpmysqlcoding
Phpmysqlcoding
 
Database basics
Database basicsDatabase basics
Database basics
 
CGS2835 HTML5
CGS2835 HTML5CGS2835 HTML5
CGS2835 HTML5
 
Mysocial databasequeries
Mysocial databasequeriesMysocial databasequeries
Mysocial databasequeries
 
Mysocial databasequeries
Mysocial databasequeriesMysocial databasequeries
Mysocial databasequeries
 
CGS2835 HTML5
CGS2835 HTML5CGS2835 HTML5
CGS2835 HTML5
 
01 intro tousingjava
01 intro tousingjava01 intro tousingjava
01 intro tousingjava
 
Web architecture v3
Web architecture v3Web architecture v3
Web architecture v3
 
Xhtml
XhtmlXhtml
Xhtml
 
Webdev
WebdevWebdev
Webdev
 
Web architecture
Web architectureWeb architecture
Web architecture
 
Javascript
JavascriptJavascript
Javascript
 
Javascript
JavascriptJavascript
Javascript
 
Html5
Html5Html5
Html5
 
Frameworks
FrameworksFrameworks
Frameworks
 
Drupal
DrupalDrupal
Drupal
 
Database
DatabaseDatabase
Database
 
Javascript2
Javascript2Javascript2
Javascript2
 
11 polymorphism
11 polymorphism11 polymorphism
11 polymorphism
 
13 interfaces
13 interfaces13 interfaces
13 interfaces
 

03 objects

  • 1.
  • 2. LIS4930 © PIC Larry’s Cube (procedural approach) Brad’s Café (OOP approach) rotate (shapeNum) { // make the shape rotate // 360° } playSound (shapeNum) { //use shapeNum to lookup // which AIF sound to play, //and play it } Square rotate ( ) { // code to rotate a //square } playSound ( ) { // code to play the // AIF file for a // square } rotate ( ) { // code to // rotate a triangle } playSound ( ) { // code to play the // AIF file for a triangle } Triangle rotate ( ) { // code to rotate a //circle } playSound ( ) { // code to play the // AIF file for a // circle } Circle Which do you think is better?
  • 3. Hold the Press! There’s a spec change. LIS4930 © PIC SPEC Change There will be an amoeba shape on the screen, with the others. When the user clicks on the amoeba, it will rotate like the others, and play a .hif sound file.
  • 4. LIS4930 © PIC Larry’s Cube (procedural approach) Brad’s Café (OOP approach) rotate (shapeNum) { // make the shape rotate // 360° } playSound (shapeNum) { //if the shape is not an //amoeba, use shapeNum //to lookup which AIF //sound to play, and play it //else //play amoeba .hif sound } Amoeba Square rotate ( ) { // code to rotate a //square } playSound ( ) { // code to play the // AIF file for a // square } rotate ( ) { // code to // rotate a triangle } playSound ( ) { // code to play the // AIF file for a triangle } Triangle rotate ( ) { // code to rotate a //circle } playSound ( ) { // code to play the // AIF file for a // circle } Circle rotate ( ) { // code to rotate a //circle } playSound ( ) { // code to play the // new .hif file for an // amoeba } Why is it not a good idea to edit previously tested code?
  • 5. What the Spec Forgot to Mention… LIS4930 © PIC Amoeba rotation point in Larry and Brad’s version. Where the amoeba rotation point should be. SPEC Change
  • 6. LIS4930 © PIC Larry’s Cube (procedural approach) Brad’s Café (OOP approach) rotate (shapeNum, xPt, yPt) { //if the shape is not an amoeba, //calculate the center point //based on a rectangle, then //rotate. //else //use the xPt and yPt as the //rotation point offset and //then rotate. } playSound (shapeNum) { //if the shape is not an //amoeba, use shapeNum //to lookup which AIF //sound to play, and play it //else //play amoeba .hif sound } int xPoint; int yPoint; rotate ( ) { //code to rotate an amoeba //using amoeba’s x and y } playSound ( ) { // code to play the // new .hif file for an // amoeba } Amoeba Which approach is more efficient? Changing one class or editing the entire file?
  • 7. SO did Brad win the vacation? LIS4930 © PIC Well Larry didn’t go down without a fight and pointed out that Brad’s code had a lot of duplicated code in it…??? Ahaa but did Larry see Brad’s final design? This is what Brad did… Square rotate ( ) playSound ( ) rotate ( ) playSound ( ) Triangle rotate ( ) playSound ( ) Circle rotate ( ) playSound ( ) Amoeba Larry looked at what all four classes had in common. 1 2 They’re all shapes, and they all rotate and playSound. So Larry abstracted out the common features and put them into a new class called Shape. Shape rotate ( ) playSound ( )
  • 8. LIS4930 © PIC Then Larry linked the other four classes to the new Shape class, in a relationship called inheritance . Shape rotate ( ) playSound ( ) superclass You can read this as, “Square inherits from Shape”, “Circle inherits from Shape”, and so on. rotate() and playSound() have been taken out of all other shapes and replaced by one copy in a superclass called Shape. The other four are the subclasses of Shape. The subclasses inherit the methods of the superclass. Square Triangle Circle Amoeba 3 subclasses
  • 9. What about the Amoeba rotate()? LIS4930 © PIC Wasn’t that the whole problem here – that the amoeba shape had a completely different rotate and playSound procedure? How can Amoeba do something different if it “inherits” its functionality from the Shape class??? Shape rotate ( ) playSound ( ) Square Triangle Circle rotate ( ) { // code to rotate a //circle } playSound ( ) { // code to play the // new .hif file for an // amoeba } Amoeba 4 The Amoeba class overrides the methods of the Shape class. Then at runtime, the JVM knows exactly which rotate() method to run when someone tells the Amoeba to rotate. Overriding just means that a subclass redefines one of its inherited methods when it needs to change or extend the behavior of that method. Overriding methods
  • 10.
  • 11.
  • 12. Making Your First Object LIS4930 © PIC Write your class 1 class Dog { int size; String breed; String name; void bark() { System.out.println(“Woof! Woof!”); } } Dog size breed name bark() instance variables methods Write a tester (TestDrive) class 2 class DogTestDrive { public static void main (String [ ] args) { //Dog test code goes here } }
  • 13. Making Your First Object LIS4930 © PIC In your test, make an object and access the object’s variables and methods 3 class DogTestDrive { public static void main (String [ ] args) { Dog rufus = new Dog(); rufus.size = 40; rufus.breed = “cockapoo”; rufus.name = “Rufus”; rufus.bark(); } } rufus size = 40 breed = cockapoo name = Rufus bark() Make a new Dog object named “rufus” Use the dot operator (.) to set the size, breed, and name of rufus. Use the dot operator (.) to call the rufus’ bark() method. If you already have some OOP experience, you might notice we are not using encapsulation. We’ll get to that in chapter 4 of the text.
  • 14. Another Example of Building a Class and Testing it. LIS4930 © PIC class Movie { String title; String genre; int rating; void playIt() { System.out.println( “ Playing the movie”); } } public class MovieTestDrive { public static void main(String [ ] args) { Movie one = new Movie(); one.title = “Gone with the Stock”; one.genre = “Tragic”; one.rating = - 2; Movie two = new Movie(); two.title = “Lost in Cubicle Space”; two.title = “Comedy”; two.rating = 5; two.playIt(); Movie three = new Movie(); three.title = “Byte Club”; three.genre = “Tragic but ultimately uplifting”; three.rating = 100; } } class tester Why is there no main()?
  • 15.
  • 16. Java Takes Out The LIS4930 © PIC Trash Each time an object is created in Java, it goes into an area of memory known as The Heap. All objects – no matter when, where, or how they’re created – live on the heap. But it’s not just any old memory heap; the java heap is actually called the Garbage-Collection Heap. When you create an object, Java allocates memory space on the heap according to how much that particular object needs. An object with, say, 15 instance variables, will probably need more space than an object with only two instance variables. But what happens when you need to reclaim that space? How do you get an object out of the heap when you’re done with it? Java manages that memory for you!

Notas del editor

  1. Demo what we have learned so far: conditional statements; loops; variables; objects; inputs; web