SlideShare ist ein Scribd-Unternehmen logo
1 von 17
Do You Remember the SimpleDotComGame? LIS4930 © PIC
What if we could do this? LIS4930 © PIC REPEAT with each of the location cells in the int array //COMPARE the user guess to the location cell IF the user guess matches INCREMENT the number of hits //FIND OUT if it was the last location cell: IF number of hits is 3, RETURN “kill” ELSE it was not a kill, so RETURN “hit” END IF ELSE user guess did not match, so RETURN “miss” END IF END REPEAT REPEAT with each of the remaining location cells //COMPARE the user guess to the location cell IF the user guess matches REMOVE this cell from the array //FIND OUT if it was the last location cell: IF the array is now empty RETURN “kill” ELSE it was not a kill, so RETURN “hit” END IF 	ELSE user guess did not match, so RETURN “miss” 	END IF END REPEAT
Welcome To The Java API! LIS4930 © PIC 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). 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. The Java API is full of code you don’t even have to type. All you need to do is learn to use it.
The ArrayList class in the java.util package LIS4930 © PIC ArrayList add(Object element) 	Adds the object parameter to the list remove(int index) 	Removes the object at the index parameter remove(Object element) 	Removes this object (if it’s in the ArrayList) contains(Object element) Returns ‘true’ if there’s a match for the object parameter isEmpty() 	Returns ‘true’ if the list has no element indexOf(Object element) 	Returns either the index of the object parameter, or -1 size() 	Returns the number of elements currently in the list get(int index) 	Returns the object currently at the index parameter
SO how is the ArrayList class different to regular arrays? LIS4930 © PIC CHECK OUT PAGE 136
SO how is the ArrayList class different to regular arrays? LIS4930 © PIC 1 2 3 4 A plain old array has to know its size at the time it’s created. new String[2] new ArrayList <String> To put an object in a regular array, you must assign it to a specific location. myList[1] = b myList.add(b) Arrays use array syntax that’s not used anywhere else in Java. myList[1] ArrayLists in Java 5.0 are parameterized. parameterized type ArrayList <String> “a list of … Strings”
How can we use the ArrayList class to improve  our SimpleDotComGame? LIS4930 © PIC
Using the Library (the Java API) LIS4930 © PIC In the Java API, classes are grouped into packages. To use a class in the API, you have to know which package the class is in. Every class in the Java library belongs to a package! ArrayList is in the package called java.util 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. 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()).
You have to know the FULL name of the class you want to use in your code. LIS4930 © PIC 1 2 ArrayList is not the full name of ArrayList, just as ‘Kelly’ isn’t a full name. The full name of ArrayList is actually: java.util.ArrayList You have to tell Java which ArrayList you want to use. You have two options: IMPORT Put an import statement at the top of your source code file. package name class name TYPE Type the full name everywhere in your code. Each time you use it. Anywhere you use it.
How to play with the API! LIS4930 © PIC What classes are in the library? Once you  find a class, how do you know what it can do? 1 2 Browse a book: Use the HTML API docs: http://java.sun.com/j2se/1.5.0/docs/api/
LIS4930 © PIC Chair Wars Revisited… 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 } Amoeba intxPoint; intyPoint; rotate ( ) { 	//code to rotate an amoeba  	//using amoeba’s x and y } playSound ( ) { 	// code to play the  	// new .hif file for an 	// amoeba }
SO did Brad win the vacation?  LIS4930 © PIC Triangle rotate ( )  playSound ( ) 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? Square rotate ( )  playSound ( ) This is what Larry did… 1 rotate ( ) playSound ( ) Circle Amoeba Larry looked at what all four classes had in common. rotate ( )  playSound ( ) 2 Shape rotate ( ) playSound ( ) 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.
LIS4930 © PIC 3 Shape rotate ( ) playSound ( ) superclass Then Larry linked the other four classes to the new Shape class, in a relationship called inheritance. subclasses Circle Amoeba Square Triangle 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.
What about the Amoeba rotate()? LIS4930 © PIC 4 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 ( ) Overriding just means that a subclass redefines one of its inherited methods when it needs to change or extend the behavior of that method. 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 methods Amoeba rotate ( ) { 	// code to rotate a  	//circle } playSound ( ) { 	// code to play the  	// new .hif file for an 	// amoeba } Circle Square Triangle
Understanding Inheritance LIS4930 © PIC When one class inherits from another, the subclass inherits from the superclass. In Java, we say that the subclass extends  the superclass. An inheritance relationship means that the subclass inherits the members (instance variables and methods) of the superclass. SuperHero suit tights specialPower useSpecPow() putOnSuit() Overriding methods PantherMan useSpecPow() putOnSuit() FriedEggMan
Let’s Look At Another Example LIS4930 © PIC
Design Steps of Inheritance (pages 170 -175) LIS4930 © PIC 1 2 3 4 5 Look for objects that have common attributes and behaviors. Design a class that represents the common state and behavior. Decide if a subclass needs behaviors (method implementations) that are specific to that particular subclass type. Look for more opportunities to use abstraction, by finding two or more subclasses that might need common behavior. Finish the class hierarchy.

Weitere ähnliche Inhalte

Andere mochten auch (9)

14a exceptions
14a exceptions14a exceptions
14a exceptions
 
Sdlc
SdlcSdlc
Sdlc
 
04 variables
04 variables04 variables
04 variables
 
Web architecture v3
Web architecture v3Web architecture v3
Web architecture v3
 
Mysocial
MysocialMysocial
Mysocial
 
06a methods original
06a methods original06a methods original
06a methods original
 
03 objects
03 objects03 objects
03 objects
 
12 abstract classes
12 abstract classes12 abstract classes
12 abstract classes
 
15a gui
15a gui15a gui
15a gui
 

Ähnlich wie 07 java api and inheritance

Read carefully and follow exactly Java You are to write a Breakou.pdf
Read carefully and follow exactly Java You are to write a Breakou.pdfRead carefully and follow exactly Java You are to write a Breakou.pdf
Read carefully and follow exactly Java You are to write a Breakou.pdf
rupeshmehta151
 
computer notes - Data Structures - 5
computer notes - Data Structures - 5computer notes - Data Structures - 5
computer notes - Data Structures - 5
ecomputernotes
 
Intro to scala
Intro to scalaIntro to scala
Intro to scala
Joe Zulli
 

Ähnlich wie 07 java api and inheritance (20)

Collections
CollectionsCollections
Collections
 
13 interfaces
13 interfaces13 interfaces
13 interfaces
 
11 interfaces
11 interfaces11 interfaces
11 interfaces
 
Preparation Data Structures 05 chain linear_list
Preparation Data Structures 05 chain linear_listPreparation Data Structures 05 chain linear_list
Preparation Data Structures 05 chain linear_list
 
Read carefully and follow exactly Java You are to write a Breakou.pdf
Read carefully and follow exactly Java You are to write a Breakou.pdfRead carefully and follow exactly Java You are to write a Breakou.pdf
Read carefully and follow exactly Java You are to write a Breakou.pdf
 
Scala - brief intro
Scala - brief introScala - brief intro
Scala - brief intro
 
03 Objects
03 Objects03 Objects
03 Objects
 
Design patterns(red)
Design patterns(red)Design patterns(red)
Design patterns(red)
 
Java Generics
Java GenericsJava Generics
Java Generics
 
Array properties
Array propertiesArray properties
Array properties
 
01-intro_stacks.ppt
01-intro_stacks.ppt01-intro_stacks.ppt
01-intro_stacks.ppt
 
computer notes - Data Structures - 5
computer notes - Data Structures - 5computer notes - Data Structures - 5
computer notes - Data Structures - 5
 
Polymorphism in C# Function overloading in C#
Polymorphism in C# Function overloading in C#Polymorphism in C# Function overloading in C#
Polymorphism in C# Function overloading in C#
 
Intro to scala
Intro to scalaIntro to scala
Intro to scala
 
Object Oriented Programming - 7.2. Polymorphism
Object Oriented Programming - 7.2. PolymorphismObject Oriented Programming - 7.2. Polymorphism
Object Oriented Programming - 7.2. Polymorphism
 
Collections and generic class
Collections and generic classCollections and generic class
Collections and generic class
 
Actionscript 3 - Session 4 Core Concept
Actionscript 3 - Session 4 Core ConceptActionscript 3 - Session 4 Core Concept
Actionscript 3 - Session 4 Core Concept
 
Internet programming slide - java.ppt
Internet programming slide - java.pptInternet programming slide - java.ppt
Internet programming slide - java.ppt
 
Computer notes - Josephus Problem
Computer notes - Josephus ProblemComputer notes - Josephus Problem
Computer notes - Josephus Problem
 
Ruby Programming Assignment Help
Ruby Programming Assignment HelpRuby Programming Assignment Help
Ruby Programming Assignment Help
 

Mehr von 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
 
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
 
15b more gui
15b more gui15b more gui
15b more gui
 
14b exceptions
14b exceptions14b exceptions
14b exceptions
 

07 java api and inheritance

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