Object-Oriented Programming Concepts

Object Oriented Programming
 
Terminology
Each 
object
 created in a program is an 
instance
 of a 
class
.
Each class presents to the outside world a concise and
consistent view of the objects that are instances of this
class, without going into too much unnecessary detail or
giving others access to the inner workings of the objects.
The class definition typically specifies 
instance variables
,
also known as 
data member
s, that the object contains, as
well as the 
methods
, also known as 
member functions
,
that the object can execute.
Object Oriented Programming
Abstraction
Abstraction is the process of showing only the
essential/necessary features of an to the
outside world.  We want to hide (abstract
away) all other irrelevant information.
Abstraction
For example you know that when you push
the power button on your remote your TV
turns on.
It is not required to understand how infra-red
waves are getting generated in the TV remote
control or how the TV manages to "capture" and
use that event.
Abstraction
When you write:
random.shuffle(data)
You know what happens but you don't know
HOW it happens.
 
And you DON'T care.
Encapsulation
Encapsulation means wrapping up data and
actions (member functions, methods)
together into a single unit i.e. class.
Encapsulation
Without OOP:
 
writePaycheck("Ben Schafer",45,21.75)
With OOP
 
myTimecard = Timecard("Ben Schafer",21.75)
 
myTimecard.worked(10)
 
 
myTimecard.writecheck()
Encapsulation
In many languages it also means restricting
DIRECT access to data and only doing it through
certain methods.
I can't just go in and adjust my hourly wage to
$1000 an hour.
I have to try
 
