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

Download 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

Post on 13-Dec-2015




0 download

Embed Size (px)


<ul><li>Slide 1</li></ul> <p>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. Slide 2 Classes (contd) 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. Slide 3 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) Wed also find instructions about what actions would be necessary to turn the ingredients into a completed cake. (methods) Slide 4 public class cake { milk sugar flour bake(){ mix milk, sugar and flour in bowl, heat } Class name variables method Slide 5 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. Slide 6 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. Slide 7 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). Slide 8 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. Slide 9 Instantiating an Object You can declare more cake objects: cake MyCake2 = new cake(); cake yourCake = new cake(); cake bDayCake = new cake(); Slide 10 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. Slide 11 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. Its job is to construct a certain type of object when it receives an order (keyword: new ) Slide 12 new The keyword new tells the java compiler that we are trying to declare an object. Compare: int myNum; and cake myCake = new cake(); In the 1 st case were declaring a primitive data type. In the 2 nd case were declaring an abstract one. Slide 13 Circle class Look at the Circle class on p181 of your textbook. We have some variables We have something called a constructor. (Dont worry for the moment) We have a method to set the radius setRadius() Slide 14 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() Slide 15 Lets 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. Slide 16 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() ) Slide 17 Good Programming Style for Objects An objects state should only be changed through its 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 objects data in this way is called encapsulation. Slide 18 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 its data. This reinforces encapsulation Slide 19 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 class name body variables constructor method Slide 20 Class Design Each class should be written in its own file. The name of the file should be the same as the class name (including capitalization) with file extension appended. Slide 21 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 Slide 22 Class Design (contd) 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 Slide 23 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. Slide 24 Class Design (contd) 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 Slide 25 Class Design (contd) 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. Whats that called again? Riiiggghhht. Encapsulation. Slide 26 Class Design (contd) 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) Slide 27 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 </p>