Chapter 1: Basic Object Information

Download Report

Transcript Chapter 1: Basic Object Information

Chapter 1
Object Orientation: Objects
and Classes
Objects and Classes
When you create a program using an
object-oriented language, you are:
– Creating a model of the problem
– The model is built up from objects in the
problem domain
– Those objects must be represented in your
Object vs. Class
Let’s ask some questions about a student:
– What color is his hair?
– How tall is he?
– What is his QCA?
Object vs. Class
In order to answer these questions we
need to know what student we are talking
– This particular student is an instance of a
In this example, the student is a class and
the instance of the student is an object.
Object vs. Class
A class is a generic example; a blue print if
you will.
An object is one particular instance of a
It is sometimes said that you instantiate an
object from a class
JAVA Conventions
Names of Classes start with a Capital
Names of objects start with lower case
This case convention is fairly well followed
throughout the Java programming
Let’s look at an example in BlueJ
Shapes Sample from the Example folder
Calling Methods
When we right clicked on a object on the
workbench in BlueJ, we got a popup menu with
a bunch of function names listed.
In Java, these functions are called methods
When we chose one of these methods for the
object to perform, we called or invoked the
When the method requires information from the
programmer, the method is said to have a
Method Signatures
A method signature provides information
about the method.
– It tells us the name of the method
– Whether or not the method requires a
– What type of information is returned from the
Data Types
The values a parameter can take
– int – whole numbers
– String – a section of text
Pitfall – forgetting the double quotes
around a string. You’ll get a message like
“Error: undefined variable”
Multiple Instances
Object-oriented programming (OOP)
allows for creating more than one object
from the same class.
The internal state of the object allows the
computer to tell the different objects apart.
All of the attributes that define an object is
referred to as the object’s state
Java refers to the individual attributes as
What is an object?
All objects of the same class have the
same fields (and methods).
The values stored in those fields are
The class indicates what fields and
methods each object will have.
Object Interaction
Objects can create other objects and
invoke those objects methods
Most Java programs have objects that will
do just this
Question: How do we do this?
Source Code
Like most programming languages, Java
is written using plain text files or source
code files
The source code tells the program how
objects are created and how the objects
will interact
After a source code file is written, it must
be compiled so that the Java Virtual
Machine (JVM) can interpret it
Return Values
Some methods not only take parameters,
but give something back to you.
These methods are said to have a return
You can tell this in the signature
– String getName()
This methods returns a String
– void moveRight()
This method returns void (nothing)
1.2, 1.5, 1.6, 1.9, 1.11, 1.18, 1.26
Explain the difference between an object
and a class.