01-Introduction
01-Introduction
01-Introduction
[email protected] 1
Agenda
▪ Welcome
▪ Course Information
▪ Brief Introduction to Object Oriented Paradigm
▪ Basic differences between C++ and JAVA
[email protected] 2
Organization
▪ Course Breakdown
▪ Object-oriented design
▪ Encapsulation and information hiding
▪ Separation of behavior and implementation:
▪ Classes, Subclasses and inheritance, Polymorphism
▪ UML and Requirement analysis
▪ Design patterns
[email protected] 4
Course Materials
▪ Many Textbooks!
▪ Objects, Abstraction, Data Structures and Design Using Java
Version 5.0 by Elliot B. Koffman and Paul A. T. Wolfgang
▪ The Essence of Object-Oriented Programming with Java and UML
by Bruce E. Wampler
▪ Object Oriented Programming with Java: Essentials and Applications
by Rajkumar Buyya, Thamarai Selvi Somasundaram, and Xingchen
Chu
▪ Design Patterns in Java by Steven John Mestker and William C.
Wake
▪ Further readings
▪ Clean Code
▪ Clean Coder
▪ And More Online Materials
▪ You can google any design pattern you want!
[email protected] 5
This is NOT …
▪ … a Java Course
▪ Java Vs. Object Oriented Programming
▪ Thinking Objects
▪ Object Oriented Programming Principles
▪ … Just About Programming
▪ Software Development is much more than just programming.
▪ Object Oriented Analysis and Design Principles
▪ Advanced Topics: Design Patterns and Refactoring
[email protected] 6
Course Contents
▪ 01 – Introduction
▪ 02 – UML (I)
▪ 03 – UML (II)
▪ 04 – Design Styles and - Basic Design Patterns
▪ 05 – Creational Design Patterns (I)
▪ 06 – Creational Design Patterns (II)
▪ 07 – Partitioning patterns
▪ 08 – Structural patterns (I)
▪ 09 – Structural patterns (II)
▪ 10 – Behavioral patterns (I)
▪ 11 – Behavioral patterns (II)
▪ 12 – Concurrency patterns
[email protected] 7
Historical Review
[email protected] 8
Overview of Programming Paradigms
▪ Non-Procedural
▪ Procedural
▪ Structured
▪ Object Oriented
▪ Aspect Oriented
[email protected] 9
Non-Procedural
▪ BASIC
[email protected] 10
Procedural Programming
▪ E.g., C, FORTRAN
[email protected] 11
Structured Programming
▪ E.g., PASCAL
[email protected] 12
Object Oriented Programming
▪ Java, c++, c#
[email protected] 13
Aspect Oriented Programming
▪ E.g., AspectJ
▪ For global concerns such as logging, transaction management,
Global Concerns
Tactical Concerns
[email protected] 14
Introduction to Object Oriented Paradigm
[email protected] 15
Advantages of OOP
[email protected] 16
Basic Concepts of OOP
[email protected] 17
Objects and Classes
▪ What is an Object?
▪ “Concept, abstraction, or thing with crisp boundary & meaning for a
problem”
▪ An Object has state and behavior
▪ Objects receive stimuli/messages & respond
▪ Receiving a stimulus, Object may change state
▪ Examples of Objects
[email protected] 18
Objects and Classes
▪ What is a Class?
▪ Group of Objects with similar
▪ properties (attributes)
▪ behavior
▪ relationships to other objects
▪ semantics
▪ Blueprints of Objects
[email protected] 19
Abstraction
[email protected] 20
Abstraction Example
[email protected] 21
Encapsulation
[email protected] 22
Example
[email protected] 23
Inheritance
[email protected] 24
Inheritance Example
[email protected] 25
Uses of Inheritance
Reuse
▪ If multiple classes have common attributes/methods, these methods can
be moved to a common class parent class.
▪ This allows reuse since the implementation is not repeated.
▪ Example:
▪ Rectangle and Circle method have a common method move(), which
requires changing the centre coordinate.
[email protected] 26
Uses of Inheritance
Specialization
[email protected] 27
Uses of Inheritance
Common Interface and Extnesion
▪ All the operations that are supported for Rectangle and Circle are the
same.
▪ Some methods have common implementation and others don’t.
▪ move() operation is common to classes and can be implemented in parent.
▪ circumference(), area() operations are significantly different and have to be
implemented in the respective classes.
▪ The Shape class provides a common interface where all 3 operations
move(), circumference() and area().
▪ Extend functionality of a class.
▪ Child class adds new operations to the parent class but does not change
the inherited behavior.
▪ E.g. Rectangle class might have a special operation
▪ that may not be meaningful to the Circle class rotate90degrees()
[email protected] 28
Uses of Inheritance
Multiple Inheritance
▪ Inherit properties from more than one class.
▪ This is called Multiple Inheritance
▪ Not in Java!
▪ Why?
[email protected] 29
Polymorphism
Object Overloading
▪ Polymorphic which means “many forms” has Greek roots.
▪ Poly → many
▪ Morphos → forms.
▪ In OO paradigm polymorphism has many forms.
▪ Allow a single object, method, operator associated with different
meaning depending on the type of data passed to it.
▪ An object of type Circle or Rectangle can be assigned to a Shape object.
The behavior of the object will depend on the object passed.
[email protected] 30
Polymorphism
Method Overloading
▪ Multiple methods can be defined with the same name, different input
arguments.
▪ Method 1 → initialize(int a)
▪ Method 2 → initialize(int a, int b)
▪ Appropriate method will be called based on the input arguments.
▪ initialize(2) → Method 1 will be called.
▪ initialize(2,4) → Method 2 will be called.
[email protected] 31
Polymorphism
Operator Overloading
▪ Allows regular operators such as +, --, *, / to have different meanings
based on the type.
▪ E.g. + operator for Circle can re defined
▪ Circle c = c + 2;
▪ Stack s = s + ‘a’
▪ Not supported in JAVA. C++ supports it.
▪ It is confusing
▪ Think of overloading the = sign …
[email protected] 32
Static vs. Dynamic Binding
[email protected] 33
Overloading vs. Overriding
[email protected] 34
Basic differences between C++ and JAVA
[email protected] 35
Language Examples: C++
[email protected] 36
Language Examples: C++
▪ Constructors
▪ Functions to initialize the data members of
▪ May also allocate storage if part of the object is heap-dynamic
▪ Can include parameters to provide parameterization of the objects
▪ Implicitly called when an instance is created
▪ Can be explicitly called
▪ Name is the same as the class name
▪ Destructors
▪ Functions to cleanup after an instance is destroyed; usually just to reclaim
heap storage
▪ Implicitly called when the object’s lifetime ends
▪ Can be explicitly called
▪ Name is the class name, preceded by a tilde (~)
[email protected] 37
An Example in C++
class stack {
private:
int*stackPtr, maxLen, topPtr;
public:
stack() { // a constructor
stackPtr= new int[100];
maxLen= 99;
topPtr= -1;
};
~stack () {delete [] stackPtr;};
void push (intnum) {…};
void pop () {…};
int top () {…};
int empty () {…};
}
[email protected] 38
Language Examples: Java
[email protected] 39
Differences between Java and C++
▪ Global variables
▪ It is impossible to create a global variable that is outside of all classes. The
only way to create a global variable is by declaring public static variable in a
class.
▪ GOTO
▪ Java has no goto statement.
▪ Pointers
▪ Java has no way to manipulate pointers (mem addresses) directly. You
cannot convert an integer to a pointer, you cannot dereference an arbitrary
memory address.
▪ Memory Allocation
▪ Java has no malloc or free() functions.
▪ Data types
▪ Hardware dependent data types such as int and char* lead to non portable
code. Java uses a standard size irrespective of the H/W.
▪ No header files.
▪ No preprocessor. [email protected] 40
Access Protection Modifiers in Java
http://www.artima.com/objectsandjava/webuscript/PackagesAccess1.html