Understanding OOP: Abstraction, Encapsulation, Inheritance, Polymorphism, Summaries of Object Oriented Programming

An introduction to Object-Oriented Programming (OOP), focusing on key concepts such as abstraction, encapsulation, inheritance, and polymorphism. OOP is a programming paradigm that uses objects and classes to represent data and functionality. Abstraction is the process of exposing only relevant information to the user, while encapsulation ensures that data is hidden and protected. Inheritance allows creating new classes based on existing ones, and polymorphism enables objects of different classes to be treated as objects of a common superclass. The document also discusses design issues and the application of OOP in languages like C++ and Java.

Typology: Summaries

2021/2022

Uploaded on 09/27/2022

jeanette
jeanette 🇬🇧

3.7

(7)

237 documents

1 / 11

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
1
Introduction to
Object Oriented
Programming
CMSC 331
Concept of Abstraction
“An abstraction is a view or representation
of an entity that includes only the attributes
of significance in a particular context.”
~Sebesta, pg. 434
Process abstraction
Data abstraction
Encapsulation
Beyond a certain size, process-abstraction
is no longer sufficient to organize large
programs
Modularization – the organization of code
into logically related processes and data
Encapsulation – organization of … into a
separately or independently compilable
unit
Data Abstraction
Abstract Data Type (ADT) is an
encapsulation containing the data
representation and methods for one
specific data type.
pf3
pf4
pf5
pf8
pf9
pfa

Partial preview of the text

Download Understanding OOP: Abstraction, Encapsulation, Inheritance, Polymorphism and more Summaries Object Oriented Programming in PDF only on Docsity!

Introduction toObject Oriented

Programming

CMSC 331

Concept of Abstraction

  • “An abstraction is a view or representation

of an entity that includes only the attributesof significance in a particular context.”~Sebesta, pg. 434– Process abstraction– Data abstraction

Encapsulation

  • Beyond a certain size, process-abstraction

is no longer sufficient to organize largeprograms

  • Modularization – the organization of code

into logically related processes and data

  • Encapsulation – organization of … into a

separately or independently compilableunit

Data Abstraction

  • Abstract Data Type (ADT) is an

encapsulation containing the datarepresentation and methods for

one

specific data type.

Data Abstraction: Formally

-^

An abstract data type is a data type that satisfiesthe following two conditions:– The representation, or definition, of the type and the

operations on objects of the type are contained in asingle syntactic unit. Also, other program units maybe allowed to create variables of the defined type.

  • The representation of objects of the type is hidden

from the program units that use the type, so the onlydirect operations possible on those objects are thoseprovided in the type’s definition.

Advantages

  • Helps to organize program into logical

units.

  • Reduces code interdependencies by

creating interface ‘boundaries’ –enhancing modification and reuse.

  • Improves reliability by keeping details

‘hidden’.

Example

  • Simple Data Types
    • Double
      • More Complex
        • Stack, Queue, Hashtable
          • Very Complex
            • Tax Return

Design Issues

  • Facilities must be present for

encapsulation of type definition andmethods

  • Basic objects should have minimal

definitions

  • Some entities will be common to most

objects

Inheritance: Benefits

  • Facilitates reuse• Allows structuring of Classes into logical

hierarchies

Polymorphism

  • Polymorphic variables can hold a value of

more than one type– e.g. if B is a derived Class of Class A, and

variable x has type A, x can hold a value oftype B as well

Polymorphism: Benefits

  • Allows for more flexible code• Supports operations on generic objects• Provides for use of virtual classes• Drawback: Does make type checking

more difficult; requires more caution atruntime

Application of OO Language

-^

“An executing program in an object-orientedlanguage can be described as a simulation of acollection of computers that communicate witheach other through messages.” (Sebesta p. 463)

-^

Invoking the method of a class is ‘sending amessage’

-^

Collection of methods define a ‘messageprotocol’

-^

Identify the objects and interactions in yourproblem, and simulate them

Design Issues

  • Exclusivity of Objects• Are Subclasses Subtypes?• Implementation and Interface Inheritance• Type Checking and Polymorphism• Single and Multiple Inheritance• Allocation and Deallocation of Objects• Dynamic and Static Binding

Issues: Exclusivity of Objects

  • In purest form, ‘everything’ is an object
    • Benefit of elegance in simplicity– Drawback in that message interface is slower
      • Compromises:
        • Retain a complete, imperative typing model,

add object model

  • Retain imperative model only for primitive,

scalar types

Issues: Subclass = Subtype?

  • Generally, this reduces to the question of

whether a subclass inherits all entities ofthe parent class– Where entities are overriden, they should be

compatible

Issues: Inheritance

  • Interface Inheritance
    • Only the interface of the superclass is seen by

the subclass

  • Implementation Inheritance
    • The implementation details of the superclass

are available to the subclass

Summary

  • OO = ADT + Inheritance + Dynamic

Binding

  • OO Languages support this with Classes,

methods, objects, and message passing

  • Some languages, like C++, are hybrid• Others, like Java support only OO

A Few Words on Java…

CMSC 331

Java is…

  • Object oriented!• Portable!• The language of the WWW!

OO

  • The Java programming language provides

support for:– Abstract Data Types (classes)– Inheritance (single, with some extensions)– Polymorphism

Portability

  • Java is a hybrid language
    • Source is compiled to

byte code

  • Byte code is interpreted by a Java Virtual

Machine (JVM)

  • JVM has been ported to many platforms• Byte code for a Java program should run

on any JVM

  • Programs are not 100% portable

Memory Management

  • JVM implements garbage collection
    • No explicit deallocation of objects– Unreferenced objects are periodically

recycled automatically

Self-Documenting Code

  • Java supports the writing of self-

documenting code:– A simple markup language for use in source

code, which allows embedded html

  • A documentation compiler, ‘javadoc’, which

creates easy to read html Classdocumentation.

Everything is a Class

  • From basic types, to programs• Language contains certain primitive types
    • int, double– Arrays– …
      • Primitive data types have analogs in the

Class hierarchy– Integer, Double, …

Exception Handling

  • Error conditions generate

exceptions

  • Like everything else in Java, exceptions

are Classes– Predefined– User defined

  • Exceptions can be

thrown

by a method, or

caught

and

handled

Security

  • Execution inside a virtual machine makes

it possible to tightly control access tomachine resources– Disk– Network– …

  • There are facilities for digitally signing

code

Jar

  • Classes can be bundled together in jar

files

  • Jar works just like tar (Unix)• Jar files can be used as libraries

Threading

  • Java provides good support for writing

multi-threaded programs, and managingconcurrency.– Thread Class– Low-level synchronization primitives

Memory? What Memory?

  • No pointers• No pointer arithmetic• No memory hassles• Only

references

to Class instances