Title: CS2
1CS2
- Module 5
- Category Elements of Java
- Topic What's an object?
- Objectives
- Second look at elements of Java
2CS 2
- Introduction to
- Object Oriented Programming
- Module 5
- Elements of Java
- What's an Object?
3Object Oriented Programming
- OOP is all about programming using something
called an object - We'll get much more deeply into this topic later
(after some basic stuff) - But just in case you can't wait...
4Previously...
- public class HelloWorld
- public void greet()
- System.out.println("Hello World!")
-
- public static void main(String argv)
- HelloWorld hw new HelloWorld()
- hw.greet()
-
-
5A Class
A class will be used to define how objects made
from this class will operate
- class Greeter
- String greeting
- public Greeter(String newGreeting)
- greeting newGreeting
-
- public void greet(String name)
- System.out.println(greeting ", " name)
-
-
6Field
A class typically will have one or more
fields which are the data items which the object
holds
- class Greeter
- String greeting
- public Greeter(String newGreeting)
- greeting newGreeting
-
- public void greet(String name)
- System.out.println(greeting ", " name)
-
-
7A Constructor
Classes can have Constructors which contain code
only run when an object is created. Useful for
initialization
- class Greeter
- String greeting
- public Greeter(String newGreeting)
- greeting newGreeting
-
- public void greet(String name)
- System.out.println(greeting ", " name)
-
-
8A Method
Once created an object can be asked to run a
"method" which will do something
normally involving data stored by the
object (plus sometimes data passed in.)
- class Greeter
- String greeting
- public Greeter(String newGreeting)
- greeting newGreeting
-
- public void greet(String name)
- System.out.println(greeting ", " name)
-
-
9Another Class
This class is just being used to hold a main
method. Here we make two Greeter objects and use
them
- class Driver
- public static void main(String args)
- Greeter g1 new Greeter("Hello")
- Greeter g2
- g2 new Greeter("Good morning")
- g1.greet("Bob")
- g2.greet("Mary")
- g1.greet("Sam")
- g2.greet("Joan")
-
-
-
Cgtjava Driver Hello, Bob Good morning,
Mary Hello, Sam Good morning, Joan Cgt
10We can also...
- class Greeter
- String greeting
- public Greeter(String newGreeting)
- greeting newGreeting
-
- public void greet(String name)
- System.out.println(greeting ", " name)
-
- public void setGreeting(String chgGreeting)
- greeting chgGreeting
-
- public String getGreeting()
- return greeting
-
We can even write methods to change or retrieve
the value of the fields.
11A Third Class
Another class just holding a main method. Here
we make a Greeter object and modify it.
- class Driver2
- public static void main(String args)
- Greeter g2
- g2 new Greeter("Good morning")
- g2.greet("Bob")
- System.out.println("Current "g2.getGreeting())
- g2.setGreeting("Good evening")
- g2.greet("Mary")
-
-
-
Cgtjava Driver2 Good morning, Bob Current Good
morning Good evening, Mary Cgtgt
12OOP
- OOP consists of designing a bunch of classes most
of which will be used as templates to make
objects - The objects will be used to hold data and will be
initialized with constructors and perform useful
functions by being asked to run their methods - As we will see they can even be used to make
sophisticated graphical applications
13Demo
14Questions?
15(No Transcript)