Centre for Computer Technology ICT115 Object Oriented Design and Programming Week 2 Intro to Classes Richard Salomon and Umesh Patel Centre for Information.

Slides:



Advertisements
Similar presentations
Chapter 4&5 Defining Classes Copyright © 2010 Pearson Addison-Wesley. All rights reserved.
Advertisements

CS0007: Introduction to Computer Programming Introduction to Classes and Objects.
Chapter 3 Introduction to Classes, Objects, Methods, and Strings
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 11 Classes and Object- Oriented Programming.
Road Map Introduction to object oriented programming. Classes
Object Oriented Programming.  OOP Basic Principles  C++ Classes  September 2004  John Edgar 22.
Evan Korth New York University Computer Science I Classes and Objects Professor: Evan Korth New York University.
16/22/2015 2:54 PM6/22/2015 2:54 PM6/22/2015 2:54 PMObject-Oriented Development Concept originated with simulating objects and their interactions. Adapted.
Terms and Rules Professor Evan Korth New York University (All rights reserved)
Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved Chapter 9 Objects and Classes.
1 Fall 2007ACS-1903 Chapter 6: Classes Classes and Objects Instance Fields and Methods Constructors Overloading of Methods and Constructors Scope of Instance.
ASP.NET Programming with C# and SQL Server First Edition
C++ fundamentals.
Object Oriented Software Development
Programming Languages and Paradigms Object-Oriented Programming.
CSM-Java Programming-I Spring,2005 Introduction to Objects and Classes Lesson - 1.
 2003 Prentice Hall, Inc. All rights reserved. 1 Introduction to Classes and Objects Outline Introduction Classes, Objects, Member Functions and Data.
© 2008 Dr. Paul Walcott – The University of the West Indies: Cave Hill CampusDr. Paul Walcott COMP6325 Advanced Web Technologies Dr. Paul Walcott The University.
Writing Classes (Chapter 4)
Chapter 5 - Writing a Problem Domain Class Definition1 Chapter 5 Writing a Problem Domain Class Definition.
OBJECT ORIENTED PROGRAMMING CONCEPTS ISC 560. Object-oriented Concepts  Objects – things names with nouns  Classes – classifications (groups) of similar.
Introduction to Object Oriented Programming. Object Oriented Programming Technique used to develop programs revolving around the real world entities In.
Chapter 11 Introduction to Classes Intro to Computer Science CS1510, Section 2 Dr. Sarah Diesburg.
Programming in Java Unit 2. Class and variable declaration A class is best thought of as a template from which objects are created. You can create many.
An Introduction to Java Chapter 11 Object-Oriented Application Development: Part I.
Copyright © 2012 Pearson Education, Inc. Chapter 13: Introduction to Classes.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 13: Introduction to Classes.
Copyright 2003 Scott/Jones Publishing Standard Version of Starting Out with C++, 4th Edition Chapter 13 Introduction to Classes.
Centre for Computer Technology ICT214 Object Oriented Design and Programming Week 02 – Classes, Objects, Instances Richard Salomon and Umesh Patel Centre.
Java™ How to Program, 10/e © Copyright by Pearson Education, Inc. All Rights Reserved.
An Object-Oriented Approach to Programming Logic and Design Chapter 3 Using Methods and Parameters.
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.
Chapter 4 Introduction to Classes, Objects, Methods and strings
Overview The Basics – Python classes and objects Procedural vs OO Programming Entity modelling Operations / methods Program flow OOP Concepts and user-defined.
CS0007: Introduction to Computer Programming Classes: Documentation, Method Overloading, Scope, Packages, and “Finding the Classes”
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 26 - Java Object-Based Programming Outline 26.1Introduction.
Chapter 3 (B) 3.5 – 3.7.  Variables declared in a function definition’s body are known as local variables and can be used only from the line of their.
Chapter 4&5 Defining Classes Copyright © 2010 Pearson Addison-Wesley. All rights reserved.
Object Oriented Programing (OOP)
© 2007 Lawrenceville Press Slide 1 Chapter 8 Objects  A variable of a data type that is a class. Also called an instance of a class.  Stores data  Can.
Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved X 1 Chapter 9 Objects and Classes.
Introduction to Object-Oriented Programming Lesson 2.
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.
Centre for Computer Technology ICT115 Object Oriented Design and Programming Week 03 – Class Design, Object Discovery and Use Richard Salomon Centre for.
Copyright © 2015 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 3 A First Look at Classes and Objects.
More about Java Classes Writing your own Java Classes More about constructors and creating objects.
Chapter 7 Classes and Methods III: Static Methods and Variables Lecture Slides to Accompany An Introduction to Computer Science Using Java (2nd Edition)
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 13: Introduction to Classes.
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Programming Logic & Design Second Edition by Tony Gaddis.
CSCI/CMPE 4341 Topic: Programming in Python Chapter 7: Introduction to Object- Oriented Programming in Python – Exercises Xiang Lian The University of.
CSC 108H: Introduction to Computer Programming Summer 2011 Marek Janicki.
Programming Logic and Design Seventh Edition
Java Primer 1: Types, Classes and Operators
Chapter 3: Using Methods, Classes, and Objects
Introduction to Classes
Road Map Introduction to object oriented programming. Classes
Chapter 4: Writing Classes
Classes In C#.
Chapter 3 Introduction to Classes, Objects Methods and Strings
Introduction to Classes
Chapter 3 Introduction to Classes, Objects Methods and Strings
Teach A-Level Computer Science: Object-Oriented Programming in Python
Outline Anatomy of a Class Encapsulation Anatomy of a Method
Object Oriented Programming in java
2.1 Introduction to Object-Oriented Programming
Lecture 8 Object Oriented Programming (OOP)
Presentation transcript:

