Title: Object Oriented Programming
1Object Oriented Programming
2Mapping the world to software
- Objects in the problem domain are mapped to
objects in software
3Object Oriented
- Data and operations are grouped together
WombatWorld
4Data Encapsulation
class Wombat private int direction
private int leavesEaten public Wombat()
public move()
5Advantages of Encapsulation
- Protection
- Consistency
- Allows change
6Objects and Classes
- Classes reflect concepts, objects reflect
instances that embody those concepts.
object
class
woman
Not Fred
7Objects and Classes contd
Operations MakeDesposit Transfer WithDraw GetBalan
ce
Class BankAccount Balance InterestYTD Owner Accoun
t_number
Balance500 InterestYTD 10 Owner
Mark Account_number 12345
Balance 10,000 InterestYTD 25 Owner
Martha Account_number 76543
8Objects as instances of Classes
- The world conceptually consists of objects
- Many objects can be said to be of the same type
or class - My bank account, your bank account, Bill Gates
bank account - We call the object type a class
9Instantiation
- An Object is instantiated from a Class
BankAccount myAccount myAccount new
BankAccount()
10Objects and Classes
- Class
- A template for objects
- Visible in source code
- Object
- Own copy of data
- Active in running program
- Occupies memory
- Has the set of operations (i.e., functions)
defined in the class
11Classification
12Inheritance
- A class which is a subtype of a more general
class is said to be inherited from it. - The sub-class inherits the base class data
members and member functions
13Inheritance contd
- A sub-class has all the data members of its
base-class plus those that it declares - A sub-class has all member functions of its base
class (with changes) plus those that it defines - More on this later.
14What is a good class ?
- A class abstracts real-world objects
- A class should be non-trivial in the context of
the program (it has data and operations different
from other classes)
15Class Design
- Use is a or has a to determine whether
something should be a subclass or be a data item. - Ex
- A dog has a name. Not A dog is a name.
- A dog is a mammal. Not A dog has a mammal.
(unless its in predator-mode which isnt a step
in oo design).
16Summary
- What is Object Oriented Programming?
- Object-oriented programming is a method of
implementation in which programs are organized as
collections of objects, each of which represents
an instance of some class, and whose classes are
all members of one or more hierarchy of classes
united via inheritance relationships