07 java api and inheritance

1,073 views

Published on

0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
1,073
On SlideShare
0
From Embeds
0
Number of Embeds
57
Actions
Shares
0
Downloads
12
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

07 java api and inheritance

  1. 1. Do You Remember the SimpleDotComGame?<br />LIS4930 © PIC<br />
  2. 2. What if we could do this?<br />LIS4930 © PIC<br />REPEAT with each of the location cells in the int array<br />//COMPARE the user guess to the location cell<br />IF the user guess matches<br />INCREMENT the number of hits<br />//FIND OUT if it was the last location cell:<br />IF number of hits is 3, RETURN “kill”<br />ELSE it was not a kill, so RETURN “hit”<br />END IF<br />ELSE user guess did not match, so RETURN “miss”<br />END IF<br />END REPEAT<br />REPEAT with each of the remaining location cells<br />//COMPARE the user guess to the location cell<br />IF the user guess matches<br />REMOVE this cell from the array<br />//FIND OUT if it was the last location cell:<br />IF the array is now empty RETURN “kill”<br />ELSE it was not a kill, so RETURN “hit”<br />END IF<br /> ELSE user guess did not match, so RETURN “miss”<br /> END IF<br />END REPEAT<br />
  3. 3. Welcome To The Java API!<br />LIS4930 © PIC<br />You don’t have to reinvent the wheel if you know how to find what you need in the Java library, known as the Java API (Application Programming Interface).<br />The core Java library is a giant pile of classes just waiting for you to use like building blocks, to assemble your own program out of largely pre-built code.<br />The Java API is full of code you don’t even have to type. All you need to do is learn to use it.<br />
  4. 4. The ArrayList class in the java.util package<br />LIS4930 © PIC<br />ArrayList<br />add(Object element)<br /> Adds the object parameter to the list<br />remove(int index)<br /> Removes the object at the index parameter<br />remove(Object element)<br /> Removes this object (if it’s in the ArrayList)<br />contains(Object element)<br />Returns ‘true’ if there’s a match for the object parameter<br />isEmpty()<br /> Returns ‘true’ if the list has no element<br />indexOf(Object element)<br /> Returns either the index of the object parameter, or -1<br />size()<br /> Returns the number of elements currently in the list<br />get(int index)<br /> Returns the object currently at the index parameter<br />
  5. 5. SO how is the ArrayList class different to regular arrays?<br />LIS4930 © PIC<br />CHECK OUT PAGE 136<br />
  6. 6. SO how is the ArrayList class different to regular arrays?<br />LIS4930 © PIC<br />1<br />2<br />3<br />4<br />A plain old array has to know its size at the time it’s created.<br />new String[2]<br />new ArrayList <String><br />To put an object in a regular array, you must assign it to a specific location.<br />myList[1] = b<br />myList.add(b)<br />Arrays use array syntax that’s not used anywhere else in Java.<br />myList[1]<br />ArrayLists in Java 5.0 are parameterized.<br />parameterized type<br />ArrayList <String><br />“a list of … Strings”<br />
  7. 7. How can we use the ArrayList class to improve our SimpleDotComGame?<br />LIS4930 © PIC<br />
  8. 8. Using the Library (the Java API)<br />LIS4930 © PIC<br />In the Java API, classes are grouped into packages.<br />To use a class in the API, you have to know which package the class is in.<br />Every class in the Java library belongs to a package!<br />ArrayList is in the package called java.util<br />To use a class from the Java API, you MUST indicate the full name of the library class you want to use, and that means package name + class name.<br />Even if you didn’t know it, you’ve already been using classes form a package. System (System.out.println), String, Math (Math.random()), and Integer (Integer.parseInt()). <br />
  9. 9. You have to know the FULL name of the class you want to use in your code.<br />LIS4930 © PIC<br />1<br />2<br />ArrayList is not the full name of ArrayList, just as ‘Kelly’ isn’t a full name. The full name of ArrayList is actually:<br />java.util.ArrayList<br />You have to tell Java which ArrayList you want to use. You have two options:<br />IMPORT<br />Put an import statement at the top of your source code file.<br />package name<br />class name<br />TYPE<br />Type the full name everywhere in your code. Each time you use it. Anywhere you use it.<br />
  10. 10. How to play with the API!<br />LIS4930 © PIC<br />What classes are in the library?<br />Once you find a class, how do you know what it can do?<br />1<br />2<br />Browse a book:<br />Use the HTML API docs:<br />http://java.sun.com/j2se/1.5.0/docs/api/<br />
  11. 11. LIS4930 © PIC<br />Chair Wars Revisited…<br />Larry’s Cube<br />(procedural approach)<br />Brad’s Café<br />(OOP approach)<br />rotate (shapeNum, xPt, yPt) {<br /> //if the shape is not an amoeba, <br /> //calculate the center point <br /> //based on a rectangle, then <br /> //rotate.<br /> //else <br /> //use the xPt and yPt as the <br /> //rotation point offset and <br /> //then rotate.<br />}<br />playSound (shapeNum) {<br /> //if the shape is not an <br /> //amoeba, use shapeNum<br /> //to lookup which AIF<br /> //sound to play, and play it <br /> //else<br /> //play amoeba .hif sound<br />}<br />Amoeba<br />intxPoint;<br />intyPoint;<br />rotate ( ) {<br /> //code to rotate an amoeba <br /> //using amoeba’s x and y<br />}<br />playSound ( ) {<br /> // code to play the <br /> // new .hif file for an<br /> // amoeba<br />}<br />
  12. 12. SO did Brad win the vacation? <br />LIS4930 © PIC<br />Triangle<br />rotate ( ) <br />playSound ( )<br />Well Larry didn’t go down without a fight and pointed out that Brad’s code had a lot of duplicated code in it…???<br />Ahaa but did Larry see Brad’s final design?<br />Square<br />rotate ( ) <br />playSound ( )<br />This is what Larry did…<br />1<br />rotate ( )<br />playSound ( )<br />Circle<br />Amoeba<br />Larry looked at what all four classes had in common.<br />rotate ( ) <br />playSound ( )<br />2<br />Shape<br />rotate ( )<br />playSound ( )<br />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.<br />
  13. 13. LIS4930 © PIC<br />3<br />Shape<br />rotate ( )<br />playSound ( )<br />superclass<br />Then Larry linked the other four classes to the new Shape class, in a relationship called inheritance.<br />subclasses<br />Circle<br />Amoeba<br />Square<br />Triangle<br />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 superclasscalled Shape.The other four are the subclassesof Shape. The subclasses inherit the methodsof the superclass. <br />
  14. 14. What about the Amoeba rotate()?<br />LIS4930 © PIC<br />4<br />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???<br />Shape<br />rotate ( )<br />playSound ( )<br />Overriding just means that a subclass redefines one of its inherited methods when it needs to change or extend the behavior of that method.<br />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.<br />Overriding methods<br />Amoeba<br />rotate ( ) {<br /> // code to rotate a <br /> //circle<br />}<br />playSound ( ) {<br /> // code to play the <br /> // new .hif file for an<br /> // amoeba<br />}<br />Circle<br />Square<br />Triangle<br />
  15. 15. Understanding Inheritance<br />LIS4930 © PIC<br />When one class inherits from another, the subclass inherits from the superclass.<br />In Java, we say that the subclass extends the superclass.<br />An inheritance relationship means that the subclass inherits the members (instance variables and methods) of the superclass.<br />SuperHero<br />suit<br />tights<br />specialPower<br />useSpecPow()<br />putOnSuit()<br />Overriding methods<br />PantherMan<br />useSpecPow()<br />putOnSuit()<br />FriedEggMan<br />
  16. 16. Let’s Look At Another Example<br />LIS4930 © PIC<br />
  17. 17. Design Steps of Inheritance (pages 170 -175)<br />LIS4930 © PIC<br />1<br />2<br />3<br />4<br />5<br />Look for objects that have common attributes and behaviors.<br />Design a class that represents the common state and behavior.<br />Decide if a subclass needs behaviors (method implementations) that are specific to that particular subclass type.<br />Look for more opportunities to use abstraction, by finding two or more subclasses that might need common behavior.<br />Finish the class hierarchy.<br />

×