Centre for Computer Technology ICT115 Object Oriented Design and Programming Week 2 Intro to Classes Richard Salomon and Umesh Patel Centre for Information and Communication Technology Box Hill Institute of TAFE

September 7, 2015September 7, 2015September 7, 2015Slide2 Copyright Box Hill Institute Contents at a Glance Introduction to classes Introduction to classes Methods Methods Introduction to Objects Introduction to Objects UML UML Program Example Program Example

September 7, 2015September 7, 2015September 7, 2015Slide3 Copyright Box Hill Institute Class Class - A blueprint for an object. Contains attributes of each object that it defines Class - A blueprint for an object. Contains attributes of each object that it defines Attributes Attributes Data (aka fields) of a class - describe the state of an object Data (aka fields) of a class - describe the state of an object behaviours or methods of a class - describe what an object can do behaviours or methods of a class - describe what an object can do

September 7, 2015September 7, 2015September 7, 2015Slide4 Copyright Box Hill Institute Data type – describes the type of data held in a field Data type – describes the type of data held in a field Instance - an object that is used by an application Instance - an object that is used by an application Method - set of instruction describing the operations performed by an object - essentially a function that belongs to a class Method - set of instruction describing the operations performed by an object - essentially a function that belongs to a class

September 7, 2015September 7, 2015September 7, 2015Slide5 Copyright Box Hill Institute Naming of Classes Must begin with a letter of an alphabet Must begin with a letter of an alphabet Can contain only letters, digits, or underscores Can contain only letters, digits, or underscores Can not be a keyword or literal Can not be a keyword or literal Case sensitive Case sensitive Start with an upper case letter and then use lower case letters, with the first character of a new word capitalised e.g. Person, MotorVehicle Start with an upper case letter and then use lower case letters, with the first character of a new word capitalised e.g. Person, MotorVehicle

September 7, 2015September 7, 2015September 7, 2015Slide6 Copyright Box Hill Institute Example 2.1. Creating a Class #!/usr/bin/python # Filename: simplestclass.py class Person: pass # An empty block p = Person() # Create an instance Person print p

September 7, 2015September 7, 2015September 7, 2015Slide7 Copyright Box Hill Institute How It Works We create a new class using the class statement followed by the name of the class. An indented block of statements which form the body of the class follows. In this case, we have an empty block which is indicated using the pass statement. We create a new class using the class statement followed by the name of the class. An indented block of statements which form the body of the class follows. In this case, we have an empty block which is indicated using the pass statement. Next, we create an object or an instance of this class using an empty constructor, which is the name of the class followed by a pair of parentheses. Next, we create an object or an instance of this class using an empty constructor, which is the name of the class followed by a pair of parentheses. For our verification, we confirm the type of the variable by simply printing it. It tells us that we have an instance of the Person class in the __main__ module. For our verification, we confirm the type of the variable by simply printing it. It tells us that we have an instance of the Person class in the __main__ module. Notice that the address of the computer memory where your object is stored is also printed. The address will have a different value on your computer since Python can store the object wherever it finds space. Notice that the address of the computer memory where your object is stored is also printed. The address will have a different value on your computer since Python can store the object wherever it finds space.

September 7, 2015September 7, 2015September 7, 2015Slide8 Copyright Box Hill Institute Output >>> >>>

