Chapter 11 Introduction to Classes Intro to Computer Science CS1510, Section 2 Dr. Sarah Diesburg.

Slides:



Advertisements
Similar presentations
Object Oriented Programming
Advertisements

Object-Oriented Programming Python. OO Paradigm - Review Three Characteristics of OO Languages –Inheritance It isn’t necessary to build every class from.
Chapter 1 Object-Oriented Concepts. A class consists of variables called fields together with functions called methods that act on those fields.
Classes and Object- Oriented... tMyn1 Classes and Object-Oriented Programming The essence of object-oriented programming is that you write programs in.
OBJECT-ORIENTED PROGRAMMING. What is an “object”? Abstract entity that contains data and actions Attributes (characteristics) and methods (functions)
Object-Oriented PHP (1)
Road Map Introduction to object oriented programming. Classes
 2006 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
Inheritance and Class Hierarchies Chapter 3. Chapter 3: Inheritance and Class Hierarchies2 Chapter Objectives To understand inheritance and how it facilitates.
1 Programming for Engineers in Python Autumn Lecture 5: Object Oriented Programming.
OOP in Java Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
CS 106 Introduction to Computer Science I 03 / 21 / 2008 Instructor: Michael Eckmann.
WEL COME PRAVEEN M JIGAJINNI PGT (Computer Science) MCA, MSc[IT], MTech[IT],MPhil (Comp.Sci), PGDCA, ADCA, Dc. Sc. & Engg.
OOP in Java Fawzi Emad Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
Guide to Programming with Python
Object Oriented Software Development
UFCEUS-20-2 : Web Programming Lecture 5 : Object Oriented PHP (1)
CSM-Java Programming-I Spring,2005 Introduction to Objects and Classes Lesson - 1.
Centre for Computer Technology ICT115 Object Oriented Design and Programming Week 2 Intro to Classes Richard Salomon and Umesh Patel Centre for Information.
Lecture 8 Inheritance Richard Gesick. 2 OBJECTIVES How inheritance promotes software reusability. The concepts of base classes and derived classes. To.
CIT 590 Intro to Programming Style Classes. Remember to finish up findAllCISCourses.py.
An Object-Oriented Approach to Programming Logic and Design
Recap (önemli noktaları yinelemek) from last week Paradigm Kay’s Description Intro to Objects Messages / Interconnections Information Hiding Classes Inheritance.
Chapter 11 Introduction to Classes. "The Practice of Computing Using Python", Punch & Enbody, Copyright © 2013 Pearson Education, Inc. Code Listing 11.1.
11 Chapter 11 Object-Oriented Databases Database Systems: Design, Implementation, and Management 4th Edition Peter Rob & Carlos Coronel.
CSCI-383 Object-Oriented Programming & Design Lecture 13.
Chapter 12 More on Classes. "The Practice of Computing Using Python", Punch & Enbody, Copyright © 2013 Pearson Education, Inc. The three OOP factors Remember,
An Object-Oriented Approach to Programming Logic and Design Fourth Edition Chapter 6 Using Methods.
More About Objects and Methods Chapter 5. Outline Programming with Methods Static Methods and Static Variables Designing Methods Overloading Constructors.
Topic 1 Object Oriented Programming. 1-2 Objectives To review the concepts and terminology of object-oriented programming To discuss some features of.
Chapter 6 Introduction to Defining Classes. Objectives: Design and implement a simple class from user requirements. Organize a program in terms of a view.
Overview The Basics – Python classes and objects Procedural vs OO Programming Entity modelling Operations / methods Program flow OOP Concepts and user-defined.
Programming with Java © 2002 The McGraw-Hill Companies, Inc. All rights reserved. 1 McGraw-Hill/Irwin Chapter 5 Creating Classes.
CS451 - Lecture 2 1 CS451 Lecture 2: Introduction to Object Orientation Yugi Lee STB #555 (816) * Acknowledgement:
Chapter 4&5 Defining Classes Copyright © 2010 Pearson Addison-Wesley. All rights reserved.
Object-Oriented Programming Chapter Chapter
COPYRIGHT 2010: Dr. David Scanlan, CSUS OBJECTIVES: How to write classes How to create an object from a class using the "New" keyword. How to communicate.
Object Oriented Programing (OOP)
Guide to Programming with Python Chapter Eight (Part I) Object Oriented Programming; Classes, constructors, attributes, and methods.
More on Functions Intro to Computer Science CS1510 Dr. Sarah Diesburg.
Session 6 Comments on Lab 3 & Implications of Inheritance.
Chapter 5 Classes and Methods II Lecture Slides to Accompany An Introduction to Computer Science Using Java (2nd Edition) by S.N. Kamin, D. Mickunas, E.
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Programming Logic & Design Second Edition by Tony Gaddis.
Chapter More on Classes Intro to Computer Science CS1510, Section 2 Dr. Sarah Diesburg.
Inheritance and Class Hierarchies Chapter 3. Chapter 3: Inheritance and Class Hierarchies2 Chapter Objectives To understand inheritance and how it facilitates.
Inheritance and Class Hierarchies Chapter 3. Chapter Objectives  To understand inheritance and how it facilitates code reuse  To understand how Java.
Chapter 5Java: an Introduction to Computer Science & Programming - Walter Savitch 1 Chapter 5 l Programming with Methods l Polymorphism l Constructors.
Topic 1 Object Oriented Programming. 1-2 Objectives To review the concepts and terminology of object-oriented programming To discuss some features of.
Chapter 7 Classes and Methods III: Static Methods and Variables Lecture Slides to Accompany An Introduction to Computer Science Using Java (2nd Edition)
Introduction to Classes Intro to Computer Science CS1510, Section 2 Dr. Sarah Diesburg.
Structure A Data structure is a collection of variable which can be same or different types. You can refer to a structure as a single variable, and to.
More on Functions Intro to Computer Science CS1510 Dr. Sarah Diesburg.
Object Oriented Programming Some Interesting Genes.
Inheritance Chapter 7 Inheritance Basics Programming with Inheritance
Copyright (c) 2017 by Dr. E. Horvath
Chapter 3: Using Methods, Classes, and Objects
Inheritance Basics Programming with Inheritance
IFS410: Advanced Analysis and Design
Lecture 22 Inheritance Richard Gesick.
Teach A-Level Computer Science: Object-Oriented Programming in Python
Week 3 Object-based Programming: Classes and Objects
Computer Programming with JAVA
An Introduction to Object Orientated Programming
COP 3330 Object-oriented Programming in C++
Object-Oriented Programming
Review of Previous Lesson
Object-Oriented PHP (1)
CPS120: Introduction to Computer Science
A Level Computer Science Topic 6: Introducing OOP
Intro to Computer Science CS1510 Dr. Sarah Diesburg
Presentation transcript:

