Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "Objects and Classes Chapter Nine. Definition “an object is a combination of some data (variables) and some actions (methods)”. Hopefully the data and."— Presentation transcript:

1 Objects and Classes Chapter Nine

2 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

3 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.

4 Balloon will be seen again in Chapter 10 In chapter 10 with more “Bells and whistles” page 172-173

5 Basics of Balloon A circle “balloon” which you easily could be made more interesting

6 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

7 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)

8 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

9 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

10 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

11 // some methods in the class (public) can be seen the private ones can not. myBalloon = new Balloon (20, 50, 50); diameterX coord Y coord

12 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

13 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); }

14 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( ); }

15 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

16 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”

17 // an example private 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

18 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))

19 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

20 “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)


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

Similar presentations


Ads by Google