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.
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.