Oo Python
Oo Python
Design of Circle
• All objects are said to be an instance of some class. The class of an object
determines which attributes the object will have.
• A class is a description of what its instances will know and do.
Circle: classes and objects
Circle Class
Attributes:
- location, radius, color
Methods:
- draw, move
• The values of the instance variables define the state of the individual object
• If the attributes are private, they serve to enforce the concept of information
hiding
Messages
• Process by which system components interact:
– send data to another object
– request data from another object
– request object to perform some behavior
• Implemented as methods (not called functions).
– Functions are processes that are object independent
– Methods are dependent on the state of the object
Message Passing
• When calling a method in another class, OO uses the term “message passing”
you are passing messages from one class to another
• Don’t be confused… this is really just a new name for calling a method or a
function
What is ‘self’
• Self is a reference to the currentinstance. Self lets you access all the instance
variables for the specific instance you’re working with.
– myCircle.myFunction(actualP1, actualP2) Do this
• is like calling:
– Circle.myFunction(myCircle,actualP1, actualP2) Not this
• “self” really gets the value of “myCircle”.. But it happens automatically!
Why use classes at all?
• Classes and objects are more like the real world. They minimize the semantic
gap by modeling the real world more closely
• The semantic gap is the difference between the real world and the
representation in a computer.
• Classes and objects allow you to define an interface to some object (it’s
operations) and then use them without know the internals.
• Defining classes helps modularize your program into multiple objects that work
together, that each have a defined purpose
Encapsulation
• Attributes and behaviors are enclosed (encapsulated) within the logical
boundary of the object entity
– In structured or procedural systems, data and code are typically maintained as
separate entities (e.g., modules and data files)
– In Object Technology systems, each object contains the data (attributes) and
the code (behaviors) that operates upon those attributes
Abstraction
• Encapsulation implements the concept of abstraction:
– details associated with object sub-components are enclosed within the logical
boundary of the object
– user of object only “sees” the public interface of the object, all the internal
details are hidden
Note - In Python, encapsulation is merely a programming convention. Other
languages (e.g., Java) enforce the concept more rigorously.
Implementing Public/Private Interfaces
• Python attributes and methods are public by default.
– public attributes: any other class or function can see and change the attribute
myCircle.radius = 20
– public method: any other class or function can call
the method myCircle.method1()
• Make things private by adding __ (two underscores) to the beginning of the
name:
– self.__radius = 20 # Private attribute
– def __method1(): # Private method
Implementing Public/Private Interfaces
• Private attributes can (almost) only be accessed by methods defined in the class
• Private methods can (almost) only be called by other methods defined in the
class
• Idea: Everything defined in the class has access to private parts.
Getters and Setters (or)
Accessors and Mutators
Printing objects
>>> aStudent = Student("Karl","Johnson", 18)
>>> print aStudent
<__main__.Student object at 0x8bd70>
Doesn’t look so good! Define a special function in the class “__str__” that is used
to convert your object to a string whenever
needed
def __str__(self):
return "Name is:"+ self.__name
Name is:KarlJohnson
Inheritance
We can declare one class to be inherited by another class.
It provides instant polymorphism.
The child class immediately gets all the data and behavior of the parent
class.
The child can then add more than the parent class had.
This is called making the child a specialization of the parent.
A 3-D rectangle might know/do all that a rectangle does, plus some
more:
class rectangle3D(rectangle):
Inheritance
• Inheritance is talked about a lot in the object-oriented world.
• It does reduce even further duplication of code.
• If you have two classes that will have many the same methods, then set up
inheritance.
• But in actual practice, inheritance doesn’t get used all that much, and can be
confusing.
class name(superclass):
statements
• Example:
class Point3D(Point): # Point3D extends Point
z = 0
...
(if > 1 superclass has the same field/method, conflicts are resolved in left-to-right
order)
• methods: class.method(object, parameters)
• constructors: class.__init__(parameters)
class Point3D(Point):
z = 0
def __init__(self, x, y, z):
Point.__init__(self, x, y)
self.z = z