Presentation is loading. Please wait.

Presentation is loading. Please wait.

CMSC 202 Classes and Objects.

Similar presentations


Presentation on theme: "CMSC 202 Classes and Objects."— Presentation transcript:

1 CMSC 202 Classes and Objects

2 Copyright © 2008 Pearson Addison-Wesley.
Class Definitions You already know how to use classes and the objects created from them, and how to invoke their methods For example, you have already been using the predefined String and Scanner classes Now you will learn how to define your own classes and their methods, and how to create your own objects from them Aug 6, 2007 Copyright © 2008 Pearson Addison-Wesley. All rights reserved 2

3 Copyright © 2008 Pearson Addison-Wesley.
A Class Is a Type A class is a special kind of programmer-defined type, and variables can be declared of a class type (recall structs from C) A value of a class type is called an object or an instance of the class If A is a class, then the phrases “X is of type A“ “X is an object of the class A" “X is an instance of the class A" mean the same thing A class determines the types of data that an object can contain, as well as the actions it can perform Aug 6, 2007 Copyright © 2008 Pearson Addison-Wesley. All rights reserved 3

4 The Contents of a Class Definition
A class definition specifies the data items and methods that all of its objects will have These data items and methods are sometimes called members of the object Data items are called fields or instance variables Instance variable declarations and method definitions can be placed in any order within the class definition Aug 6, 2007 Copyright © 2008 Pearson Addison-Wesley. All rights reserved 4

5 Primitive Type Values vs. Class Type Values
A primitive type value (int, double, char) is a single piece of data A class type value or object can have multiple pieces of data, as well as actions called methods All objects of a class have the same methods All objects of a class have the same pieces of data (i.e., name, type, and number) For a given object, each piece of data can hold a different value Aug 6, 2007 Copyright © 2008 Pearson Addison-Wesley. All rights reserved 5

6 Anatomy of a Java Class public class
Visibility modifier (More on this later) Keyword class Name of the class public class Date1 { Class body: instance variables, methods } NO semi-colon Aug 6, 2007

7 Instance Variables are defined inside the class definition.
may be primitive types or other class types are accessible by all methods of the class i.e. have “class scope” Aug 6, 2007

8 Anatomy of a method Class methods are very much like functions. Methods include a visibility modifier, return type, method name, and optional parameters Name of the method Visibility modifier (More on this later) return type Optional parameters public double toCelcius (double fTemp) { Method code: local variables and statements }

9 A Date Class The code below defines a class named Date1.
This class definition goes in a file named Date1.java public class Date1 { public String month; public int day; public int year; public void print( ) System.out.println(month + “ “ + day + “, “ + year); } These are the (public)“data members” or “instance variables” of the class This is a method definition and its implementation A method may use the class’ instance variables Aug 6, 2007

10 Notes on Date1 Class print is a method of the Date1 class. It’s definition and implementation are part of the Date1 class. Like functions in C, class methods may be void, return a value, and (optionally) have parameters. Method parameters may be primitive types passed by value or may be objects (which need further discussion later). Class methods have access to the class’ instance variables. In Java, the name of the .java file must match the name of the class defined within in. Aug 6, 2007