Chapter 11 Introduction to Classes Intro to Computer Science CS1510, Section 2 Dr. Sarah Diesburg

Lab 12 Let’s review a solution

What is a Class? The short answer is that object oriented programming is a way to think about “objects” in a program (such as variables, functions, etc). A program becomes less a list of instructions and more a set of objects and how they interact.

OOP Principles Encapsulation: hiding design details to make the program clearer and more easily modified later. Modularity: the ability to make objects “stand alone” so they can be reused (our modules). I.e. the math module. Inheritance: create a new object by inheriting (like father to son) many object characteristics while creating or over-riding for this object. Polymorphism: (hard) Allow one message to be sent to any object and have it respond appropriately based on the type of object it is.

Class versus Instance A class is used as a definition to create new instances of those classes (which are objects)  Describes what the object is going to look like

Why a Class? We make classes because we need more complicated, user-defined data types to construct instances we can use. Each class has potentially two aspects:  the data (types, number, names) that each instance might contain  the messages that each instance can respond to

A First Class

class Student(object): """Simple Student class.""" def __init__(self,first='', last='', id=0): # init instance self.firstNameStr = first self.lastNameStr = last self.idInt = id def __str__(self): # string representation for printing return "{} {}, ID:{}".format\ (self.firstNameStr, self.lastNameStr, self.idInt)

Constructor When a class is defined, a function is made with the same name as the class This function is called the constructor. By calling it, you can create an instance of the class. We can affect this creation (more later), but by default Python can make an instance.

A Class is a New Type >>>myLst = [1, 2, 3] type(myLst) => type >>>myStr = ‘abc’ type(myStr) => type >>>s1 = Student(“Sarah",“Diesburg",12345) type(s1)

Instance Knows its Class Because each instance has as its type the class that it was made from, an instance “remembers” its class. This is often called the “instance-of” relationship.

“Dot” Reference We can refer to the attributes of an object by doing a “dot” reference, of the form: object.attribute The attribute can be a variable or a function. It is part of the object, either directly or by that object being part of a class.

Accessor Methods A method that returns information about the STATE of the object. (The value of one or more instance variables).

Two ways to access data print myInst.myVal  print a variable associated with the object myInst myInst.myMethod()  call a method associated with the object myInst Variable versus method, you can tell by the parenthesis at the end of the reference