myTimecard.setWage(1000)
[Which probably has some built in restrictions to
say I don't have permission to write that code]
Inheritance
The ability to create a new class from an
existing class.
Inheritance allows a class (subclass) to acquire
the properties and behavior of another class
(super-class).
It helps to reuse, customize and enhance the
existing code. So it helps to write a code
accurately and reduce the development time.
Inheritance
A triangle is a special kind of polygon
A rectangle is a special kind of polygon
A square is a special kind of rectangle
Polymorphism
Polymorphism is derived from 2 Greek words:
poly and morphs.
"poly" means many
"morphs" means forms
So polymorphism means "many forms".
What happens in python when I say
 
result = var1 + var2
Duck Typing
Python treats abstractions and polymorphism implicitly using
a mechanism known as 
duck typing
.
A program can treat objects as having certain functionality and they
will behave correctly provided those objects provide this expected
functionality.
As an interpreted and dynamically typed language, there is no
compile time
 checking of data types in Python, and no
formal requirement for declarations of abstract base classes.
The term 
duck typing
 comes from an adage attributed to
poet James Whitcomb Riley, stating that 
when I see a bird
that walks like a duck and swims like a duck and quacks like a
duck, I call that bird a duck.
What IS a class?
Fundamentally a Class is a definition of what
individual objects will look like.
Class = Cookie Cutter
Objects/Instances = Cookies cut with that cutter
When declaring a class we have to consider 3
things:
State  (data, "instance variables")
Actions (methods, "instance methods")
What instances look like when they are constructed
(initialization)
Class Definitions
A class serves as the primary means for abstraction in object-
oriented programming.
In Python, every piece of data is represented as an instance
of some class.
A class provides a set of behaviors in the form of member
functions (also known as 
methods
), with implementations
that belong to all its instances.
A class also serves as a blueprint for its instances, effectively
determining the way that state information for each instance
is represented in the form of 
attributes
 (also known as 
fields
,
instance variables
, or 
data members
).
Writing a class in Python
 
The 
self
 Identifier
In Python, the 
self
 identifier plays a key role.
In any class, there can possibly be many
different instances, and each must maintain its
own instance variables.
Therefore, each instance stores its own
instance variables to reflect its current state.
Syntactically, 
self
 identifies the instance upon
which a method is invoked.
Slide Note
Embed
Share

Learn about essential Object-Oriented Programming concepts such as classes, instances, encapsulation, and abstraction. See how OOP allows for a clear and concise representation of objects in a program while hiding unnecessary details and managing data access through methods. Dive into the world of OOP with examples and visual explanations.

  • OOP Concepts
  • Classes
  • Encapsulation
  • Abstraction
  • Programming

Uploaded on Oct 08, 2024 | 0 Views


Download Presentation

Please find below an Image/Link to download the presentation.

The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author.If you encounter any issues during the download, it is possible that the publisher has removed the file from their server.

You are allowed to download the files provided on this website for personal or commercial use, subject to the condition that they are used lawfully. All files are the property of their respective owners.

The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author.

E N D

Presentation Transcript


  1. Object Oriented Programming

  2. Terminology Each object created in a program is an instance of a class. Each class presents to the outside world a concise and consistent view of the objects that are instances of this class, without going into too much unnecessary detail or giving others access to the inner workings of the objects. The class definition typically specifies instance variables, also known as data members, that the object contains, as well as the methods, also known as member functions, that the object can execute.

  3. Object Oriented Programming

  4. Abstraction Abstraction is the process of showing only the essential/necessary features of an to the outside world. We want to hide (abstract away) all other irrelevant information.

  5. Abstraction For example you know that when you push the power button on your remote your TV turns on. It is not required to understand how infra-red waves are getting generated in the TV remote control or how the TV manages to "capture" and use that event.

  6. Abstraction When you write: random.shuffle(data) You know what happens but you don't know HOW it happens. And you DON'T care.

  7. Encapsulation Encapsulation means wrapping up data and actions (member functions, methods) together into a single unit i.e. class.

  8. Encapsulation Without OOP: writePaycheck("Ben Schafer",45,21.75) With OOP myTimecard = Timecard("Ben Schafer",21.75) myTimecard.worked(10) myTimecard.writecheck()

  9. Encapsulation In many languages it also means restricting DIRECT access to data and only doing it through certain methods. I can't just go in and adjust my hourly wage to $1000 an hour. I have to try myTimecard.setWage(1000) [Which probably has some built in restrictions to say I don't have permission to write that code]

  10. Inheritance The ability to create a new class from an existing class. Inheritance allows a class (subclass) to acquire the properties and behavior of another class (super-class). It helps to reuse, customize and enhance the existing code. So it helps to write a code accurately and reduce the development time.

  11. Inheritance A triangle is a special kind of polygon A rectangle is a special kind of polygon A square is a special kind of rectangle

  12. Polymorphism Polymorphism is derived from 2 Greek words: poly and morphs. "poly" means many "morphs" means forms So polymorphism means "many forms". What happens in python when I say result = var1 + var2

  13. Duck Typing Python treats abstractions and polymorphism implicitly using a mechanism known as duck typing. A program can treat objects as having certain functionality and they will behave correctly provided those objects provide this expected functionality. As an interpreted and dynamically typed language, there is no compile time checking of data types in Python, and no formal requirement for declarations of abstract base classes. The term duck typing comes from an adage attributed to poet James Whitcomb Riley, stating that when I see a bird that walks like a duck and swims like a duck and quacks like a duck, I call that bird a duck.

  14. What IS a class? Fundamentally a Class is a definition of what individual objects will look like. Class = Cookie Cutter Objects/Instances = Cookies cut with that cutter When declaring a class we have to consider 3 things: State (data, "instance variables") Actions (methods, "instance methods") What instances look like when they are constructed (initialization)

  15. Class Definitions A class serves as the primary means for abstraction in object- oriented programming. In Python, every piece of data is represented as an instance of some class. A class provides a set of behaviors in the form of member functions (also known as methods), with implementations that belong to all its instances. A class also serves as a blueprint for its instances, effectively determining the way that state information for each instance is represented in the form of attributes (also known as fields, instance variables, or data members).

  16. Writing a class in Python

  17. The self Identifier In Python, the self identifier plays a key role. In any class, there can possibly be many different instances, and each must maintain its own instance variables. Therefore, each instance stores its own instance variables to reflect its current state. Syntactically, self identifies the instance upon which a method is invoked.

More Related Content

giItT1WQy@!-/#giItT1WQy@!-/#giItT1WQy@!-/#