Objects and Classes
Part 1 – Concepts of OOP
Chapter 4, Core Java, Volume I
1
Contents
▪ Procedural Programming vs Object-Oriented Programming
▪ Classes and Objects
▪ Data Abstraction
▪ Encapsulation
▪ Inheritance and Polymorphism
▪ Identifying Classes and Methods
▪ Relationship between Classes
2
Procedural Programming vs Object-Oriented Programming
▪ Procedural Programming (including Structured Programming)
• Program = Algorithms + Data Structures (Niklaus Wirth, 1975)
• A program = a set of procedures
• Procedures operate on shared data.
• A program excutes by calling
D1 D1 … Dk
sequence of procedure calls.
P1 P2 P3
▪ Object-Oriented Programming
P3
• A program = a set of classes
• A class(or object) = Data + Methods
• A class describes the structure of objects
O1 O2 C1
• Program execution is viewed as a
collaboration of objects C2
…
O3 Cn
3
Classes and Objects
▪ Class
• The template or blueprint from which objects are created. (Think about a cookie cutter)
• Describes object data (instance variables or fields) and behavior (methods or functions).
▪ Object
• Instance of a class (A class is considered a type).
Class Name
• Three components
Instance
- Methods – defined in its class Variables
- Its own copy of instance variables Methods
- Identity
▪ Message Sending (or Method Call)
• An object can call a method in another object. anObject anotherObject
(id) (id)
instance instance
variables variables
different states
4
Inheritance
▪ Inheritance
• Define a class using existing classes
• Superclass-subclass relationship
• Increase code reusability
- A subclass inherits the data and methods from its superclasses
• Denote subtype relationship (in Java)
- is-a relationship (e.g. Animal a = new Bear();)
Animal superclass
Bear Wolf subclass
5
Data Abstraction
▪ Data Abstraction
• Separation of essentials (external behavior) and
inessentials(implementation details) of an object
• Focus on external behavior (method signature and functionality) Class
• Ignore implementation details (data structure and method
implementation) method-1
external implementation
• Primarily addressed during the design phase …
behavior details
• Classes are units of data abstraction
method-k
▪ Example: Stack
• Focus on the behavior of pushing or popping elements at the top of the stack
• Ignore the underlying structure of the stack or how push and pop are implemented
• The client doesn’t care about the implementation details (e.g. data structure using an array
or a linked list)
6
Encapsulation
▪ Encapsulation
• Bundling of an object’s state(data) and behvaior(methods) into
a single unit (a class in OOP)
• Controlling access to the object’s internal state and behavior
Class
• The behavior is exposed while the internal state is protected.
• Primarily addressed during the implementation phase
method-1
• Classes are units of encapsulation implementation
external
… details
behavior
method-k
▪ Example: Stack
• Expose the push and pop methods to the public
• Hide the data structure of the stack (its container and the top index)
• The client cannot directly access the internal data of the stack(e.g. its container or top index)
7
Polymorphism
▪ Polymorphism
• Providing a single interface(such as methods or operators) to entities of different types
- overloading, coercion, parameteric polymorphism, subtype polymorphism
• Subtype polymorphism (in object-oriented languages) is important
e.g.
Animal a = new Bear();
[Link]();
Animal b = new Wolf();
[Link]();
• Subtype polymorphism is related to the inheritance and dynamic binding
• Increase extensiblity
Animal
Bear Wolf
8
Identifying Classes and Methods
▪ How to start object-oriented programming
• Identify classes
• Identify attributes(data) of the class
• Identify methods of the class (assignment of responsibility)
▪ Identifying classes and methods (a rule of thumb)
• Nouns are often classes
• Verbs are often methods
▪ Example : Order-processing system
Items are added to orders
Orders are shipped or canceled
Payments are applied to orders
…
• Classes : Item, Order, Payment, etc.
• Methods : add an item, ship an order, cancel an order, apply a payment to order
9
Relationship between Classes
▪ The most common relationships between classes
• Association (“use-a”) ( )
• Aggregation and Composition (“has-a”) ( , )
• Inheritance (“is-a”) ( )
▪ Examples
• an Order object uses an Account object
• an Order object has some Item objects
• a RushOrder object is an Order object
▪ UML (Unified Modeling Language)
• Class Diagram
• Sequence Diagram
• Activity Diagram
• Use Case Diagram
• …
▪ Object-Oriented Analysis and Design (Object-oriented Development Methodology)
10