A programming language which allows the modelling of object interaction.
Java is an OO programming language.
Typically an OO programming language supports:
Encapsulation (via the notion of class)
Inheritence: method for extending existing classes by adding methods and fields
Polymorphism: Behaviour varies depending on the actual type of an object
OO Programming Languages encourage code reuse, capturing of concepts.
References
A Reference holds the address of an instance
A reference is NOT the instance
Speed Dial analogy (no Inheritence)
The VALUE of a reference is the address it holds
a=b calculated by computing the VALUE of the right side and placing it in the left
Java is pass by VALUE
Java occasionally garbage collects instances that have no references to them.
Using Objects
Circle c=new Circle(); does the following
Create a new instance of circle
Call the initialization code for a new Circle (constructor)
Create a new reference to Circle called c
Assign the address of the newly created/initialized circle to c
Exercise: Using the Shapes code below (and drJava) create a face (circles for eyes, square for nose, rectangle for mouth).
Remember to import java.awt.*; so you can use Color.red etc.
Example: Create a few references to circle, change which reference refers to which circle
and call methods on them. Explain.
Our goals...
Problem solve: Understand a given problem (there is a LOT to this!!) Determine if a solution takes the form of a computer program (and if so)...
Create Conceptual Solution: Describe the system to be modelled. That is, give an english description of the system
Concepts mapped to OO Paradigm:
Think OO:Analyze the problem/solution, view it as a collection
of interacting objects, then identify the
classes/objects. These are the concepts that need to be captured. Sometimes noun or noun phrases in the above description.
methods (RESPONDS-TO): for each class. These are the things that the class can do. Verbs usually identify these in the problem description.
attributes (HAS-A): for each class. These are things that each instance has, they define the state of the instance.
inheritence (IS-A): classes that this class extends
Think Algorithmically:Write algorithms for each of the methods
Java: Translate the above to something the computer can understand (Java).
Jug Exercise
Our goal will be to write a program which will allow a user to play the Jug puzzle.
Problem Statement:
There are three water jugs, which can hold 8, 5 and 3 gallons of water
respectively. The first jug is full, and the other two are empty.
It is necessary to divide the water into two equal portions, that is,
to put exactly four gallons into both the 8 and 5 gallon jugs.
The only operation allowed is to pour some or all of the contents of one
jug into another.
Can the water be divided into two equal portions? How?
For the Jug class, identify
IS-A: inheritance (extends)
HAS-A: attributes
RESPONDS-TO: methods (including toString(), the method that allows a user to fetch
a String representation of an instance).
constructors: special initialization code used when creating a new Jug()
Inheritence (IS-A): method for extending existing classes by adding methods and fields
Subclass and superclass, what it means Subclass inherits all methods and attributes of superclass. (ie Shapes UML Diagram)
Object: is the superclass of all classes.
extends: used to define subclass/superclass relation between two classes.
Method overriding: Method in subclass has same name as method in superclass. In Java,
method search proceedes from class of instance back towards Object.
Polymorphism: behaviour varies depending on the actual type of an object.
Speed Dial example (extended)
Exercises
Create a Shapes reference and use it to point to different Shapes and move them.