Download presentation
Presentation is loading. Please wait.
Published byDeirdre Hodges Modified over 9 years ago
1
Working With Objects Tonga Institute of Higher Education
2
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.
3
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
4
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 ; }
5
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.
6
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
7
Using Object Variables 2 Steps to using variables 1. Declare the variable 2. Instantiate the variable / Initialize the variable
8
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 variable 2. Type of the variable (What kind of variable you have) Object types Integer String This works exactly the same for primitive variables! Type Name
9
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!
10
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 KeywordType of Object Parameters may not be required
11
Declaring and Initializing Object Variables in 1 line You can declare and initialize a variable in 1 line.
12
Strings are Special They are objects, but you can use them like primitives Is the same as
13
Demonstration Declaring, Instantiating and Initializing Variables
14
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.
15
Demonstration Constructors
16
Attributes / Fields Attributes / Fields – A variable that a class allows others to see Use dot notation to access it Example:. Example: JOptionPane.INFORMATION_MESSAGE You can find a list of fields in the Java Developer’s Kit documentation.
17
Demonstration Attributes / Fields
18
Methods Methods - Pieces of code that perform a single function Use dot notation to access it Example:. ( ) 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(…)
19
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() Method InputOutput Input InformationMethod Name
20
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 Method InputOutput Information
21
Demonstration Methods
22
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.
23
Demonstration Method Overloading
24
Members Member – An attribute/field or method. Sometimes used to refer to attributes/fields and methods as a whole.
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.