Class Notes (1,100,000)
CA (620,000)
U of C (8,000)
CPSC (100)
Lecture 15

CPSC 233 Lecture Notes - Lecture 15: Instance Variable, Multiple Inheritance, Enumerated Type


Department
Computer Science
Course Code
CPSC 233
Professor
Leonard Manzara
Lecture
15

Page:
of 3
CPS 233 - 12 Feb Fri
Arrays of Object
You can eclare an array of object references
o General form
ClassName[] arrayName = new ClassName[size]
o (E.g.)
point[] p = new Point[3]
This not create 3 Point objects , but only an array of object references
o You could create the actual objects in a loop:
for (int i = 0; i < p.length; i++)
p[i] = new Point();
Arrays of Strings
Can easily be created with an initializer list
o (E.g.)
String[] stooge = {"Larry", "Curly", "Moe"};
o This is equivalent to
String[] stooge = new String[3];
stooge[0] = "Larry"
stooge[1] = "Curly""
stooge[2] = "Moe"
Enhanced for Loop
Suitable only when iterating through all elements of an array or Collection
class
General form
for (array_base_type var: array_name)
statement;
(E.g.)
double[] myArray = new double[20];
. . .
for (double element : myArray)
element = 0.0;
Enumerated Type
Are used to create custom data types consisting of a list of named constants
enum typeName {VALUE_1, VALUE_2, . . . VALUE_N};
(E.g.)
enum Season {SPRING, SUMMER, FALL, WINTER};
This custom data type can be used to declare variables
(E.g.)
Season highSeason, lowSeason;
Constants re assigned to these variables using the general form:
var = typeName.VALUE_X;
(E.g.)
highSeason = Season.SUMMER;
An enumerated type is actually a class
o The values are objects of the class
o Has usefil methods such as toString() and valueOf()
(E.g.) implicit use of toSring():
System.out.println(highSeason); -> (Prints out SUMMER)
Can be used in switch assignments
Relationships Among Classes
Inheritance
Classes may be related by inheritance
o A subclass (below class) inherits behavior (instance methods) and structure
(instance variable) from its superclass (above class)
o A subclass may also augment or redefine the behavior and structure
inherited from its superclass
o Iheritace idicates a is a kid of relatioship
Classes related by inheritance from a class hierarchy
o Also called a generalization/specialization hierarchy
Inheritance helps create compact code
o General functionality is put into superclasses
o Specialized functionality is added to the general case by creating the
appropriate subclass
Often you subclass from existing classes in a library
Saves you much labor
o The subclass reuses code from the superclass for non-specialized
functionality
Single Inheritance
o Where subclasses can have only one immediate superclass
(E.g.)
mammal
canine feline primate
dog wolf fox lion tiger puma chimpanzee gorilla orangutan
Multiple Inheritance
o Where subclass may have more than one immediate superclass
(E.g.)
teacher researcher
professor
“oe laguages e.g. Java do’t support ultiple iheritace