240 likes | 254 Views
Learn the fundamentals of object-oriented programming and how to work with objects in Java, including encapsulation, instantiation, and object variables. Explore the advantages of object-oriented programming and understand the difference between classes and objects.
E N D
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.
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
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; } }
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.
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
Using Object Variables • 2 Steps to using variables • Declare the variable • Instantiate the variable / Initialize the variable
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: • Name of the variable • Type of the variable (What kind of variable you have) • Object types • Integer • String • This works exactly the same for primitive variables! Type Name
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!
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. Parameters may not be required New Keyword Type of Object
Declaring and Initializing Object Variables in 1 line • You can declare and initialize a variable in 1 line.
Strings are Special • They are objects, but you can use them like primitives Is the same as
Demonstration Declaring, Instantiating and Initializing Variables
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.
Demonstration Constructors
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.
Demonstration Attributes / Fields
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(…)
Method Input Output 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 Name Input Information
Method Input Output 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 Output Information
Demonstration Methods
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.
Demonstration Method Overloading
Members • Member – An attribute/field or method. • Sometimes used to refer to attributes/fields and methods as a whole.