Search for notes by fellow students, in your own course and all over the country.

Browse our notes for titles which look like what you need, you can preview any of the notes via a sample of the contents. After you're happy these are the notes you're after simply pop them into your shopping cart.

My Basket

You have nothing in your shopping cart yet.

Title: Detailed object orientated programming notes
Description: These are my Object Orientated Programming Notes! It covers every aspect of OOP in detail and is easy to understand. If you are a computer science student or just an entrepreneur, these are my ultimate notes that have helped me understand OOP with EXAMPLES.

Document Preview

Extracts from the notes are below, to see the PDF you'll receive please use the links above


Introducing object-oriented
What is an Object and a Class?
Everyone knows what an object is—a tangible thing that we can sense, feel, and
manipulate
...
Wooden blocks,
plastic shapes, and over-sized puzzle pieces are common first objects
...

In Python, every piece of data you see or come into contact with is represented by an object
...
Integers, strings, lists, and so forth are all nothing more than objects
...
A class allows you to define and encapsulate a group of objects into one
convenient space
...
A class, therefore, is a “cookie cutter” that can be
used to make as many object instances of that type object as needed
...
How do we differentiate
between types of objects? Apples and oranges are both objects, but it is a common adage
that they cannot be compared
...
To
facilitate the example, we can assume that apples go in barrels and oranges go in baskets
...
In object-oriented
programming, the term used for kind of object is class
...

What's the difference between an object and a class? Classes describe objects
...
You might have three oranges sitting on the
table in front of you
...

The relationship between the four classes of objects in our inventory system can be
described using a Unified Modeling Language (invariably referred to as UML, because three
letter acronyms never go out of style) class diagram
...
Association is the most basic way for two classes
to be related
...
Objects are instances of
classes that can be associated with each other
...
That's simple enough, but what are these data
and behaviors that are associated with each object?

Data describes objects

