Lecture 03
Lecture 03
Lecture 03
5
Classes and Objects
•Object
– A chunk of structured data in a running software
system
– Has properties
• Represent its state
– Has behavior
• How it acts and reacts
• May simulate the behavior of an object in the real
world
6
Objects
7
Classes
•A class:
– A unit of abstraction in an object oriented
(OO) program
– Represents similar objects
• Its instances
9
Instance Variables
•Variables defined inside a class corresponding
to data present in each instance
– Also called fields or member variables
– Attributes
• Simple data
• E.g. name, dateOfBirth
– Associations
• Relationships to other important classes
• E.g. supervisor, coursesTaken
11
Variables vs. Objects
•A variable
– Refers to an object
– May refer to different objects at different points in
time
•An object can be referred to by several different
variables at the same time
•Type of a variable
– Determines what classes of objects it may contain
12
Class variables
•A class variable’s value is shared by all instances
of a class.
– Also called a static variable
– If one instance sets the value of a class variable, then
all the other instances see the same value.
– Class variables are useful for:
• Default or ‘constant’ values (e.g. PI)
• Lookup tables and similar structures
•Operation
– A higher-level procedural abstraction that
specifies a type of behaviour
14
Operations and Methods
•Method
– A procedural abstraction used to implement the
behaviour of a class
16
Organizing Classes into Inheritance Hierarchies
•Super-class
– One class contains features common to a set
of subclasses
•Inheritance hierarchies
– Show the relationships among super-classes
and sub-classes
– A triangle shows a generalization
•Inheritance
– The implicit possession by all subclasses of
features defined in its super-classes
17
An Example Inheritance Hierarchy
•Inheritance
– The implicit possession by all subclasses of
features defined in its superclasses
18
The Is a Rule
•Always check generalizations to ensure they
obey the is a rule
– “A checking account is an account”
19
A possible inheritance hierarchy of
mathematical objects
MathematicalObject
Shape2D Shape3D
Circle Quadrilateral
Rectangle 20
Make Sure all Inherited Features Make
Sense in Subclasses
21
Inheritance, Polymorphism and Variables
26
Concepts that Define Object Orientation
•The following are necessary for a system to be OO
• Classes
– The code is organized using classes, each of which
describes a set of objects
• Abstraction
– Object -> something in the world
– Class -> objects
– Superclass -> subclasses
– Operation -> methods
– Attributes and associations -> instance variables
27
Concepts that Define Object Orientation
• Inheritance
• The mechanism where features in a hierarchy inherit from
superclasses to subclasses
• Polymorphism
• The mechanism by which several methods can have the same
name and implement the same abstract operation.
• Encapsulation
– Details can be hidden in classes
– This gives rise to information hiding:
• Modularity
– Code can be constructed entirely of classes
28
Overview of Java
•The next few slides will remind you of several
key Java features
– Not in the book
– See the web site:
http://www.site.uottawa.ca/school/research/llose
ng/supportMateria
– for
• A more detailed overview of Java
• Pointers to tutorials, books etc.
29
Casting
•Java is very strict about types
– If variable v is declared to have type X, you can
only invoke methods on v that are defined in X
or its superclasses
– If you know an instance of a subclass is stored,
then you can cast the variable to the subclass
• E.g. if I know a list i contains instances of String, I can
get the next element of its Iterator using:
(String)i.next();
• To avoid casting you could also have used templates::
a = ArrayList<String>;
i=a.iterator();
32
i.next()
Exceptions
•Anything that can go wrong should result in the
raising of an Exception
– Exception is a class with many subclasses for
specific things that can go wrong
•Use a try - catch block to trap an exception
try
{
// some code
}
catch (e)
{
// code to handle exception
} 33
Packages and importing
•A package combines related classes into
subsystems
– All the classes in a particular directory
•Classes in different packages can have the
same name
– Although not recommended
•Importing a package is done as follows:
import finance.banking.accounts.*;
34
Access control
•Applies to methods and variables
– public
• Any class can access
– protected
• Only code in the package, or subclasses can access
– private
• Only code written in the class can access
• Inheritance still occurs!
– (blank)
• Only code in the package can access
35
Programming Style Guidelines
•Remember that programs are for people to
read
– Always choose the simpler alternative
– Reject clever code that is hard to understand
– Shorter code is not necessarily better