24
Working With Objects Tonga Institute of Higher Education

Working With Objects Tonga Institute of Higher Education

Embed Size (px)

Citation preview

Page 1: Working With Objects Tonga Institute of Higher Education

Working With Objects

Tonga Institute of Higher Education

Page 2: Working With Objects Tonga Institute of Higher Education

Introduction The building block of an object-oriented language is an

object. Object - A self-contained entity that contains data and

procedures to manipulate the data. An object is like a tool that we can use to do things. In Java, almost everything is an object.

String Integer System PrintStream Etc.

Java has over 2,700 pre-built objects for us to use. You can find a list of pre-built objects in the Java

Developer’s Kit documentation.

Page 3: Working With Objects Tonga Institute of Higher Education

Advantages of Object Oriented Programming

Code is easier to maintain Code is more readable Encapsulation

Show what the object can do This is normally what we want to know

Hide how the object does it This can be very complicated We often don’t care how it is done

Code is easier to re-use A piece of code, once written, should not be thrown away. It

is best to re-use the code in other programs. Example: Millions of people use System.out.println(). But it

was only written once. Code development is more efficient

You don’t code the same thing over and over again

Page 4: Working With Objects Tonga Institute of Higher Education

Advantage of Encapsulation Show what the object can do.

This is accomplished by exposing the signature. Signature – The combination of method name and parameters

used to uniquely identify a method.

Hide the code that the object uses. The person using the object doesn’t know how it works.

Therefore, we can change the code in a method and don’t need to update the programs using it if we don’t change the signature.

If others are using your signature, do not change it! If you do, you will cause everybody using your object to crash.

class Math {public double getPI() {

return 3.14;}

}

class Math {public double getPI() {

return 3.1415926;}

}

Page 5: Working With Objects Tonga Institute of Higher Education

Objects Objects are like primitive data types with extra

functionality. Generally, use them like primitive data types. Except:

Capitalize the first letter of an object data type Ex: byte vs. String

They have methods that provide extra functionality Look at Java documentation to find functionality. Examples:

String String.charAt(int index) String.compareTo(String anotherString)

Date Date.getHours()

Character Character.compareTo(Character anotherCharacter)

They have constructors that let you create them. Primitive data types can have object wrappers.

Ex: The object wrapper for the int primitive data type is Integer.

Page 6: Working With Objects Tonga Institute of Higher Education

Classes vs. Objects Object - A self-contained entity that contains

data and procedures to manipulate the data. Class - The blue print or design for creating an

object.

Instantiate – The act of creating an object from a class

Instance – An instantiated class/object

Page 7: Working With Objects Tonga Institute of Higher Education

Using Object Variables

2 Steps to using variables1. Declare the variable

2. Instantiate the variable / Initialize the variable

Page 8: Working With Objects Tonga Institute of Higher Education

Declaring Object Variables – 1 Declare the variable – Tell the computer to reserve a space in memory

for the variable.

You need to tell the computer 2 things:1. Name of the variable2. Type of the variable (What kind of variable you have)

Object types Integer String

This works exactly thesame for primitive variables!

Type Name

Page 9: Working With Objects Tonga Institute of Higher Education

Declaring Object Variables – 2

Use a name that is easy to remember. Do not use x, y, z

Variable names must start with a letter, underscore or dollar sign.

Variables should begin with a lowercase character. Then a capital letter for each next word.

Examples firstName customerID

This works exactly the same for primitive variables!

Page 10: Working With Objects Tonga Institute of Higher Education

Instantiating Object Variables / Initializing Object Variables Instantiate – The act of creating an object from a class

Use the new keyword Initialize the variable – Assign an initial value to a variable.

A newly instantiated object can be used to initialize a variable Char values must be enclosed in single quotes. String values must be enclosed in double quotes.

New Keyword Type of Object

Parameters may not be required

Page 11: Working With Objects Tonga Institute of Higher Education

Declaring and Initializing Object Variables in 1 line

You can declare and initialize a variable in 1 line.

Page 12: Working With Objects Tonga Institute of Higher Education

Strings are Special

They are objects, but you can use them like primitives

Is the same as

Page 13: Working With Objects Tonga Institute of Higher Education

Demonstration

Declaring, Instantiating and Initializing Variables

Page 14: Working With Objects Tonga Institute of Higher Education

Constructors

Constructor – A method that is automatically executed when an object is created. This allows you to set initial values for the object. Many objects have multiple constructors. (They are overloaded)

You can find a list of constructors in the Java Developer’s Kit documentation.

Page 15: Working With Objects Tonga Institute of Higher Education

Demonstration

Constructors

Page 16: Working With Objects Tonga Institute of Higher Education

Attributes / Fields

Attributes / Fields – A variable that a class allows others to see Use dot notation to access it

Example: <Object name>.<field name> Example: JOptionPane.INFORMATION_MESSAGE

You can find a list of fields in the Java Developer’s Kit documentation.

Page 17: Working With Objects Tonga Institute of Higher Education

Demonstration

Attributes / Fields

Page 18: Working With Objects Tonga Institute of Higher Education

Methods

Methods - Pieces of code that perform a single function Use dot notation to access it

Example: <Object name>.<method name>(<parameters>) You can find a list of methods in the Java Developer’s Kit

documentation.

Calling a Method – The act of using a method

We’ve already used a lot of methods: println(…) main(…) intValue(…)

Page 19: Working With Objects Tonga Institute of Higher Education

Method Inputs

Some methods take inputs Parameter/Arguments – A piece of information that provides additional

information to the method as to how it should behave. Parameters should be in the parenthesis next to the method name The order they are passed is important Values are separated by commas Even if you aren’t passing any parameters, you still need to use ()

Example: Integer.intValue()

MethodInput Output

Input InformationMethod Name

Page 20: Working With Objects Tonga Institute of Higher Education

Method Outputs

Some methods return outputs When something is returned, it may or may not be used.

The programmer chooses what to do with the data returned. Only one thing may be returned. Void means nothing is coming back

Function – A method that returns a value

MethodInput Output

OutputInformation

Page 21: Working With Objects Tonga Institute of Higher Education

Demonstration

Methods

Page 22: Working With Objects Tonga Institute of Higher Education

Method Overloading

If two methods do the same thing, they should have the same name Overloading - Having multiple methods with the same name but

different parameters Java determines the correct method to use by matching up the

number and type of arguments. Therefore, you can’t have 2 methods with the same name and same

number & type of arguments. Without overloading, we would have to remember more function

names. That would make code more complicated.

Page 23: Working With Objects Tonga Institute of Higher Education

Demonstration

Method Overloading

Page 24: Working With Objects Tonga Institute of Higher Education

Members

Member – An attribute/field or method. Sometimes used to refer to

attributes/fields and methods as a whole.