chapter 4 objects and classes. assignment 2 solution in class exercise

Post on 11-Jan-2016

219 Views

Category:

Documents

3 Downloads

Preview:

Click to see full reader

TRANSCRIPT

Chapter 4

Objects and Classes

Assignment 2 Solution

• In class exercise

Object Oriented Programming

• OOP• Programs made up of objects– Functionality exposed to users– Implementation hidden from users

• Objects can be used from library or custom created

• Using object and their methods can reduce program complexity

Classes

• The template or “blueprint” from which objects are made.

• Construct an object from class means to create an “instance” of that class

• Encapsulation – aka info hiding.• The data inside an object is known as its

instance fields• Function that operate on the data are called

methods

Class cont’

• Notion that classes are cookie cutters and objects are the cookies created from classes

• The user interacts with the methods of class – you should never deal with how the methods are implemented

Classes cont’

• Java has the ability to “extend” a class. • A new class has all of the properties and

methods of the class you extend• You can write new methods and add

properties that apply to your new class. • This is also referred to as “inheritance”

Where to start??

• The top down approach doesn’t apply– You don’t start with the void main(String args[])

• When thinking about creating your own classes (use nouns for analyzing the problem space)

• Use verbs to describe methods on the nouns

Using Predefined Classes

• Take, for example, the Math class. You have seen that you can use methods of the Math class, such as Math.random(), without needing to know how they are implemented—

• all you need to know is the name and parameters (if any). – That is the point of encapsulation and will certainly be true of

all classes.• But the Math class only encapsulates functionality; it neither

needs nor hides data. Because there is no data, you do not need to worry about making objects and initializing their instance fields—there aren’t any

• Arrays class is another example of this

Object Variables• To work with objects, you first construct them and specify their

initial state. Then you apply methods to the objects.• You use constructors to construct new instances. A constructor is a

special method whose purpose is to construct and initialize objects.• Constructors always have the same name as the class name. To

construct an object, you combine the constructor with• the new operator.

– new Date();• There is an important difference between objects and object

variables. • Date deadline; // deadline doesn't refer to

any object• The above defines an object variable, deadline, that can refer to

objects of type Date

Object Variables Cont’

• The return value of the new operator is also a reference. A statement such as

Date deadline = new Date();• Has two parts. The expression new Date()

makes an object of type Date, and its value is a reference to that newly created object. That reference is then stored in the deadline variable.

Lets create an Object!!!

• Bicycle Class–Nouns? •Bike components

–Verbs?•Bike (behaviors)

Bicycle Class

• Bike properties– Gear, speed, and

cadence

• State– Current gear, speed,

and cadence

• Methods– SpeedUp, ChangeGear,

Brake, ChangeCadence

Bike Class Java Code

public class Bicycle { //members (properties of Bike class)

int cadence = 0;int speed = 0;int gear = 1;

Bike Class Java Code cont’void changeCadence(int newValue) {

cadence = newValue; } void changeGear(int newValue) {

gear = newValue; } void speedUp(int increment) {

speed = speed + increment; } void applyBrakes(int decrement) {

speed = speed - decrement; } void printStates() { System.out.println("cadence:"+cadence+" speed:” +speed+" gear:"+gear); } } //end of class

Bike Class Java Code cont’class BicycleDemo {

public static void main(String[] args) { // Create Bicycle objects Bicycle bike1 = new Bicycle(); // Invoke methods on those objects

bike1.changeCadence(50); bike1.speedUp(10); bike1.changeGear(2); bike1.printStates();

}//end main()}//end BicycleDemo

Extending Bicycle

class MountainBike extends Bicycle { // new fields and methods defining a mountain bike would go here

}

Relationships Between Classes

Constructors

Constructors Cont’

this

Accessor and Mutator and Private

• Are types of functions used in your classes– Accessor fucntions (get) Return values of instance

fields (class properties)– Mutator functions (set) Change the value of the

instance fields (class properties• The private keyword makes sure that the only

methods that can access these instance fields• No outside method can read or write to these

fields.

Instance Fields

Final Instance Fields

Static Fields and Methods

Initializing Fields

Method Parameters

Packages

• Java allows you to group classes in a collection called a package.

• Packages are convenient for organizing your work– separating your work from code libraries provided

by others• The main reason for using packages is to

guarantee the uniqueness of class names.– You wouldn’t want two copies of Bicycle class

Packages Cont.’

Static Imports

Static Imports Cont.’

Extending A Class

Dynamic Binding

super

• Whenever you want to access a instance field of your super class inside of your sub class you have to use the keyword super

• See eclipse for example

Final Classes

Final Methods

Assignments 3 & 4

• Due Dec 4th – posted to class website

top related