Session 1 (18 March)
Chapter 2: Object Oriented Programming
Older languages are procedural: you describe a sequence of instructions to be carried out by the computer. Java is object oriented: you describe entities which model the real world and have attributes and behaviors.
show definition
Object orientation is the way a lot of software gets written today. This is one reason intro programming classes use Java now and not Pascal.
Class & Object
The important thing is to model the real world, not the way a computer thinks.
class is a type of object, object is an instance of class
Show definition
example: class Teacher, object me other object Thomas. Class whiteboard, object this whiteboard, etc.
(write down teacher and student classes on board)
Attributes
the class defines the attributes (aka fields), but they get values in the instantiated object. Think of the class as a template.
example: studentId in Student class
example: teacherId in Teacher class
example: name in Student class, name in Teacher class
An attribute can also be an object, example: a reference to a Course object in the Student class.
Actually this is an object reference, similar to a pointer in C. We'll talk more about references in a few weeks.
Behaviors
aka methods: things that the class does
example: Student.takeNotes(), Teacher.gradeTests()
example: Student.attendClass(), Teacher.attendClass()
example: Student.rideBart(), Teacher.rideBart()
Methods can also have arguments, information you pass in when you call the method. example: readChapter(int chapterNum).
Arguments can also be objects, example: signUpForCourse(Course c)
Methods can also return information, which can be a primitive or an object
example: getGrade() returns a char 'A', 'B' etc.
example: getCourse() returns a Course object (reference)
Inheritance
create a parent class (superclass) which is a more general case. The child or subclasses will extend this class.
Simple example: automobile, aircraft, and boat all inherit from vehicle. Sports car, station wagon, SUV, mini-van, and pickup truck all inherit from automobile.
example: BerkeleyExtensionPerson class as parent of both Student and Teacher.
Move name, id attributes to parent
Move rideBart() method to parent
(Don't move attendClass() yet, we will talk about that)
Can refer to an object using either super or subclass type
This is called inheritance
If no explicit superclass, inherits from java.lang.Object (abstract superclass of all Java classes)
Everything in the Java API is a subclass of Object except primitives.
Polymorphism
Some methods have the same signature but the actual behavior is different.
example: attendClass(): create a method in the superclass but keep the methods in the subclasses.
this is called overriding a method. Any Java method can be overriden unless it is declared final.
I can now call BerkeleyExtensionPerson.attendClass() and the object will do the appropriate thing. I don’t care what type of object it is at that point.
This is called polymorphism. Show definition
Encapsulation
It is better to hide most internals of a class by making them private
example: suppose BerkeleyExtensionPerson.name is public. But you want to change it to lastname, firstname. You can’t, because programs already expect it to have a name attribute.
instead use getName() and setName() methods-- called accessor methods.
show how this is more maintainable in case we change name to fname, lname.
this is what we call a social contract between you as the class designer and other developers who use your class.
This is called information hiding or encapsulation. Show definition.
Interfaces
Some languages like C++ support multiple inheritance, but it becomes too complicated in practice.
Java has a limited form of multiple inheritance. A class can extend only one superclass but it can implement any number of interfaces.
An interface is a special kind of class than has method signatures but no defined behavior (no implementation).
example: hyperdrive-enabled ship
Purpose: force classes to follow a specification, also provides for additional polymorphism (refer to class by interface type).
Statics
Classes can have static methods and fields which are given values at the level of the class, not the objects. Also known as class methods and class fields.
static attributes: Good for sharing values across objects of a class or defining constants (static final).
example: Math.PI == 3.14159...
The only common exception to the encapsulation rule above
static methods: Good for behaviors that do not require an object instance
example: main()
example: Integer.parseInt()
Relationships between objects
We have already mentioned in passing, but let's review
inheritance: one class inherits from another (or interface)
an object can be a field value of another object
an object can be a field value of a static field (class field)
an object reference can be passed in to a method
an object can be created and its reference returned from a method
an object might be created within a method and destroyed at the end of the method: this object is local to the method and is not visible outside.
Review
Classes & Objects
(ask for examples)
Attributes
Behaviors
Inheritance
Simplifies code by grouping common functionality into superclasses.
New class only needs to define behavior that is different from superclass -- the rest is inherited by default.
Polymorphism
Simplifies use of objects by eliminating the need to check types when calling virtual methods
Promotes extensibility of classes and interfaces: client program does not need to know in advance about different subtypes.
Encapsulation
Simplifies use of a class by hiding unnecessary (from client's pov) details
Supports social contract between class designer and class user by eliminating extra dependencies
Interfaces
Allows greater range of polymorphic behavior through a limited form of multiple inheritance
Enforces a specification, so one person or group can basically make sure another person or group follows certain rules about how to make classes.
Statics
constants
attribute values shared across object instances
behaviors that do not require object instances
Example hierarchy: http://java.sun.com/j2se/1.3/docs/api/index.html
Sketch what the code would look like
Let's come up with another class hierarchy