Let's start with data
...
A class can define specific sets of characteristics that are shared by all objects of that
class
...
For
example, our three oranges on the table (if we haven't eaten any) could each weigh a
different amount
...
All instances of the
orange class have a weight attribute, but each orange has a different value for this attribute
...

As a more realistic example, two objects representing different customers might have the
same value for a first name attribute
...
Some authors suggest that
the terms have different meanings, usually that attributes are settable, while properties are
read-only
...
In addition, as we'll discuss in Chapter 5,
When to Use Object-oriented Programming, the property keyword has a special meaning in
Python for a particular kind of attribute
...
Attribute types are often primitives that are standard to most programming
languages, such as integer, floating-point number, string, byte, or Boolean
...
This is one area where the design stage can overlap with the programming stage
...

Usually, we don't need to be overly concerned with data types at the design stage, as
implementation-specific details are chosen during the programming stage
...
If our design calls for a list container type, the Java
programmers can choose to use a LinkedList or an ArrayList when implementing it, while the
Python programmers (that's us!) can choose between the list built-in and a tuple
...
However, there are
some implicit attributes that we can make explicit—the associations
...


Behaviors are actions

Now, we know what data is, but what are behaviors? Behaviors are actions that can occur
on an object
...
At the programming level, methods are like functions in structured programming,
but they magically have access to all the data associated with this object
...

Parameters to a method are a list of objects that need to be passed into the method that is
being called (the objects that are passed in from the calling object are usually referred to as
arguments)
...
Returned values are the results of that task
...

The idea of objects is an important one in the world of computers
...

To really understand how objects work in Python, we need to think about types of objects
...


A giraffe is a type of mammal, which is a type of animal
...

Now consider a sidewalk
...
Let’s call it an inanimate object (in other words, it’s not alive)
...


Breaking Things into Classes
In Python, objects are defined by classes, which we can think of as a way to classify objects
into groups
...
Below the Things class, we have Inanimate and Animate
...


Now let’s create the same set of classes as shown in our tree diagram, starting from the top
...
Since Things is the broadest
class, we’ll create it first:
>>> class Things:
pass

We name the class Things and use the pass statement to let Python know that we’re not
going to give any more information
...

Next, we’ll add the other classes and build some relationships between them
...
Classes can be both children of and parents to other classes
...
For example,
Inanimate and Animate are both children of the class Things, meaning that Things is their

parent
...
Next, we create a class called Animate and tell Python that
its parent class is also Things, using class Animate(Things)
...
We create the Sidewalks class with the
parent class Inanimate like so:
>>> class Sidewalks(Inanimate):
pass

And we can organize the Animals, Mammals, and Giraffes classes using their parent classes
as well:
>>> class Animals(Animate):

pass
>>> class Mammals(Animals):
pass
>>> class Giraffes(Mammals):
pass

Adding Objects to Classes
We now have a bunch of classes, but what about putting some things into those classes?
Say we have a giraffe named Reginald
...
To
“introduce” Reginald to Python, we use this little snippet of code:
>>> reginald = Giraffes()

Defining Functions of Classes
When we define a function that is associated with a class, we do so in the same way that we
define any other function, except that we indent it beneath the class definition
...
See?')

Adding Class Characteristics As Functions
Consider the child classes of the Animate class which we

defined earlier We can add

characteristics to each class to describe what it is and what it can do
...

For example, what do all animals have in common? Well, to start with, they all breathe
...
What about mammals? Mammals all feed their young with milk
...
We know that giraffes eat leaves from high up in trees, and like all
mammals, they feed their young with milk, breathe, move, and eat food
...


!
To add a function to a class, we use the def keyword
...
The self parameter is a way for one function in the class to call another function in

the class (and in the parent class)
...

On the next line, the pass keyword tells Python we’re not going to provide any more
information about the breathe function because it’s going to do nothing for now
...
We’ll re-create our classes
shortly and put some proper code in the functions
...
Often, programmers will create classes with functions that do nothing as a way to
figure out what the class should do, before getting into the details of the individual functions
...
Each class will be
able to use the characteristics (the functions) of its parent
...
(This is a good way to make your classes simpler and easier to
understand
...
We call functions on an object by using the dot
operator and the name of the function
...
move()
>>> reginald
...
Let’s create another Giraffes object
called harold:
>>> harold = Giraffes()

Because we’re using objects and classes, we can tell Python exactly which giraffe we’re
talking about when we want to run the move function
...
move()
>>> class Animals(Animate):
def breathe(self):

print('breathing')
def move(self):
print('moving')
def eat_food(self):
print('eating food')
>>> class Mammals(Animals):
def feed_young_with_milk(self):
print('feeding young')
>>> class Giraffes(Mammals):
def eat_leaves_from_trees(self):
print('eating leaves')

Now when we create our reginald and harold objects and call functions on them, we can see
something actually happen:
>>> reginald = Giraffes()
>>> harold = Giraffes()
>>> reginald
...
eat_leaves_from_trees()
eating leaves

On the first two lines, we create the variables reginald and harold, which are objects of the
Giraffes class
...
In the same way, we call the eat_leaves_from_trees function on harold, and
Python prints eating leaves
...


Class Work
#1: The Giraffe Shuffle
Add functions to the Giraffes class to move the giraffe’s left and right feet forward and
backward
...
The result of calling this new function will be a simple
dance:
>>> reginald = Giraffes()
>>> reginald
...
It prepares the new object for use, often
accepting arguments that the constructor uses to set required member variables
...
Python relies on the constructor to perform tasks
such as initializing (assigning values to) any instance variables that the object will need
when it starts
...

The name of a constructor is always the same, __init__()
...
When you create a class without a
constructor, Python automatically creates a default constructor for you that doesn’t do
anything
...


Private, protected and public in Python
Python doesn’t have any mechanisms, that would effectively restrict you from accessing a
variable or calling a member method
...


In C++ and Java, things are pretty straight-forward
...
But there’s
no such a thing in Python
...
How to accomplish this in Python? The answer is – by convention
...
See the example below:
class Cup:
def __init__(self):
self
...
_content = None # protected variable
def fill(self, beverage):
self
...
_content = None
cup = Cup()
cup
...
So when you want to
make your member public, you just do nothing
...
color = None
self
...
content = beverage
def empty(self):
self
...
color = "red"
redCup
...
empty()
redCup
...
e
...
Python supports a technique
called name mangling
...

So how to make your member private? Let’s have a look at the example below:
class Cup:
def __init__(self, color):
self
...
__content = None # private variable
def fill(self, beverage):
self
...
__content = None
redCup = Cup("red")
redCup
...
Its width is
set to 10
...

Area: The area() method will return 20
...

class Box:
def area(self):
return self
...
height
def __init__(self, width, height):
self
...
height = height
# Create an instance of Box
...

print(x
...
Greeting = Name + "!"
def SayHello(self):
print("Hello {0}"
...
Greeting))
Object-oriented programming languages, such as Python, provide three fundamental
features that support object-oriented programming— encapsulation , inheritance , and
polymorphism
...


What Is Encapsulation?
Encapsulation is a means of bundling together instance variables and methods to form a
given type (class)
...
Information hiding is a form of abstraction
...


As an example, we give a depiction of a Fraction object

The Fraction object shows private instance variables __numerator and __denominator,
and four public methods
...
For example, trying to access the instance variables of
Fraction object frac1 is invalid,
frac1
...
__denominator 5 6 NOT ALLOWED
Public members of a class, on the other hand, are directly accessible
...
getNumerator() ALLOWED
frac1
...
setNumerator(4) ALLOWED
frac1
...
Restricting access to instance
variables via getter and setter methods allows the methods to control what values are
assigned (such as not allowing an assignment of 0 to the denominator), and how they are
represented when retrieved
...

----------------------------------------

Encapsulation is a means of bundling together instance variables and methods to form a
given type, as well as a way of restricting access to certain class members
...
This means that if we want to change these
attributes or methods we can do so in the parent class and the changes will be immediately
available in all child classes
...

Inheritance
...
The class we want to derive
from must be defined
...


Inheritance , in object-oriented programming, is the ability of a class to inherit members of
another class as part of its own defi nition
...

Superclasses may themselves inherit from other classes, resulting in a hierarchy of classes
as shown below:

Class Hierarchy

Example
Class A is the superclass of all the classes in the figure
...
In addition, Class B defines variable
var2 and method method2, and Class E defines method5
...
And since Class D is also a subclass of Class B, it inherits everything in Class A
and Class B, also defining method4
...

-------------------------

Example
Class B derives from class A
...

Size: This def method is found directly on class B
...

Width: This is found by checking the base class of class B, which is class A
...

class A:
def width(self):
print("a, width called")
class B(A):
def size(self):
print("b, size called")
# Create new class instance
...

b
...

b
...
” In
object oriented programming, polymorphism allows objects of different types, each with their
own specific behaviors, to be treated as the same general type
...
We
call this  polymorphism  or  overriding  and it is useful because we do not want to keep
introducing new method names for functionality that is pretty similar in each class

For example, consider the Shape class and its subclasses as shown below:

Polymorphic Shape Class

class MyClass:
def public(self):
pass
def __private(self):
# mangled to _MyClass__private
pass

c = MyClass()
c
...
__private() # this doesn’t

I HOPE YOU benefitted
Title: Detailed object orientated programming notes
Description: These are my Object Orientated Programming Notes! It covers every aspect of OOP in detail and is easy to understand. If you are a computer science student or just an entrepreneur, these are my ultimate notes that have helped me understand OOP with EXAMPLES.