September 7, 2015September 7, 2015September 7, 2015Slide9 Copyright Box Hill Institute Constructor Methods All class definitions include a constructor method to create objects All class definitions include a constructor method to create objects methodName = ClassName() methodName = ClassName() ClassName() is a null constructor ClassName() is a null constructor NULL constructor – without arguments NULL constructor – without arguments

September 7, 2015September 7, 2015September 7, 2015Slide10 Copyright Box Hill Institute Example 2.2. Using Object Methods #!/usr/bin/python # Filename: method.py class Person: def sayHi(self): print 'Hello, how are you?‘ p = Person() # instantiate a person named p p.sayHi() # This short example can also be written as Person().sayHi()

September 7, 2015September 7, 2015September 7, 2015Slide11 Copyright Box Hill Institute Output and How It Works >>> Hello, how are you? >>> Here we see the self in action. Notice that the sayHi method takes no parameters but still has the self in the function definition. Here we see the self in action. Notice that the sayHi method takes no parameters but still has the self in the function definition.

September 7, 2015September 7, 2015September 7, 2015Slide12 Copyright Box Hill Institute The self Class methods have only one specific difference from ordinary functions - they must have an extra first name that has to be added to the beginning of the parameter list, but you do not give a value for this parameter when you call the method. Class methods have only one specific difference from ordinary functions - they must have an extra first name that has to be added to the beginning of the parameter list, but you do not give a value for this parameter when you call the method. Python will provide it. Python will provide it. This particular variable refers to the object itself, and by convention, it is given the name self. This particular variable refers to the object itself, and by convention, it is given the name self.

September 7, 2015September 7, 2015September 7, 2015Slide13 Copyright Box Hill Institute The __init__ method There are many method names which have special significance in Python classes. The __init__ method is run as soon as an object of a class is instantiated. The method is useful to do any initialization you want to do with your object. Notice the double underscore both in the beginning and at the end in the name. The __init__ method is a constructor. The __init__ method is a constructor.

September 7, 2015September 7, 2015September 7, 2015Slide14 Copyright Box Hill Institute Example 2.3. Using the __init__ method #!/usr/bin/python # Filename: class_init.py class Person: def __init__(self, aName): self.name = aName def sayHi(self): print 'Hello, my name is', self.name p = Person('Swaroop') p.sayHi()

September 7, 2015September 7, 2015September 7, 2015Slide15 Copyright Box Hill Institute Output >>> Hello, my name is Swaroop >>>

September 7, 2015September 7, 2015September 7, 2015Slide16 Copyright Box Hill Institute Class and Object Variables Class variables are shared in the sense that they are accessed by all objects (instances) of that class. Class variables are shared in the sense that they are accessed by all objects (instances) of that class. There is only one copy of the class variable and when any one object makes a change to a class variable, the change is reflected in all the other instances as well. There is only one copy of the class variable and when any one object makes a change to a class variable, the change is reflected in all the other instances as well. Object variables are owned by each individual object/instance of the class. In this case, each object has its own copy of the field i.e. they are not shared and are not related in any way to the field by the same name in a different instance of the same class. Object variables are owned by each individual object/instance of the class. In this case, each object has its own copy of the field i.e. they are not shared and are not related in any way to the field by the same name in a different instance of the same class. Remember this simple difference between class and object variables. Remember this simple difference between class and object variables. An example will make this easy to understand. An example will make this easy to understand.

September 7, 2015September 7, 2015September 7, 2015Slide17 Copyright Box Hill Institute Example 2.4. Using Class and Object Variables See the filename: objvar.py found in the Wk02-Examples folder and Observe its operation

September 7, 2015September 7, 2015September 7, 2015Slide18 Copyright Box Hill Institute Example 2.4 details In Example 2.4., we also see the use of docstrings for classes as well as methods. In Example 2.4., we also see the use of docstrings for classes as well as methods. We can access the class docstring at runtime using Person.__doc__ and We can access the class docstring at runtime using Person.__doc__ and the method docstring as Person.sayHi.__doc__ the method docstring as Person.sayHi.__doc__ Just like the __init__ method, there is another special method __del__ which is called when an object is going to die i.e. it is no longer being used Just like the __init__ method, there is another special method __del__ which is called when an object is going to die i.e. it is no longer being used The __del__ method returns the resources used by the object, such as its allocated memory, back to the system for reusing. The __del__ method returns the resources used by the object, such as its allocated memory, back to the system for reusing.

