2.4 Creating and Using Objects. Writing the code for classes of your own will come later. At this time it is possible to understand and correctly write.

Slides:



Advertisements
Similar presentations
The Line Class Suppose you are involved in the development of a large mathematical application, and this application needs an object to represent a Line.
Advertisements

METHOD OVERRIDING Sub class can override the methods defined by the super class. Overridden Methods in the sub classes should have same name, same signature.
Variable types We have already encountered the idea of a variable type. It is also possible to think about variables in terms of their kind, namely: 1)
 It is possible to declare names for object references and not assign object references to them.  Such names literally refer to nothing at all.  It.
Prof. Fateman CS 164 Lecture 151 Language definition by interpreter, translator, continued Lecture 15.
26-Jun-15 Methods. About methods A method is a named group of declarations and statements You execute those declarations and statements by calling the.
Copyright 2008 by Pearson Education Building Java Programs Chapter 8 Lecture 8-2: Object Methods and Constructors reading: self-checks: #1-12.
Writing Methods. Create the method Methods, like functions, do something They contain the code that performs the job Methods have two parts.
Introduction to Methods
Unit 2: Java Introduction to Programming 2.1 Initial Example.
Copyright 2010 by Pearson Education Building Java Programs Chapter 8 Lecture 8-2: Object Behavior (Methods) and Constructors reading:
2.2 Information on Program Appearance and Printing.
Writing Classes (Chapter 4)
© The McGraw-Hill Companies, 2006 Chapter 4 Implementing methods.
4.1 Instance Variables, Constructors, and Methods.
Week 4 Introduction to Computer Science and Object-Oriented Programming COMP 111 George Basham.
Unit 3: Java Data Types Math class and String class.
JAVA BASICS: Variables and References SYNTAX, ERRORS, AND DEBUGGING.
Java Quiz Bowl A fun review of the Java you should know from CMPT 201 If you don’t know the answers - this week is for you to study up!
Inheritance and Polymorphism Daniel Liang, Introduction to Java Programming.
CS 320 Assignment 1 Rewriting the MISC Osystem class to support loading machine language programs at addresses other than 0 1.
Output in Java Hello World!. Structure of a Java Program  All Java files in ICS3U1 have the following structure: class HelloWorld { }  Notice the open.
Introduction to programming in the Java programming language.
1 CISC181 Introduction to Computer Science Dr. McCoy Lecture 7 Clicker Questions September 22, 2009.
An Object-Oriented Approach to Programming Logic and Design Chapter 3 Using Methods and Parameters.
Chapter 5: Ball Worlds Features 2 classes, constant data fields, constructors, extension through inheritance, graphics.
Classes In C++ 1. What is a class Can make a new type in C++ by declaring a class. A class is an expanded concept of a data structure: instead of holding.
OOP in Java : © W. Milner 2005 : Slide 1 Java and OOP Part 2 – Classes and objects.
1 Object state: fields. Fields field: A variable inside an object that represents part of its internal state.  Each object will have its own copy of.
1 BUILDING JAVA PROGRAMS CHAPTER 2 Pseudocode and Scope.
Factory Method Explained. Intent  Define an interface for creating an object, but let subclasses decide which class to instantiate.  Factory Method.
CS305j Introduction to Computing Classes 1 Topic 23 Classes – Part I "A 'class' is where we teach an 'object' to behave." -Rich Pattis Based on slides.
CSI 3125, Preliminaries, page 1 Compiling the Program.
24-Dec-15 Class Structure. 2 Classes A class describes a set of objects The objects are called instances of the class A class describes: Fields (instance.
CS305j Introduction to Computing Classes II 1 Topic 24 Classes Part II "Object-oriented programming as it emerged in Simula 67 allows software structure.
What am I? while b != 0 if a > b a := a − b else b := b − a return a AST == Abstract Syntax Tree.
CSI 3125, Preliminaries, page 1 Class. CSI 3125, Preliminaries, page 2 Class The most important thing to understand about a class is that it defines a.
Lecture 08. Since all Java program activity occurs within a class, we have been using classes since the start of this lecture series. A class is a template.
Java: Variables and Methods By Joshua Li Created for the allAboutJavaClasses wikispace.
5.1 Basics of defining and using classes A review of class and object definitions A class is a template or blueprint for an object A class defines.
Chapter 3 Implementing Classes
Tarik Booker CS 242. What we will cover…  Functions  Function Syntax  Local Variables  Global Variables  The Scope of Variables  Making Functions.
Copyright 2008 by Pearson Education Building Java Programs Chapter 8 Lecture 8-2: Object Methods and Constructors reading: self-checks: #1-12.
MT311 Java Application Development and Programming Languages Li Tak Sing( 李德成 )
9.1 CLASS (STATIC) VARIABLES AND METHODS Defining classes is only one aspect of object-oriented programming. The real power of object-oriented programming.
Summary prepared by Kirk Scott
Building Java Programs
9.1 Class (static) Variables and Methods
Unit 2: Java Introduction to Programming
Class Structure 15-Jun-18.
Some Eclipse shortcuts
10.2 Implementation and Execution of Recursive Code
Using local variable without initialization is an error.
Class Structure 16-Nov-18.
Writing Methods.
Class Structure 28-Nov-18.
Building Java Programs
CHAPTER 6 GENERAL-PURPOSE METHODS
Class Structure 7-Dec-18.
Building Java Programs
Class Structure 2-Jan-19.
Class Structure 25-Feb-19.
Building Java Programs
Java Programming with BlueJ Objectives
Building Java Programs
Chapter 11 Inheritance and Polymorphism Part 1
Building Java Programs
Lecture 8-2: Object Behavior (Methods) and Constructors
Building Java Programs
Building Java Programs
Presentation transcript:

2.4 Creating and Using Objects

Writing the code for classes of your own will come later. At this time it is possible to understand and correctly write lines of code and small, complete programs that construct objects from system supplied classes and then make use of them.

Assuming that the Point class has been imported into a program, the following two lines of code 1) Declare a name which can be used for a Point object, and 2) Construct an object and give it that name, using the “=” sign, or assignment to do so: Point myPoint; myPoint = new Point(10, 20);

