Chapter 3 Objects and Classes Object Oriented Object Oriented Programming Programming � OOP is a relatively new approach to programming which supports the creation of new data types and operations to manipulate those types. 2 What is this Object ? What is this Object ? � There is no real answer to the question, but we’ll call it a “thinking cap”. � The plan is to describe a thinking cap by telling you what actions can be done to it. 3
Using the Object’s Using the Object’s Slots Slots � You may put a piece of paper in each of the two slots (green and red), with a sentence written on each. � You may push the green button and the thinking cap will speak the sentence from the green slot’s paper. � And same for the red button. 4 Example Example 5 Example Example That test was a breeze ! 6
Example Example I should study harder ! 7 Thinking Cap Thinking Cap Implementation Implementation public class � We can implement ThinkingCap the thinking cap { using a data type called a class. . . . } 8 Thinking Cap Thinking Cap Implementation Implementation � The class will have public class ThinkingCap two components { called greenWords String greenWords; and redWords. These String redWords; components are . . . strings which hold the information that is placed in the two } slots. � Using a class permits two features . . . 9
Thinking Cap Thinking Cap Implementation Implementation � The two components will be private instance public class ThinkingCap variables. This ensures { that nobody can directly private char greenWords; access this information. private char redWords; The only access is . . . through methods that } we provide for the class. 10 Thinking Cap Thinking Cap Implementation Implementation � In a class, the class ThinkingCap methods which { manipulate the private char greenWords; class are also private char redWords; listed. . . . } Implementations of Implementations of the thinking cap the thinking cap methods go here. methods go here. 11 Thinking Cap Thinking Cap Implementation Implementation Our thinking cap has at least three methods: Our thinking cap has at least three methods: public class ThinkingCap { private char greenWords; private char redWords; public void slots(String newGreen, String newRed)... public void pushGreen( )... public void pushRed( )... } 12
Thinking Cap Thinking Cap Implementation Implementation The code for a new class is generally put in a Java The code for a new class is generally put in a Java package, as shown here: package, as shown here: package myclassses.325; public class ThinkingCap This means that ThinkingCap.java and { ThinkingCap.class files private char greenWords; will be in a subdirectory private char redWords; myclasses/325 public void slots(String newGreen, String newRed)... public void pushGreen( )... public void pushRed( )... } 13 CLASSPATH Environment Variable Java searches for class files (including package files) in the directories referenced by the CLASSPATH environment variable 14 Using the Thinking Cap Using the Thinking Cap � A program that wants to use the thinking cap can import import myclasses.325.ThinkingCap; the ThinkingCap ... class. 15
Package Visibility Rules � Anything declared private can only be accessed from within the same class � Anything declared public can be accessed from anywhere inside or outside the package � Anything not declared public or private can be accessed from within the same package � Top-level classes cannot be declared private 16 Using the Thinking Cap Using the Thinking Cap � Just for fun, the example import myclasses.325.ThinkingCap; program will public class Example declare two { ThinkingCap public static void main( ) { variables named ThinkingCap student; student and fan. ThinkingCap fan; 17 Using the Thinking Cap Using the Thinking Cap � The variables are examples of reference import myclasses.325.ThinkingCap; variables, which public class Example means that they { have the capability public static void main( ) of referring to { ThinkingCap ThinkingCap student; ThinkingCap fan; objects that we create with the new student = new ThinkingCap( ); operator. fan = new ThinkingCap( ); 18
Using the Thinking Cap Using the Thinking Cap � Once the ThinkingCaps are created, import myclasses.325.ThinkingCap; we can public class Example activate { methods such public static void main( ) { as slots for the ThinkingCap student; student ThinkingCap fan; ThinkingCap. student = new ThinkingCap( ); fan = new ThinkingCap( ); student.slots( "Hello", "Bye"); 19 Using the Thinking Cap Using the Thinking Cap � The method activation consists of four parts, starting with the variable student . slots( "Hello", "Bye"); name. Name of the variable Name of the variable 20 Using the Thinking Cap Using the Thinking Cap The variable name is followed by a period. student . slots( "Hello", "Bye"); A Period A Period 21
Using the Thinking Cap Using the Thinking Cap � After the period is the name of the method that you are student . slots( "Hello", "Bye"); activating. N N a m a m e e o o f f t h t h e e m m e e t h t h o o d d 22 Using the Thinking Cap Using the Thinking Cap � Finally, the Arguments Arguments arguments for the method. In this example the first argument (newGreen) is student . slots( "Hello", "Bye" ); "Hello" and the second argument (newRed) is "Bye". 23 A Quiz A Quiz How would you public static void main(String[ ] args) activate student's { pushGreen method ? ThinkingCap student; ThinkingCap fan; What would be the student = new ThinkingCap( ); output of student's fan = new ThinkingCap( ); student.slots( "Hello", "Bye"); pushGreen method at this point in the program ? 24
A Quiz A Quiz Notice that the public static void main(String[ ] args) pushGreen method has { no arguments. ThinkingCap student; ThinkingCap fan; At this point, activating student = new ThinkingCap( ); student.pushGreen will fan = new ThinkingCap( ); student.slots( "Hello", "Bye"); print the string student.pushGreen( ); Hello . 25 A Quiz A Quiz Trace through this public static void main(String[ ] args) { program, and tell me ThinkingCap student; the complete output. ThinkingCap fan; student = new ThinkingCap( ); fan = new ThinkingCap( ); student.slots( "Hello", "Bye"); fan.slots( "Go Cougars!", "Boo!"); student.pushGreen( ); fan.pushGreen( ); student.pushRed( ); . . . 26 Thinking Cap Thinking Cap Implementation Implementation We will look at the body of slots, which must copy its We will look at the body of slots, which must copy its two arguments to the two private instance two arguments to the two private instance variables. variables. public class ThinkingCap { private String greenWords; private String redWords; public void slots(String newGreen, String newRed)... public void pushGreen( )... public void pushRed( )... } 27
Thinking Cap Thinking Cap Implementation Implementation The method’ ’s implementation occurs after the s implementation occurs after the The method parameter list parameter list public void slots(String newGreen, String newRed) { greenWords = newGreen; redWords = newRed; } 28 Thinking Cap Thinking Cap Implementation Implementation Within the body of the method, the class’s instance variables and other methods may all be accessed. public void slots(String newGreen, String newRed) { greenWords = newGreen; redWords = newRed; } 29 Thinking Cap Thinking Cap Implementation Implementation Within the body of the method, the class’ instance variables and other methods may all be accessed. But, whose instance variables are these? Are they public void slots(String newGreen, String newRed) student.greenWords { student.redWords greenWords = newGreen; fan.greenWords redWords = newRed; fan.redWords } ? 30
Thinking Cap Thinking Cap Implementation Implementation Within the body of the method, the class’s instance variables and other methods may all be accessed. If we activate student.slots: public void slots(String newGreen, String newRed) student.greenWords { student.redWords greenWords = newGreen; redWords = newRed; } 31 Thinking Cap Thinking Cap Implementation Implementation Here is the implementation of the pushGreen method, which prints the green Words: public void pushGreen( ) { System.out.println(greenWords); } Notice how this method implementation uses the Notice how this method implementation uses the greenWords instance variable of the object. greenWords instance variable of the object. 32 A Common Pattern A Common Pattern � Often, one or more methods will place data in the instance variables... public class ThinkingCap { private String greenWords; slots pushGreen & pushRed private String redWords; ... } � ...so that other methods may use that data. � ...so that other methods may use that data. 33
Recommend
More recommend