Basic Object-Oriented concepts

Basic Object-Oriented concepts

Concept: An object has behaviors

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)

Example: A “Rabbit” object

You could (in a game, for example) create an object representing a rabbit
It would have data:
How hungry it is
How frightened it is
Where it is
And methods:
eat, hide, run, dig

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 are like Abstract Data Types

An Abstract Data Type (ADT) bundles together:
some data, representing an object or « thing »
the operations on that data
Example: a CheckingAccount, with operations deposit, withdraw, getBalance, etc.
Classes enforce this bundling together

Example of a class

class Employee {
// fields
String name;
double salary; // a method
void pay () {
System.out.println(« Pay to the order of  » +
name +  » $ » + salary);
}
}

Cours gratuitTélécharger le cours complet

Télécharger aussi :

Laisser un commentaire

Votre adresse e-mail ne sera pas publiée. Les champs obligatoires sont indiqués avec *