This is the specific syntax for calling a constructor and passing it parameters: new Point(10, 20); In order to call a constructor, the keyword “new” has to be used. The call shown will initialize the x and y coordinate values of the constructed point to 10 and 20, respectively. The two lines given above are frequently compressed into a single line of code: Point myPoint = new Point(10, 20);

When you construct an object and give it a name, you can think of this as putting a handle on the object. The name, “myPoint” is the handle. You can use this handle later on to refer to the object and manipulate it. To make this idea clearer, it is possible to consider the following example, where an object is created, but it is not given a name: new Point(10, 20);

The code is syntactically valid. It will not cause compile time or run time errors. It causes a Point object to be created. However, the object cannot be accessed in the rest of the program because it doesn’t have a name.

In the discussion above, the ideas were explained in terms of “giving an object a name”. The more technical term, which will be used in the rest of these notes, is “object reference”. A call to a constructor returns an unnamed reference to an object. When you declare a name, such as myPoint, and assign the unnamed reference to that name, the name becomes a reference to the object.

The idea of named and unnamed references can be illustrated further with another example. Consider the following line of code, which is syntactically correct. The println() method will accept the reference returned by a call to the constructor for a Point, even though the object is unnamed. This reference is a valid parameter and println() will print out information about the object referred to. System.out.println(new Point(10, 20));

Code where one call is contained in another is not particularly easy to read or understand, and the use of unnamed references is generally not very clear. This example is not given because it illustrates good code writing. It is given because it concretely illustrates that a call to a constructor causes an object to come into existence even if it remains unnamed.

If an object has been constructed and given a name, methods can be used on it. The Point class has a method translate(), which has the effect of shifting the location of a point in the plane by adding or subtracting values to its x and y coordinates. Here is an example of a line of code where a method is called on an object: myPoint.translate(30, 40);

The call takes this form: An object reference, a dot, a method name, and a parameter list. It is the dot which signifies the call of the method on the object. It is important to note the syntactic difference between calling a constructor and a method. The method call does not use the keyword “new”.

The effect of executing this line of code is to shift the point which was constructed above with x and y coordinates of 10 and 20 to a new location with an x coordinate of , or 40, and a y coordinate of , or 60. The line of code making the method call does not take the form of an assignment.

Values are assigned to instance variables as a result of the call, but the work of doing the assignments is hidden inside the method code. The Point class is a black box because it is not necessary to see the method code in order to correctly make use of the method.

Here is a synopsis of the basic method calling pattern, which will be repeated over and over again, with variations: object.method(parameters);

At this stage there is one more thing we can do with the object: Print it out. The printing methods will accept object references as parameters. If the object exists, this is a valid line of code: System.out.println(myPoint);

We are not yet doing graphical programming, and the output looks like this: java.awt.Point[x=10,y=20] The system is simply telling the type of the reference, namely, the class of the object, and what the current values of its instance variables are.

It is now possible to write an example program which includes all of the features described above: import java.awt.Point; /* This is the second example program. */ public class SecondProg { public static void main(String[] args) { Point myPoint = new Point(10, 20); System.out.println(myPoint); myPoint.translate(30, 40); System.out.println(myPoint); }