September 7, 2015September 7, 2015September 7, 2015Slide19 Copyright Box Hill Institute The __del__ method The __del__ method is run when the object is no longer in use and there is no guarantee when that method will be run. The __del__ method is run when the object is no longer in use and there is no guarantee when that method will be run. If you want to explicitly kill an object, you just have to call the __del__ method. Calling the __del__ method can create some bad side effects, so it is best to avoid it. If you want to explicitly kill an object, you just have to call the __del__ method. Calling the __del__ method can create some bad side effects, so it is best to avoid it. The __del__ method is analogous to the concept of a destructor. The __del__ method is analogous to the concept of a destructor.

September 7, 2015September 7, 2015September 7, 2015Slide20 Copyright Box Hill Institute Attribute characteristics All class members (including the data members) are public and all the methods are virtual in Python. All class members (including the data members) are public and all the methods are virtual in Python. One exception: If you use data members with names using the double underscore prefix such as __privateVar, Python uses name-mangling to effectively make it a private variable. One exception: If you use data members with names using the double underscore prefix such as __privateVar, Python uses name-mangling to effectively make it a private variable.

September 7, 2015September 7, 2015September 7, 2015Slide21 Copyright Box Hill Institute Attribute characteristics The convention followed is that any variable that is to be used only within the class or object should begin with an underscore, all other names are public and can be used by other classes/objects. Remember that this is only a convention and is not enforced by Python (except for the double underscore prefix). The convention followed is that any variable that is to be used only within the class or object should begin with an underscore, all other names are public and can be used by other classes/objects. Remember that this is only a convention and is not enforced by Python (except for the double underscore prefix).

September 7, 2015September 7, 2015September 7, 2015Slide22 Copyright Box Hill Institute Method Types Accessor or getter a method that gets data from an object Accessor or getter a method that gets data from an object Mutator or setter a method that sets data in an object Mutator or setter a method that sets data in an object Custom Method a method to perform a specific user defined task Custom Method a method to perform a specific user defined task

September 7, 2015September 7, 2015September 7, 2015Slide23 Copyright Box Hill Institute Message – request for a method Message – request for a method Message sending objects send (and receive) messages to interact with each other [ This is really just a method call ] Message sending objects send (and receive) messages to interact with each other [ This is really just a method call ] Re-use once written and tested, objects and classes can be used in other applications Re-use once written and tested, objects and classes can be used in other applications

September 7, 2015September 7, 2015September 7, 2015Slide24 Copyright Box Hill Institute Encapsulation containing or packaging data and methods in a class Encapsulation containing or packaging data and methods in a class Information (data) hiding black box approach i.e. no need to publish details Information (data) hiding black box approach i.e. no need to publish details

September 7, 2015September 7, 2015September 7, 2015Slide25 Copyright Box Hill Institute Overloaded Methods methods with same name but different formats (data types) are called overloaded methods methods with same name but different formats (data types) are called overloaded methods method signature identifies the specific method used method signature identifies the specific method used

September 7, 2015September 7, 2015September 7, 2015Slide26 Copyright Box Hill Institute UML UML – Unified Modelling Language UML – Unified Modelling Language Diagrams Diagrams Use Case Diagramfunctionality Use Case Diagramfunctionality Class diagramstatic structure Class diagramstatic structure Object diagram Object diagram State chart Diagramdynamic behaviour State chart Diagramdynamic behaviour Collaboration diagramsobject interaction Collaboration diagramsobject interaction Activity diagramsdynamic nature Activity diagramsdynamic nature Component Component Deployment Deployment

September 7, 2015September 7, 2015September 7, 2015Slide27 Copyright Box Hill Institute UML Diagrams Rectangle describes classes and objects Rectangle describes classes and objects Lines describe relationships Lines describe relationships Symbols describe accessibility and strength of relationships Symbols describe accessibility and strength of relationships + public - private # protected default (no symbol) Links inheritance association aggregation Links inheritance association aggregation

September 7, 2015September 7, 2015September 7, 2015Slide28 Copyright Box Hill Institute UML Class Diagram Person Class Name Attribute s (fields) methods +name : String -dateOfBirth : String -id : integer +Person (String aName, String aDate, int aID)

September 7, 2015September 7, 2015September 7, 2015Slide29 Copyright Box Hill Institute References Farrell Joyce, 2006, An Object-Oriented Approach to Programming Logic and Design, [Thomson] Sinan Si Alhir, Learning UML, [O’Reilly Press] Paul Harmon & Mark Watson, Understanding UML