11 Create a Date1 object named myDate
Using Date1 We can create instances of Date1, modify, and print the date. This class definition (program) goes in a file named Date1Demo.java public class Date1Demo { public static void main( String[ ] args ) { Date1 myDate; myDate = new Date1( ); myDate.month = “July”; myDate.day = 4; myDate.year = 2007; myDate.print( ); } Create a Date1 object named myDate Give values to the data members Invoke the print method Aug 6, 2007

12 Notes on Date1Demo The statement Date1 myDate; defines a variable of type Date1. But there is no Date1 object yet! The statement myDate = new Date1( ); creates a “new” Date1 object and names it with the variable “myDate”. Now “myDate” refers to a Date1 object For convenience, these statements can be combined Date1 myDate = new Date1( ); In general, a statement of the form ClassVariable = new ClassName( ); creates a new object of the specified class and associates it with the class variable. (continued) Aug 6, 2007

13 Notes on Date1Demo Public instance variables of an object are referenced using the “dot” operator (like members of a struct in C) myDate.month = “July”; myDate.day = 4; myDate.year = 2007; Instance variable can be used like any other variable of the same type. The set of values stored in all instance variables define the state of the myDate object. (continued) Aug 6, 2007

14 Notes on Date1Demo The print method of myDate is invoked using the “dot” operator (again similar to accessing a member in a C struct). The statement myDate.print( ); invokes the print method of myDate which refers to an object of type Date1. In OO terminology we say that we are “sending the print message” to the object referred to by myDate. The object myDate is referred to as the “calling object” or “host object”. It is the object in which the print method is invoked. Aug 6, 2007

15 Methods with primitive parameters
The print method of Date1 has no parameters, but like functions in C, methods may have primitive parameters as shown by the new methods on the following slide. Aug 6, 2007

16 New Date1 Methods // change the month (using an int), day, and year.
public void setDate( int newMonth, int newDay, int newYear ) { month = monthString( newMonth ); day = newDay; year = newYear; } // change month number (int) to string public String monthString( int monthNumber ) { switch ( monthNumber ) { case 1: return "January"; case 2: return "February"; case 3: return "March"; case 4: return "April"; case 5: return "May"; case 6: return "June"; case 7: return "July"; case 8: return "August"; case 9: return "September"; case 10: return "October"; case 11: return "November"; case 12: return "December"; default: return “????”;

17 Confusion In the preceding setDate method it’s tempting to define the method using the common terms “month”, “day” and “year” as the parameters. public void setDate( int month, int day, int year) { month = monthString( month ); // which month is which? day = day; // which day is which? year = year; // which year is which? } The problem with this code is that the compiler assumes that all uses of “day”, “month”, and “year” refer to the method parameters and hence this code has no effect. Jan 23, 2008

18 Calling Object When any class method is called, the instance variables used within the method are assumed to belong to the calling object. What code in setDate is really trying to do is public void setDate( int month, int day, int year) { “calling object”.month = monthString( month ); “calling object”.day = day; “calling object”.year = year; } It’s handy (and sometimes necessary) to have a name for the calling object. In Java, we use the reserved word this as the name of the calling object. (continued) Jan 23, 2008

19 Using this So, if we want to name our parameters the same as our instance variables, we could write the setDate method as public void setDate( int month, int day, int year) { this.month = monthString( month ); this.day = day; this.year = year; } Note that many examples in the text use this technique for class methods and some Java programmer tools (including Eclipse) use this technique when writing code for you. Jan 23, 2008

20 this again As a final example, recall the print method from Date1 public void print( ) { System.out.println(month + “ “ + day + “ “ + year); } Here it’s clear that month, day, and year refer to the instance variables of the calling object because there are no parameters. To be more explicit we could have written System.out.println(this.month + “ “ + this.day + “ “ + this.year); With no chance for confusion, using the prefix this is unnecessary and usually omitted. Aug 6, 2007

21 Date1 Code Example Date1 newYears = new Date1( );
// Determine the output from this code snippet Date1 newYears = new Date1( ); newYears.month = “January”; newYears.day = 1; newYears.year = 2008; Date1 birthday = new Date1( ); birthday.month = “July”; birthday.day = 4; birthday.year = 1776; newYears.print( ); // line 1 birthday.print( ); // line 2 System.out.println(birthday.monthString(6)); // line 3 birthday.setDate( 2, 2, 2002); // line 4 birthday.print( ); // line 5 newYears.day = 42; // line 6 newYears.print( ); // line 7 The output from line 1 is “January 1, 2008” The output from line 2 is “July 4, 1776” The output from line 3 is “June” No output from line 4 – it changes birthday The output from line 5 is “February 2, 2002” No output from line 6 – this changes newYears’s day = 42; The output from line 7 is “January 42, 2008” Sept 7, 2007

22 January 42, 2008 It appears that classes allow the user to change the data anytime he chooses and can possibly make the data invalid That’s true so far because we have defined our instance variables with public access. This is rarely the case in real applications. Our focus today was on classes vs objects and the syntax used for accessing methods. Next time we’ll see a more robust class that will prevent the user from making our data invalid. Sep 7, 2007


Download ppt "CMSC 202 Classes and Objects."

Similar presentations


Ads by Google