Object Oriented Programming
No description yet. This curated deck is ready for daily study.
Card preview (24)
An object is an instance and has:
• a type • an internal data representation • each instance is a particular type of object
Objects
• A data abstraction that capture • Can create new instances of objects • Can destroy objects
Data abstraction captures
• internal representation - interface for interacting with objects through methods (procedures)
Creating a class
• defining the class name - defining the class attributes
Using the class
• creating new instances of objects - doing operations on the instance
Advantage of OOP
• Bundle data into packages together with procedures (functions) • Divide-and-conquer development • Classes make it easy to reuse code
Divide-and-conquer development
• implement and test behavior of each class separately • increase modularity reduces complexity • inheritance allows sub-classes to redefine or extend a subset of a super-class behavior
Method is
A function that works only with THIS class
More about methods
Python always passes the actual object as the first argument to use as ‘self’
The “.” operator is used to:
Access any attribute: • a data attribute of an object • a method of an object
Class is
A blueprint created by a programmer for an object
Object is
An instance of a class
Power of OOP
• bundle objects together • use abstractions (how to implements vs. how to use) • build layers of object abstractions • create own classes of objects
Implementing class vs using class
write code from two different perspectives
Implementing an object
• define the class • define the data attributes • define methods
Using an object
• create instances of object type - do operations
Class def. of an object
• class is the type • class is defined generically (use self) • common across all instances
Instance of a class
• one particular object • data values vary between instances • structure of the class
Why use OOP and classes of objects?
• mimic real life - group different objects as part of the same type
Groups of objects have attributes
• data attributes - procedural attributes
Data attributes
• how can you represent your object with data - what it is
Procedural attributes
Behavior/operations/methods • what kind of things can you do with the object? • what it does
Instance of DOT notation
• instantiation creates an instance of an object • dot notation used to access attributes • better to use getters and setters!
Information hiding
• may change data attribute var. names • getting an error if: accessing data attributes outside class and class definition changes