Object-Oriented Programming (OOP)mirfan/slides/2020 Spring/PL/OOP... · 2020-04-15 · However,...
Transcript of Object-Oriented Programming (OOP)mirfan/slides/2020 Spring/PL/OOP... · 2020-04-15 · However,...
4/15/20
1
CSCI-2320Object-Oriented Programming
(OOP)
Mohammad T. Irfan
1
Imperative vs. object-oriented paradigms
2
4/15/20
2
Imperative vs. object-oriented
u Imperativeu Procedural decomposition
u Procedures are all powerful
u Data is helpless, at the mercy of procedures
u Object-oriented (OO)u Data-centric: data governs the decomposition
u Classes – templates/patterns, abstracts away datau Objects – actual things/instantiations of classes
u Advantages of OO paradigmu Collaboration
u Debugging
u Reuse
3
Roadmap
u OOP principles (current slides)
u OOP paradigm: Ruby (next topic after this)u Learn the basics of Ruby
u Investigate Ruby’s object-oriented design principles
u Ruby on Rails for web programming
4
4/15/20
3
OOP PrinciplesExamples: Java
5
OOP principles
1. Encapsulation
2. Inheritance
3. Polymorphism
4. Abstraction
These principles typically interact with one another.
6
4/15/20
4
1. Encapsulation
u “to enclose in or as if in a capsule” (Mish, 1988)
u Data abstractionu “List”– hides how it’s implemented; how to append to
a list, etc.
u Hide internal representation of data by providing methods (e.g., getter, setter, and other methods that manipulate the data)u Allow for multiple levels of visibility: from not hiding
at all (public) to hiding (private)
u Benefits: decoupling data and functionality, protection of data, hiding unnecessary details of representation
7
Encapsulation example: Book class
u Want a class for representing certain information about a booku Note: each object is one single book
u Multiple objects à many books
1. What are the attributes or properties of a book?
2. What are the actions or behaviors that you can apply on book data?
u Encapsulation: bind the above two together
8
4/15/20
5
9
10
4/15/20
6
11
A tale of two cities1859
Moby Dick1851
Unknown-1
book1 book2 book3
Shared class variable: count = 3Methods are also shared
12
4/15/20
7
Encapsulation question
u Build on the Book class to include author names. How would you represent multiple authors?
13
2. Inheritance
u Allows one class to "inherit" the methods (functionalities) and attributes (variables) of another class
u Subclass (AKA derived class)
extends (or inherits)
Superclass (AKA base class)
u Java's keyword: extends
14
4/15/20
8
Subclass of subclass variables and methods
Inheritance in picture
u Hierarchical organization
Subclass variables and methods
Superclass variables and methods
16
Inheritance in Java
u✗
u✓
Subclass
Multiple superclasses
Superclass
Multiple subclasses
Chain of inheritance
17
4/15/20
9
19
20
4/15/20
10
Chain of inheritance(Multilevel inheritance)u New class for shipping a box, inherits
WeightedBox
25
Demo
26
4/15/20
11
3. Polymorphism
u The ability of an object to take many formsu Compare with method overloading
u This is achieved via inheritanceu Note the inter-relation
u Superclass can refer to subclass object
u Concept: method overriding
27
Method overriding
u Subclass re-defines a superclass method with the same method signature
u Next few slidesu PlainBox class models a simple 3D box
u WeightedBox class extends PlainBox class
u Adds weight variable
u Overrides the multiply method
u BoxDemo class gives a demo
28
4/15/20
12
29
Superclass' multiply method is hidden from the subclassunless the subclass explicitly calls it using super
30
4/15/20
13
Output
31
4. Abstraction
u Generalizationu “Process abstraction”
u Examplesu C (not OOP): qsort works with different data types
u C++: STL
u Java: Abstract class allows generalization by hidingimplementation details
u Daily example: driving a car (hiding implementation details of pressing on gas pedal)
34
4/15/20
14
Abstract class in Javau Give high-level ideas while hiding
implementation details
u Use: manage complexity
u Next few slidesu Abstract class Shape outlines a geometric shape
u Which shape?
u getArea(): Area depends on shape!
u Subclasses of Shape: defines the getArea() method
u Rectangle
u Triangle
Cannot create any object of abstract class!
35
36
4/15/20
15
37
38
4/15/20
16
Output
39
Encapsulation vs abstraction vs information hidingu Debates on orthogonality of concepts
u Roughly–data abstraction (capsule) vs process abstraction (generalization)
u http://www.tonymarston.co.uk/php-mysql/abstraction.txt
40
4/15/20
17
Snapshot of debate“Encapsulation or equivalently information hiding refers to the practice of including within an object everything it needs, and furthermore doing this in such a way that no other object need ever be aware of this internal structure.”-- [Ian Graham, 1991]
“If encapsulation was "the same thing as information hiding," then one might make the argument that "everything that was encapsulated was also hidden." This is obviously not true. ... It is indeed true that encapsulation mechanisms such as classes allow some information to be hidden. However, these same encapsulation mechanisms also allow some information to be visible. Some even allow varying degrees of visibility, e.g., C++'s public, protected, and private members.” -- Tony Marston
41
Encapsulation: correct definitions?
“Encapsulation is used as a generic term for techniques which realize data abstraction. Encapsulation therefore implies the provision of mechanisms to support both modularity and information hiding. There is therefore a one to one correspondence in this case between the technique of encapsulation and the principle of data abstraction.” -- [Blair et al, 1991]
42
4/15/20
18
Abstraction: correct definition?
"Abstraction is generally defined as 'the process of formulating generalised concepts by extracting common qualities from specific examples.'"
-- [Blair et al, 1991]
43