Transcript
Page 1: Classes Java and other Object-Orientated Programs or OOP are based on the creation and interaction of classes. Every program in Java has at least one class

Classes

• Java and other Object-Orientated Programs or OOP are based on the creation and interaction of classes.

• Every program in Java has at least one class.

Page 2: Classes Java and other Object-Orientated Programs or OOP are based on the creation and interaction of classes. Every program in Java has at least one class

Classes (cont’d)

• The understanding of what classes and objects are can be a little confusing at first.

• Many texts uses a number of different examples and analogies to try and explain the concept of a class.

• Lets look at a few. • In doing so you can pick the one that makes

the most sense to you.

Page 3: Classes Java and other Object-Orientated Programs or OOP are based on the creation and interaction of classes. Every program in Java has at least one class

The Recipe Analogy

• A class might be thought of as a recipe for making a cake.

• Within the recipe would be listed all the ingredients for making the cake (variables)

• We’d also find instructions about what actions would be necessary to turn the ingredients into a completed cake. (methods)

Page 4: Classes Java and other Object-Orientated Programs or OOP are based on the creation and interaction of classes. Every program in Java has at least one class

public class cake {

milk

sugar

flour

bake(){

mix milk, sugar and flour in bowl, heat ……

}

}

Class name

variables

method

Page 5: Classes Java and other Object-Orientated Programs or OOP are based on the creation and interaction of classes. Every program in Java has at least one class

Class vs Object

• Having a class does not mean any objects have actually been created.

• Like the recipe, a class is only a description of the components and the actions associated with a potential object.

Page 6: Classes Java and other Object-Orientated Programs or OOP are based on the creation and interaction of classes. Every program in Java has at least one class

Class vs Object

• Cake, as a concept,, is something that we know and understand but it is not something that we can see, touch or eat.

• A cake, in contrast, is something that we can see and eat.

• Compare the cake that mom just made sitting in the fridge (topped with chocolate icing that came with a warning of dire consequences if we touch it before the party) with the recipe for that cake.

• This distinction is crucial in understanding how classes are used in programming.

Page 7: Classes Java and other Object-Orientated Programs or OOP are based on the creation and interaction of classes. Every program in Java has at least one class

• A class defines a new type• A class is a description of an object • An object is an instance of a class. • We say that we instantiate an object when we

create it.• Every object is Java must have a class associated

with it. • The class is the type (and these two terms are

often used interchangeably).

Page 8: Classes Java and other Object-Orientated Programs or OOP are based on the creation and interaction of classes. Every program in Java has at least one class

Instantiating an Object

• To declare an object entitled MyCake of class cake use the following syntax: cake MyCake = new cake();

• The class cake describes all the qualities of the object MyCake.

• MyCake is an object of class(or type) cake.

Page 9: Classes Java and other Object-Orientated Programs or OOP are based on the creation and interaction of classes. Every program in Java has at least one class

Instantiating an Object

• You can declare more cake objects:cake MyCake2 = new cake();

cake yourCake = new cake();

cake bDayCake = new cake();

Page 10: Classes Java and other Object-Orientated Programs or OOP are based on the creation and interaction of classes. Every program in Java has at least one class

The Factory Analogy

• Consider a factory whose purpose is to make chairs

• The factory itself is not a chair.

• It is a mechanism for making chairs.

• The factory is designed to be the most efficient and functional chair maker it can be.

Page 11: Classes Java and other Object-Orientated Programs or OOP are based on the creation and interaction of classes. Every program in Java has at least one class

The Factory Analogy

• We can design and build a factory but the factory can sit idle until it receives an order to construct some chairs.

• An class is like a factory.

• It’s job is to construct a certain type of object when it receives an order (keyword: new)

Page 12: Classes Java and other Object-Orientated Programs or OOP are based on the creation and interaction of classes. Every program in Java has at least one class

new

• The keyword new tells the java compiler that we are trying to declare an object.

• Compare:int myNum;andcake myCake = new cake();

• In the 1st case we’re declaring a primitive data type. In the 2nd case we’re declaring an abstract one.

Page 13: Classes Java and other Object-Orientated Programs or OOP are based on the creation and interaction of classes. Every program in Java has at least one class

Circle class

• Look at the Circle class on p181 of your textbook.

• We have some variables

• We have something called a constructor. (Don’t worry for the moment)

• We have a method to set the radius setRadius()

Page 14: Classes Java and other Object-Orientated Programs or OOP are based on the creation and interaction of classes. Every program in Java has at least one class

