05-03-2013, 09:52 AM
Basic Object-Oriented concepts
Basic Object-Oriented.ppt (Size: 85 KB / Downloads: 120)
In old style programming, you had:
data, which was completely passive
functions, which could manipulate any data
An object contains both data and methods that manipulate that data
An object is active, not passive; it does things
An object is responsible for its own data
But: it can expose that data to other objects
Concept: An object has state
An object contains both data and methods that manipulate that data
The data represent the state of the object
Data can also describe the relationships between this object and other objects
Example: A CheckingAccount might have
A balance (the internal state of the account)
An owner (some object representing a person)
Concept: Classes describe objects
Every object belongs to (is an instance of) a class
An object may have fields, or variables
The class describes those fields
An object may have methods
The class describes those methods
A class is like a template, or cookie cutter
Concept: Classes form a hierarchy
Classes are arranged in a treelike structure called a hierarchy
The class at the root is named Object
Every class, except Object, has a superclass
A class may have several ancestors, up to Object
When you define a class, you specify its superclass
If you don’t specify a superclass, Object is assumed
Every class may have one or more subclasses
Concept: Constructors make objects
Every class has a constructor to make its objects
Use the keyword new to call a constructor
secretary = new Employee ( );
You can write your own constructors; but if you don’t,
Java provides a default constructor with no arguments
It sets all the fields of the new object to zero
If this is good enough, you don’t need to write your own
The syntax for writing constructors is almost like that for writing methods
Kinds of access
Java provides four levels of access:
public: available everywhere
protected: available within the package (in the same subdirectory) and to all subclasses
[default]: available within the package
private: only available within the class itself
The default is called package visibility
In small programs this isn't important...right?