class Student(object): """Simple Student class.""" def __init__(self,first='', last='', id=0): # init instance self.firstNameStr = first self.lastNameStr = last self.idInt = id def __str__(self): # string rep, e.g. for printing return "%s %s, ID:%s" % \ (self.firstNameStr, self.lastNameStr, self.idInt) def getID(self): return self.id

Why I prefer accessor methods over direct access print s1.idInt print s1.getID() Both return the same exact value right now, but I think that the accessor method is a better way to do this. Any idea why?

Mutator Methods Methods that have the potential to change the state of an object.

class Student(object): def __init__(self,first='', last='', id=0): self.firstNameStr = first self.lastNameStr = last self.idInt = id def update(self,first='',last='',id=0): if first: self.firsNameStr = first if last: self.lastNameStr = last if id: self.idInt = id def __str__(self): # string rep, e.g. for printing return "%s %s, ID:%s" % \ (self.firstNameStr, self.lastNameStr, self.idInt)

Method versus Function Discussed before, a method and a function are closely related. They are both “small programs” that have parameters, perform some operation and (potentially) return a value. The main difference is that methods are functions tied to a particular object.

Difference in Calling Functions are called, methods are called in the context of an object: function: dosomething(param1) method: anObject.doSomething(param1) This means that the object that the method is called on is always implicitly a parameter!

More on Self self is an important variable. In any method it is bound to the object that called the method. Through self, we can access the internal structure of the instance.

self is Bound for Us When a dot method call is made, the object that called the method is automatically assigned to self. We can use self to remember, and therefore refer, to the calling object. To reference any part of the calling object, we must always precede it with self. The method can be written generically, dealing with all calling objects through self.

Writing a class

Constructor There are some special methods that have certain pre-defined roles for all classes. One of the first we will learn is a constructor. Constructor is called when an instance is made and provides the class designer the opportunity to set up the instance with variables, by assignment.

Calling a Constructor As mentioned, a constructor is called by using the name of the class as a function call (by adding () after the class name): myInst = myClass() Creates a new instance using myClass.

Special Python Keywords Again, Python has special uses for keywords that begin and end with __. so far we have seen the __doc__ attributed of a function with a doc string. In classes, we will see more of these special values.

The __init__ Method One of the special method names in a class is the constructor name, __init__. By assigning values in the constructor, every instance will start out with the same variables. You can also pass arguments to a constructor through its init method.

def __init__(self,first='', last='', id=0): self.firstNameStr = first self.lastNameStr = last self.idInt = id self is bound to the default instance as it is being made. If we want to add an attribute to that instance, we modify the attribute associated with self.

Default Constructor If you don’t provide a constructor then only the default constructor is provided. The default constructor does “system stuff” to create the instance, nothing more. You cannot pass arguments to the default constructor.

Every Class Should Have __init__ By providing the constructor, we ensure that every instance, at least at the point of construction, is created with the same contents. This gives us some control over each instance.

Printing def __str__(self): return "%s %s, ID:%s" % \ (self.firstNameStr, self.lastNameStr, self.idInt) What happens when we call print myInst? This is assumed, by Python, to be a call to “convert the instance to a string”, which is the __str__ method. In the method, myInst is bound to self, and printing then occurs using that instance.

Now There are Three There are now three groups in our coding scheme:  user  programmer, class user  programmer, class designer

Class Designer The class designer is creating code to be used by other programmers. In so doing, the class designer is making a kind of library that other programmers can take advantage of.

Remember this:OOP Principles Encapsulation: hiding design details to make the program clearer and more easily modified later. Modularity: the ability to make objects “stand alone” so they can be reused (our modules). I.e. the math module. Inheritance: create a new object by inheriting (like father to son) many object characteristics while creating or over-riding for this object. Polymorphism: (hard) Allow one message to be sent to any object and have it respond appropriately based on the type of object it is.

We are Still at Encapsulation Features of encapsulation: - hid details of the implementation so that the program was easier to read and write - modularity: make an object so that it can be reused in other contexts - providing an interface (the methods) that are the approved way to deal with the class

Private Values

Private Variables in an Instance Many OOP approaches allow you to make a variable or function in an instance private. Private means not accessible by the class user, only the class developer. There are advantages to controlling who can access the instance values.

‘Privacy’ in Python Python takes the approach “We are all adults here.” No hard restrictions. Provides naming to avoid accidents. Use __ in front of any variable This ‘mangles’ the name to include the class, namely __var becomes _class__var. Still fully accessible, and the __dict__ makes it obvious.

Privacy Example class myClass(object): def __init__(self,p1='firstParam',p2='secondParam'): self.var1=p1 self.__var2=p2 myInst = myClass() myInst.__dict__.items() [('_myClass__var2', 'secondParam'), ('var1', 'firstParam')]