Classes and MethodsObjects have behaviorsObjects have stateExample: a “Rabbit” objectClassesDefining a classDefining fieldsDefining constructorsParametersExample constructorExample use of a constructorA problem with namesSlide 13Slide 14this to the rescueA problem with names—solved!A typical use of thisDefining a methodReturning a result from a methodReturning no result from a methodSending messagesPutting it all togetherUsing our new classnullClasses form a hierarchyWhat is the class hierarchy for?Example of inheritanceClass variables and methodsWhy have class variables and methods?Example use of a class variableVocabulary IVocabulary IIVocabulary IIIVocabulary IVThe EndJan 14, 2019Classes and Methods2Objects have behaviorsIn old style programming, you had:data, which was completely passivefunctions, which could manipulate any dataIn O-O programming, an object contains both data and methods that manipulate that dataAn object is active, not passive; it does thingsAn object is responsible for its own dataIt can protect that data (by marking it private)It can expose that data to other objects (by not marking it, or by marking it public)Data should not be exposed without good reason3Objects have stateAn object contains dataThe data represent the state of the objectData can also describe the relationship of the object to other objectsExample: a checkingAccount might haveA balance (the internal state of the account)An owner (some object representing a person)An accountNumber (used as an ID number)4Example: a “Rabbit” objectYou could create an object representing a rabbitIt would have data:How hungry it isHow healthy it isWhere it isAnd methods:eat, run, dig, hide5ClassesA class describes a set of objectsThe objects are called instances of the classA class describes:Fields that hold the data for each objectConstructors that tell how to create a new object of this classMethods that describe the actions the object can performIn addition, a class can have data and methods of its own (not part of the objects)For example, it can keep a count of the number of objects it has createdSuch data and methods are called static6Defining a classHere is the simplest syntax for defining a class: class ClassName { // the fields (variables) of the object // the constructors for the object // the methods of the object}You can put public, protected, or private before the word classThings in a class can be in any order (I recommend the above order)7Defining fieldsAn object’s data is stored in fields (also calledinstance variables)The fields describe the state of the objectFields are defined with ordinary variable declarations: String name;Double health;int age = 0;8Defining constructorsA constructor is code to create an objectYou can do other work in a constructor, but you shouldn’tThe syntax for a constructor is: ClassName(parameters) { …code…}The ClassName has to be the same as the class that the constructor occurs inThe parameters are a comma-separated list of variable declarations9ParametersWe usually need to give information to constructors and to methodsA parameter is a variable used to hold the incoming informationA parameter must have a name and a typeYou supply values for the parameters when you use the constructor or methodThe parameter name is only meaningful within the constructor or method in which it occurs10Example constructor public class Person { String name; int age; boolean male; Person (String aName, boolean isMale) { name = aName; male = isMale; }}ConstructorParameters11Example use of a constructorThe constructor looks like: Person (String aName, boolean isMale) {…}aName and isMale are called formal parametersThe formal parameters are used to receive valuesYou can construct a new Person like this: Person john = new Person("John Smith", true);"John Smith" and true are called actual parametersThe actual parameters are used to give values to the formal parametersYou must have the same number of actual parameters as formal parameters, in the same order, and they must have the same types12A problem with namesIt would be nice if we could say:public class Person { String name; boolean male; Person (String name, boolean male) { name = name ; male = male ; }}13A problem with namesAnd have it mean:public class Person { String name; boolean male; Person (String name, boolean male) { name = name ; male = male ; }}14A problem with namesBut this is what it would really mean:public class Person { String name; boolean male; Person (String name, boolean male) { name = name ; male = male ; }}15this to the rescueA parameter may have the same name as an instance variableThe name always refers to the parameterThe keyword this refers to the current objectPutting this in front of a name means that the name is a field of this object (it isn't a parameter)16A problem with names—solved!Here is how we do what we want:public class Person { String name; boolean male; Person (String name, boolean male) { this.name = name ; this.male = male ; }}17A typical use of thisIf you write a constructor with parameters……and the parameters are used to set fields that have the same meaning……then use the same names! Person (String name, boolean male) { this.name = name ; this.male = male ; }In fact, this is the recommended way to do it18Defining a methodA method has the syntax: return-type method-name ( parameters ) { method-variables code}Example: boolean isAdult( ) { int magicAge = 21; return age >= magicAge;}19Returning a result from a methodIf a method is to return a result, it must specify the type of the result boolean isAdult ( …You must use a return statement to exit the method with a result of the correct type: return age >= magicAge;This is for methods only (constructors automatically return a result of the correct type)20Returning no result from a methodThe keyword void is used to indicate that a method doesn’t return a value void printAge( ) { System.out.println(name + " is " + age + " years old."); return;}The keyword return is not required in a void methodThe method will return automatically when it reaches the end of the method (the final closing brace)21Sending
View Full Document