25-Jun-15 Starting Classes and Methods. Objects have behaviors In old style programming, you had:...

35
Jun 27, 2 022 Starting Classes and Methods
  • date post

    21-Dec-2015
  • Category

    Documents

  • view

    213
  • download

    0

Transcript of 25-Jun-15 Starting Classes and Methods. Objects have behaviors In old style programming, you had:...

Page 1: 25-Jun-15 Starting Classes and Methods. Objects have behaviors In old style programming, you had: data, which was completely passive functions, which.

Apr 18, 2023

Starting Classes and Methods

Page 2: 25-Jun-15 Starting Classes and Methods. Objects have behaviors In old style programming, you had: data, which was completely passive functions, which.

Objects have behaviors

In old style programming, you had: data, which was completely passive functions, which could manipulate any data

In O-O programming, an object contains both data and methods that manipulate that data An object is active, not passive; it does things An object is responsible for its own data

But it can expose that data to other objects

Page 3: 25-Jun-15 Starting Classes and Methods. Objects have behaviors In old style programming, you had: data, which was completely passive functions, which.

Objects have state

An object contains data The data represent the state of the object Data can also describe the relationship of the object to other

objects Example: a checkingAccount might have

A balance (the internal state of the account) An owner (some object representing a person) An accountNumber (used as an ID number)

Page 4: 25-Jun-15 Starting Classes and Methods. Objects have behaviors In old style programming, you had: data, which was completely passive functions, which.

Example: a “Rabbit” object

You could create an object representing a rabbit It would have data:

How hungry it is How healthy it is Where it is

And methods: eat, run, dig, hide

Page 5: 25-Jun-15 Starting Classes and Methods. Objects have behaviors In old style programming, you had: data, which was completely passive functions, which.

Classes

A class describes a set of objects The objects are called instances of the class A class describes:

Fields that hold the data for each object Constructors that tell how to create a new object of this

class Methods that describe the actions the object can

perform In 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 created Such data and methods are called static

Page 6: 25-Jun-15 Starting Classes and Methods. Objects have behaviors In old style programming, you had: data, which was completely passive functions, which.

Defining a class

Here is the simplest syntax for defining a class: class NameOfClass {

// 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 class

Things in a class can be in any order (I recommend the above order)

Page 7: 25-Jun-15 Starting Classes and Methods. Objects have behaviors In old style programming, you had: data, which was completely passive functions, which.

Defining fields

An object’s data is stored in fields (also called instance variables)

The fields describe the state of the object Fields are defined with ordinary variable declarations:

String name;Double health;int age = 0;

Page 8: 25-Jun-15 Starting Classes and Methods. Objects have behaviors In old style programming, you had: data, which was completely passive functions, which.

Defining constructors

A constructor is code to create an object You can do other work in a constructor, but you shouldn’t

The syntax for a constructor is: ClassName(parameters) {

…code…}

The ClassName has to be the same as the class that the constructor occurs in

The parameters are a comma-separated list of variable declarations

Page 9: 25-Jun-15 Starting Classes and Methods. Objects have behaviors In old style programming, you had: data, which was completely passive functions, which.

Parameters

We usually need to give information to constructors and to methods

A parameter is a variable used to hold the incoming information

A parameter must have a name and a type You supply values for the parameters when you use the

constructor or method The parameter name is only meaningful within the

constructor or method in which it occurs

Page 10: 25-Jun-15 Starting Classes and Methods. Objects have behaviors In old style programming, you had: data, which was completely passive functions, which.

Example constructor

public class Person { String name; int age; boolean male;

Person (String aName, boolean isMale) { name = aName; male = isMale; }

}

Constructor

Parameters

Page 11: 25-Jun-15 Starting Classes and Methods. Objects have behaviors In old style programming, you had: data, which was completely passive functions, which.

Example use of a constructor

The constructor looks like: Person (String aName, boolean isMale) {…} aName and isMale are called formal parameters The formal parameters are used to receive values

You can construct a new Person like this: Person john = new Person("John Smith", true); "John Smith" and true are called actual parameters The actual parameters are used to give values to the formal

parameters You must have the same number of actual parameters

as formal parameters, in the same order, and they must have the same types

Page 12: 25-Jun-15 Starting Classes and Methods. Objects have behaviors In old style programming, you had: data, which was completely passive functions, which.

A problem with names

It would be nice if we could say: public class Person {

String name; boolean male;

Person (String name, boolean male) {

name = name ;

male = male ; }}

Page 13: 25-Jun-15 Starting Classes and Methods. Objects have behaviors In old style programming, you had: data, which was completely passive functions, which.

A problem with names

And have it mean: public class Person {

String name; boolean male;

Person (String name, boolean male) {

name = name ;

male = male ; }}

Page 14: 25-Jun-15 Starting Classes and Methods. Objects have behaviors In old style programming, you had: data, which was completely passive functions, which.

A problem with names

But this is what it would really mean: public class Person {

String name; boolean male;

Person (String name, boolean male) {

name = name ;

male = male ; }}

Page 15: 25-Jun-15 Starting Classes and Methods. Objects have behaviors In old style programming, you had: data, which was completely passive functions, which.

this to the rescue

A parameter may have the same name as an instance variable The name always refers to the parameter

The keyword this refers to the current object Putting this in front of a name means that the

name is a field of this object (it isn't a parameter)

Page 16: 25-Jun-15 Starting Classes and Methods. Objects have behaviors In old style programming, you had: data, which was completely passive functions, which.

A 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 ; }}