Circle class

• We have a method to calculate the area of a circle – area()

• We have a method to return the radius of a circle – getRadius()

Page 15: Classes Java and other Object-Orientated Programs or OOP are based on the creation and interaction of classes. Every program in Java has at least one class

Let’s try some code

• Start a new JCreator project and call it Circle.• Enter the code from p181 of your text.• Do not delete the main method.• Build the code.• Modify the main method to create a new circle.• Set the radius to 5.• Call the area() method to print the area• Call the getRadius() method to print the

radius.

Page 16: Classes Java and other Object-Orientated Programs or OOP are based on the creation and interaction of classes. Every program in Java has at least one class

More About Objects

• Objects have two generic qualities:– state: reflects the data stored by the object

• think variables (e.g. radius)

– behaviour: the actions it can take and the communication it provides

• think methods (e.g. area())

Page 17: Classes Java and other Object-Orientated Programs or OOP are based on the creation and interaction of classes. Every program in Java has at least one class

Good Programming Style for Objects

• An object’s state should only be changed through it’s behaviour.

• If we want to change the radius of a circle object we only allow this to be done via the setRadius() method.

• Protecting an object’s data in this way is called encapsulation.

Page 18: Classes Java and other Object-Orientated Programs or OOP are based on the creation and interaction of classes. Every program in Java has at least one class

Client Code

• Our text uses the paradigm of client code• Client Code is an application that uses one

or more classes.• It can access the class’ methods but not it’s

data.• This reinforces…• …encapsulation

Page 19: Classes Java and other Object-Orientated Programs or OOP are based on the creation and interaction of classes. Every program in Java has at least one class

Chapter 8

A Class

public class Circle {private static final double PI = 3.14;private double radius;

public Circle() {radius = 1;

}

public void setRadius(double newRadius) {radius = newRadius;

}

access

level

access

level

class

name

class

name

bodybody

variablevariabless

constructorconstructor

methodmethod

Page 20: Classes Java and other Object-Orientated Programs or OOP are based on the creation and interaction of classes. Every program in Java has at least one class

Class Design

• Each class should be written in it’s own file.• The name of the file should be the same as

the class name (including capitalization) with the .java file extension appended.

Page 21: Classes Java and other Object-Orientated Programs or OOP are based on the creation and interaction of classes. Every program in Java has at least one class

Class Design

• Just like methods, classes have declarations• The class declaration consists of:

– an access level (public or private)– the keyword class– the class name

• Everything inside the braces is the class body

Page 22: Classes Java and other Object-Orientated Programs or OOP are based on the creation and interaction of classes. Every program in Java has at least one class

Class Design (cont’d)

• The class body is typically composed of:– variables– one or more constructors

• a piece of code executed when an object is created from the class (more to come, later)

– methods

Page 23: Classes Java and other Object-Orientated Programs or OOP are based on the creation and interaction of classes. Every program in Java has at least one class

Class Design -conventions

• Class names should be nouns• Class names should begin with uppercase

letters• If the class name is composed of more than

one word, each subsequent word should be uppercase

• Like other java identifiers, spaces are not allowed.

Page 24: Classes Java and other Object-Orientated Programs or OOP are based on the creation and interaction of classes. Every program in Java has at least one class

Class Design (cont’d)

• An opening brace (“{“) begins a class• A closing brace (“}”) ends a class• Class variables are declared at the top of the

class after the opening brace• Class variables are declared outside of the

class methods

Page 25: Classes Java and other Object-Orientated Programs or OOP are based on the creation and interaction of classes. Every program in Java has at least one class

Class Design (cont’d)

• Class variables should have an access level of private

• This makes them visible to the class but not to client code which can then only access them via method calls.

• What’s that called again?• Riiiggghhht. Encapsulation.

Page 26: Classes Java and other Object-Orientated Programs or OOP are based on the creation and interaction of classes. Every program in Java has at least one class

Class Design (cont’d)

• Class methods fall into 3 categories:– accessor methods

• called to determine the value of a variable (e.g. getRadius())

– modifier methods• called to change the value of a variable (e.g. setRadius())

– helper methods• called by other methods in the class to perform tasks

(no e.g. in the Circle class)

Page 27: Classes Java and other Object-Orientated Programs or OOP are based on the creation and interaction of classes. Every program in Java has at least one class

You do

• Pg 182 Review: Circle - part 1 of 4• Pg 182 Review: Coin - part 1 of 2

– Coin involves random numbers. See pg 109-110 or the web


Top Related