20
Objects and Classes Chapter Nine

Objects and Classes Chapter Nine. Definition “an object is a combination of some data (variables) and some actions (methods)”. Hopefully the data and

Embed Size (px)

Citation preview

Page 1: Objects and Classes Chapter Nine. Definition “an object is a combination of some data (variables) and some actions (methods)”. Hopefully the data and

Objects and Classes

Chapter Nine

Page 2: Objects and Classes Chapter Nine. Definition “an object is a combination of some data (variables) and some actions (methods)”. Hopefully the data and

Definition

“an object is a combination of some data (variables) and some actions (methods)”.

Hopefully the data and methods are closely related NOT randomly thrown together

Page 3: Objects and Classes Chapter Nine. Definition “an object is a combination of some data (variables) and some actions (methods)”. Hopefully the data and

Example of an object

A balloon: describe what the balloon looks like - then make as many copies of the object as wanted - needed.

“Constructor” a method most often used to initialize variables So what’s so special about it: Same name as class, never returns a value.

Page 4: Objects and Classes Chapter Nine. Definition “an object is a combination of some data (variables) and some actions (methods)”. Hopefully the data and

Balloon will be seen again in Chapter 10

In chapter 10 with more “Bells and whistles”

page 172-173

Page 5: Objects and Classes Chapter Nine. Definition “an object is a combination of some data (variables) and some actions (methods)”. Hopefully the data and

Basics of Balloon

A circle “balloon” which you easily could be made more interesting

Page 6: Objects and Classes Chapter Nine. Definition “an object is a combination of some data (variables) and some actions (methods)”. Hopefully the data and

The program also displays two action buttons

one makes it larger, the other smaller

X and Y coordinates place the balloon anywhere in the frame (screen)

Data and methods are packaged = an object

Page 7: Objects and Classes Chapter Nine. Definition “an object is a combination of some data (variables) and some actions (methods)”. Hopefully the data and

Classes

Java and other OOPs languages can not write instructions directly that define a balloon as a single object. Java makes the programmer define what a balloon is (what it looks like)

A class is like a template (generic) and an instance is concrete (object)

Page 8: Objects and Classes Chapter Nine. Definition “an object is a combination of some data (variables) and some actions (methods)”. Hopefully the data and

Plan first, then build the real thing is designed and as a bonus you can get as many as you need (ie. More cars, trucks, balloons).

Class Balloon { some private variables

diameter = 10;

xCoord =20;

yCoord = 20;

} // All the new balloons will have the same initial values

Page 9: Objects and Classes Chapter Nine. Definition “an object is a combination of some data (variables) and some actions (methods)”. Hopefully the data and

Public Balloon ( int initalDiameter, int initalX, int initalY) {

diameter = initalDiameter;

xCoord = initalX;

yCoord = initialY; }

//If not noted otherwise set to these values

int = zero

boolean = false

char = \u000 (blank space) and

any object = null

Page 10: Objects and Classes Chapter Nine. Definition “an object is a combination of some data (variables) and some actions (methods)”. Hopefully the data and

Public or private

if private we have control no one outside the class can “see” alter out variables or methods

“Encapsulation” or information hiding if private

Variables...

Public one

Public two

Public three

Page 11: Objects and Classes Chapter Nine. Definition “an object is a combination of some data (variables) and some actions (methods)”. Hopefully the data and

// some methods in the class (public) can be seen the private ones can not.

myBalloon = new Balloon (20, 50, 50);

diameter X coord Y coord

Page 12: Objects and Classes Chapter Nine. Definition “an object is a combination of some data (variables) and some actions (methods)”. Hopefully the data and

Remember from earlier?

Balloon anotherBalloon;

anotherBalloon = new Balloon (20, 100, 100);

Balloon blueBalloon = new Balloon (20, 100,100);

// as many balloons as you want/need all instances or objects

Page 13: Objects and Classes Chapter Nine. Definition “an object is a combination of some data (variables) and some actions (methods)”. Hopefully the data and

A class in the java library

TextField data = new TextField ( );// use Text sometimes similar to drawString

for messages on screen

public void display (Graphics g) {g.drawOval ( xCoord, yCoord, diameter,diameter);

}

Page 14: Objects and Classes Chapter Nine. Definition “an object is a combination of some data (variables) and some actions (methods)”. Hopefully the data and

Another method

Public void actionPerformed (ActionEvent event) {

if (event.getSource ( ) = = grow)

// make the Balloon larger

myBalloon.changeSize(10);

if (event.getSource ( ) = =shrink)

myBalloon.changeSize( - 10); // smaller

repaint( ); }

Page 15: Objects and Classes Chapter Nine. Definition “an object is a combination of some data (variables) and some actions (methods)”. Hopefully the data and

Class PlayBalloon (two main objects

1. User interface object created from the class PlayBalloon. This is created by the browser or Appletviewer.

2. Balloon object, balloon, created from the class Balloon. This is created by the user interface object.

Example pgm pages 149 - 150 See it on web

Page 16: Objects and Classes Chapter Nine. Definition “an object is a combination of some data (variables) and some actions (methods)”. Hopefully the data and

Public data = X and Y coordinates

Private data = only accessed within class (not from the outside world)

Names of classes same as variables except underscore and $ dollar sign

the reserved word “this”

if a method needs to refer to the object it is presently working with it can refer to it using “this”

Page 17: Objects and Classes Chapter Nine. Definition “an object is a combination of some data (variables) and some actions (methods)”. Hopefully the data and

// an exampleprivate Scrollbar slider;

slider.addAdjustmentListener(this);“instanceof”// example segment

if (event.getSource( )instancesof TextField)handleTextFields(ActionEvent event);

// almost same code for instanceof Button //instead of TextField in above code

Page 18: Objects and Classes Chapter Nine. Definition “an object is a combination of some data (variables) and some actions (methods)”. Hopefully the data and

Built in types (primitive)

boolean, char, byte, short, long, float, double

Objects created from classes using new

//Strings Example

String S1 = “abc”, S2 = “abc”;

if (S1 = = S2) // Will be false because this tests if they are the same object

You can compare if (S1.equals(S2))

Page 19: Objects and Classes Chapter Nine. Definition “an object is a combination of some data (variables) and some actions (methods)”. Hopefully the data and

Object description & Garbage collection

Java has automatic garbage collection some languages do not - So you do not have to worry about freeing memory

“overloading” use of methods with same name as each other but difference is in signature Some examples:

abc (int x, int y) { }

abc ( float y, int z) { }

abc ( int a, int b, int c) { } may return void of some type value

Page 20: Objects and Classes Chapter Nine. Definition “an object is a combination of some data (variables) and some actions (methods)”. Hopefully the data and

“overriding” a method that extend another

“static” java reserved word that makes a variable or method of static to be used anywhere within the class (a static variable is “owned” by the class)

classes and files: You can have more than one class in a file but only one can by public. Lots of classes within one java source pgm but when compiled each class will have its own file ( .class file)