Page 17: 25-Jun-15 Starting Classes and Methods. Objects have behaviors In old style programming, you had: data, which was completely passive functions, which.

A typical use of this

If 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 it

Page 18: 25-Jun-15 Starting Classes and Methods. Objects have behaviors In old style programming, you had: data, which was completely passive functions, which.

Defining a method

A method has the syntax: return-type method-name ( parameters ) {

method-variables code}

Example: boolean isAdult( ) {

int magicAge = 21; return age >= magicAge;}

Page 19: 25-Jun-15 Starting Classes and Methods. Objects have behaviors In old style programming, you had: data, which was completely passive functions, which.

Returning a result from a method

If 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)

Page 20: 25-Jun-15 Starting Classes and Methods. Objects have behaviors In old style programming, you had: data, which was completely passive functions, which.

Returning no result from a method

The 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 method The method will return automatically when it reaches

the end of the method (the final closing brace)

Page 21: 25-Jun-15 Starting Classes and Methods. Objects have behaviors In old style programming, you had: data, which was completely passive functions, which.

Sending messages

We send a message to an object by: Naming the object Naming the method we want to use Providing any needed actual parameters

Example: if (john.isAdult ()) {

john.printAge();}

john.isAdult() returns a value (subsequently used by the if statement)

john.printAge() does not return a value

Page 22: 25-Jun-15 Starting Classes and Methods. Objects have behaviors In old style programming, you had: data, which was completely passive functions, which.

Putting it all together

class Person {

// fields String name; int age;

// constructor Person(String name) { this.name = name; age = 0; }

// methods String getName() { return name; }

void birthday() { age = age + 1; System.out.println( "Happy birthday!"); }

}

Page 23: 25-Jun-15 Starting Classes and Methods. Objects have behaviors In old style programming, you had: data, which was completely passive functions, which.

Using our new class

Person john; john = new Person("John Smith");

System.out.print (john.getName()); System.out.println(" is having a birthday!"); john.birthday();

Of course, this code must also be inside a class!

Page 24: 25-Jun-15 Starting Classes and Methods. Objects have behaviors In old style programming, you had: data, which was completely passive functions, which.

null If you declare a variable to have a given object type, for example,

Person john; String name;

...and if you have not yet assigned a value to it, for example, with john = new Person();

String name = “John Smith"; ...then the value of the variable is null null is a legal value, but there isn’t much you can do with it

It’s an error to refer to its fields, because it has none It’s an error to send a message to it, because it has no methods The error you will see is NullPointerException

