Object: Software Bundle of Related State and Behavior

Total Page:16

File Type:pdf, Size:1020Kb

Object: Software Bundle of Related State and Behavior

Java Notes 12

Introduction:

Object: software bundle of related state and behavior.

Class: blueprint or prototype from which objects are created.

Inheritance: organizing and structuring your software.

Interface: contract between a class and the outside world.

Package: namespace for organizing classes and interfaces in a logical manner.

OBJECTS

Objects: key to understanding object-oriented programming

Real world objects all have a state and a behavior.

Dog: state = name, color, breed, hungry, … (nouns) behavior = barking, fetching, wagging, … (verbs)

Bicycles: state = current gear, current pedal cadence, current speed, … (nouns) behavior = changing gear, changing pedal cadence, applying brakes, … (verbs) state = how does it exist? behavior = what is it doing?

Indeed, “state” is a noun and “behavior” is a verb.

--

9/24/08 Objects – cont’d –

But this really sound as if an object is an element in a set – as in set theory. However, the java set (Class) gives instructions how to build the object. Just like a manufacturing plant. They build objects according to the instructions they have on hand in their plant.

1 From: Trail: Learning the Java Language http://java.sun.com/docs/books/tutorial/java/index.html 2 Note: all software is copyrighted: http://java.sun.com/docs/books/tutorial/java/concepts/examples/BicycleDemo.java A software object.

An object has its state stored in fields (aka “variables” in other programming languages).

An object has its behavior stored in methods (aka “functions” in other programming languages).

Methods act on an object’s state and perform object-to-object communication.

What is going on in the foreground (the code of the program) is happening in the method, which is not seen by anyone who reads the code. It seems that what is going on is that Java is loaded with what are called “subroutines” in other programming languages. Indeed,

“Hiding internal state and requiring all interaction to be performed through an object's methods is known as data encapsulation – …”

Example: the bicycle

A bicycle modeled as a software object.

The above software model of a bicycle (object) represents how the methods allow the object (bicycle) to be used. “changeGears” could be the method which tests that the value of the gear does not exceed the bicycle’s gear design. So the method acts as the go-between the outside world and the object’s state. (Pictured above in “A software object”.)

OO Advantages: 1 – modular: the object can be maintained separately from the source code which uses it, 2 – hiding: the object’s implementation remains hidden from the casual viewer, 3 – re-use: the reverse of #1 above, namely other written objects can be used in your code – easily, 4 – pluggable: objects can be plugged in or unplugged from the code w/o damage to your code, and 5 – debugging: b/c of #1 and #4, debugging a massive code reduces to debugging only 1, small, object. CLASSES

Classes: if you have many bicycles made from the same blueprint then the one you use is called an instance of that class of bicycle. E.g. have the object (blueprint) in your code, then using one of it is an instance of that object. “A class is the blueprint from which individual objects are created.”

The basic relation is:

