1
Principles of Computer Science II
- Prof. Nadeem Abdul Hamid
CSC 121A – Spring 2005 Lecture Slides 2 - OO Review, Scope, and Inheritance
CSC 121A - Berry College - Spring 2005 2
Object-Oriented Programming
Everything is an object Program is a bunch of objects – Tell each other what to do by sending messages (calling methods) Each object has its own memory made up of
- ther objects
Every object has a type – Each object is an instance of a class All objects of a particular type can receive
the same messages
CSC 121A - Berry College - Spring 2005 3
Definition of an Object
An object has state, behavior and identity.
[Booch]
– State: internal data (fields or instance variables) – Behavior: methods – Identity: each object uniquely distinguished from others; i.e. has a unique address in memory
CSC 121A - Berry College - Spring 2005 4
Scope
Determines the visibility and lifetime of variables In Java, scope determined by placement of curly
braces {}
{ int x = 12; // Only x available { int q = 96; // Both x & q available } // Only x available // q “out of scope” }
CSC 121A - Berry College - Spring 2005 5
Object Lifetimes
Not the same as primitive variables { String s = “a string”; } // end of scope The reference, s, vanishes at the end of scope String object pointed to is still there Unlike other languages, don’t worry about
cleaning up memory
Java uses a garbage collector to figure out which
- bjects are no longer in use and reclaim their
memory
CSC 121A - Berry College - Spring 2005 6
Access Specifiers
Every class member may have an access
specifier before it: public/protected/private
Package access – Also known as “friendly”: default access which applies when you don’t specify access – All classes in the same package have access to that class member (field/method) – To all classes outside the package, the member appears private