Page 25: 25-Jun-15 Starting Classes and Methods. Objects have behaviors In old style programming, you had: data, which was completely passive functions, which.

Classes form a hierarchy

Classes are arranged is a treelike hierarchy There is one class at the top, or root, of the hierarchy,

named Object In computer science we draw trees upside-down, with the root

at the top Every class except Object has one “parent” class, or

superclass Each class is a subclass of its superclass

Page 26: 25-Jun-15 Starting Classes and Methods. Objects have behaviors In old style programming, you had: data, which was completely passive functions, which.

What is the class hierarchy for?

Classes inherit from their superclasses A class has not only its own fields and methods, but also:

Every field described in any class above it Every method described in any class above it Classes do not inherit constructors, however

Hence, a class may contain much more information than is obvious from the class description

Page 27: 25-Jun-15 Starting Classes and Methods. Objects have behaviors In old style programming, you had: data, which was completely passive functions, which.

Example of inheritance

Example: class Employee extends Person {

double hourlyWage;

void pay(double hoursWorked) { System.out.println("Pay to the order of: " + name + " $" + hoursWorked * hourlyWage); }}

An Employee has a name, an age, an hourlyWage, and birthday and pay methods.

Page 28: 25-Jun-15 Starting Classes and Methods. Objects have behaviors In old style programming, you had: data, which was completely passive functions, which.

Class variables and methods

A class describes the variables and methods belonging to objects of that class These are called instance variables and instance methods

A class may also have its own variables and methods These are called class variables and class methods

Class variables and class methods are denoted by the keyword static C programmers: same word as in C, different meaning

Page 29: 25-Jun-15 Starting Classes and Methods. Objects have behaviors In old style programming, you had: data, which was completely passive functions, which.

Why have class variables and methods?

Sometimes you want to keep information about the class itself Example: Class Person might have a class variable

Population that counts people This would not be appropriate data to keep in each Person!

Sometimes you want to do something relevant to the class as a whole For example, find the average age of a population

Sometimes you don’t have any objects For example, you want to start up a program

Page 30: 25-Jun-15 Starting Classes and Methods. Objects have behaviors In old style programming, you had: data, which was completely passive functions, which.

Example use of a class variable

class Person { // fields String name; int age; static int population;

// constructor Person(String name) { this.name = name; age = 0; population = population + 1; }

// methods String getName() { return name; }

void birthday() { age = age + 1; System.out.println( "Happy birthday!"); }

}

Page 31: 25-Jun-15 Starting Classes and Methods. Objects have behaviors In old style programming, you had: data, which was completely passive functions, which.

Vocabulary I

class – a description of a set of objects object – a member of a class instance – same as “object” field – data belong to an object or a class variable – a name used to refer to a data object

instance variable – a variable belonging to an object class variable, static variable – a variable belonging to the

class as a whole method variable – a temporary variable used in a method

Page 32: 25-Jun-15 Starting Classes and Methods. Objects have behaviors In old style programming, you had: data, which was completely passive functions, which.

Vocabulary II

method – a block of code that can be used by other parts of the program instance method – a method belonging to an object class method, static method – a method belonging to the class

as a whole constructor – a block of code used to create an object

Page 33: 25-Jun-15 Starting Classes and Methods. Objects have behaviors In old style programming, you had: data, which was completely passive functions, which.

Vocabulary III

parameter – a piece of information given to a method or to a constructor actual parameter – the value that is passed to the method or

constructor formal parameter – the name used by the method or

constructor to refer to that value return value – the value (if any) returned by a method

Page 34: 25-Jun-15 Starting Classes and Methods. Objects have behaviors In old style programming, you had: data, which was completely passive functions, which.

Vocabulary IV

hierarchy – a treelike arrangement of classes root – the topmost thing in a tree Object – the root of the class hierarchy subclass – a class that is beneath another in the

class hierarchy superclass – a class that is above another in the

class hierarchy inherit – to have the same data and methods as a

superclass

Page 35: 25-Jun-15 Starting Classes and Methods. Objects have behaviors In old style programming, you had: data, which was completely passive functions, which.

The End