Instance < Object < Class < Package class Bicycle { // the class of bicycles

int cadence = 0; // the states of the bicycle int speed = 0; int gear = 1;

void changeCadence(int newValue) { // the behaviors of the bicycle 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); } }

See the code BicycleDemo.java which is an application that creates two new bicycle instances: bike1 and bike2.

“You may have noticed that the Bicycle class does not contain a main method. That's because it's not a complete application; it's just the blueprint for bicycles that might be used in an application. The responsibility of creating and using new Bicycle objects belongs to some other class in your application.” INHERITANCE

Inheritance: this occurs when one class of objects contains the same states and behaviors of other classes. We normally think of one class as the larger, or “mother” (my word) class, since it has none of the specific features which make the “smaller” classes the sub-classes in the relationship. The “larger” class is the supeclass of the subclasses. E.g. “bicycle” is generic for: road bike, tandem bike, and mountain bike. All of the bicycles share the same states as those present in the class “bicycle” yet the road bike has drop handlebars; the tandem bike has two handlebars; and the mountain bike has performance handlebars. All of them have gears, change of speed, etc. Thus “bicycle” is the superclass of the subclasses: road, tandem, and mountain bikes.

Again, a little knowledge of sets is a good thing. :)

BICYCLE

drop-down handlebars two handlebars performance handlebars

road tandem mountain

Now, how do you relate the subclass to the superclass? It’s best said by Sun: class MountainBike extends Bicycle {

// new fields and methods defining a mountain bike would go here

}

Basically you are making the superclass larger by the addition of the subclass – thus you are “extending” the superclass to now include the new subclass. So “MountainBike extends Bicycle”. And in the new subclass you will have new fields and methods which make your subclass specific to that class of objects, e.g. mountain bikes. At the same time your new subclass inherits all the same fileds and methods present in the superclass. INTERFACE

Interface: since objects talk to the outside world using methods, then a separate place for all these methods would seem appropriate. This is the purpose of the interface. It allows the methods for a class to be defined as an interface class. Continuing with the bicycle example, the interface for denoting the bicycle’s behavior would be:

interface Bicycle {

void changeCadence(int newValue);

void changeGear(int newValue);

void speedUp(int increment);

void applyBrakes(int decrement);

}

These are all the methods which appear in the Bicycle class. Notice that each has a field and each field is named accordingly to that method: speedUp uses “increment” while applyBrakes uses “decrement”.

Implementing this interface one has to do three things: 1) the class must now be given a new name, say ACMEBicycle, and 2) the new class is defined in the same way but with an “implements” instruction, and 3) since these methods are now defined outside of Bicycle (i.e. they are defined in the bicycle interface – and not the Bicycle class itself – then you need to let the Bicycle class be public. So a the full line would read: public class ACMEBicycle implements Bicycle

Now this interface forms a “contract” with the outside world to deliver these methods from the ACMEBicycle class to the user. PACKAGE

Package: a package is similar to a folder. It organizes classes and their interfaces into one group which are made available as they are separately imported or as a group.

Library: a set of class packages. Java provides an enourmous class library called the API, which is short for “Application Programming Interface”.

“ There are literally thousands of classes to choose from. This allows you, the programmer, to focus on the design of your particular application, rather than the infrastructure required to make it work.”

Platform: the list of libraries and all other supporting files for writing Java.

JAVA: Questions and Exercises 01

Questions and Exercises: Object-Oriented Programming Concepts

Questions

1. Real-world objects contain ___ and ___. 2. A software object's state is stored in ___. 3. A software object's behavior is exposed through ___. 4. Hiding internal data from the outside world, and accessing it only through publicly exposed methods is known as data ___. 5. A blueprint for a software object is called a ___. 6. Common behavior can be defined in a ___ and inherited into a ___ using the ___ keyword. 7. A collection of methods with no implementation is called an ___. 8. A namespace that organizes classes and interfaces by functionality is called a ___. 9. The term API stands for ___?

Exercises

1. Create new classes for each real-world object that you observed at the beginning of this trail. Refer to the Bicycle class if you forget the required syntax. 2. For each new class that you've created above, create an interface that defines its behavior, then require your class to implement it. Omit one or two methods and try compiling. What does the error look like?

End of Object-Oriented Programming Concepts. ANSWERS to the Questions: Questions:

1. states and behavior 2. fields 3. methods 4. encapsulisation 5. classes 6. superclasses, subclasses, and “extends” 7. interface 8. package (e.g. the API) 9. Application Programming Interface

Exercises: class DeskLamp{

// states int state;

// behaviors void switch(int state){ if (state == 1) state = 0 else state = 1; } } class DesktopRadio{ // states int power; int volume; int station;

// behaviors void switch(power){ if (power == 0) power =1 else power = 0; } void volumeUp(volume){ +=volume; } void volumeDown(volume){ -=volume; } void changeStation(station){ station = ; } void seekStation(station){ station = ; }

}

Recommended publications