CSC 108H: Introduction to Computer Programming Summer 2012 Marek Janicki.

Slides:



Advertisements
Similar presentations
Python Objects and Classes
Advertisements

Java Inheritance. What is inherited A subclass inherits variables and methods from its superclass and all of its ancestors. The subclass can use these.
ITEC200 – Week03 Inheritance and Class Hierarchies.
OOP in Java Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
Inheritance Review/Recap. ClassA extends ClassB ClassA now inherits (can access and use) all public and protected elements of ClassB We can expect the.
OOP in Java Fawzi Emad Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
BIT 115: Introduction To Programming LECTURE 3 Instructor: Craig Duckett
Fall 2002CS 150: Intro. to Computing1 Streams and File I/O (That is, Input/Output) OR How you read data from files and write data to files.
Programming with Java © 2002 The McGraw-Hill Companies, Inc. All rights reserved. 1 McGraw-Hill/Irwin Chapter 5 Creating Classes.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Chapter 9 Inheritance and.
Introduction to Object-Oriented Programming Lesson 2.
ICS3U_FileIO.ppt File Input/Output (I/O)‏ ICS3U_FileIO.ppt File I/O Declare a file object File myFile = new File("billy.txt"); a file object whose name.
CPSC 217 T03 Week VI Part #1: A2 Post-Mortem and Functions Hubert (Sathaporn) Hu.
Quick Review of OOP Constructs Classes:  Data types for structured data and behavior  fields and methods Objects:  Variables whose data type is a class.
Exceptions Lecture 11 COMP 401, Fall /25/2014.
Chapter 9: Continuing Classes By Matt Hirsch. Table Of Contents 1.Static Fields and Methods 2.Inheritance I. Recycle Code with Inheritance II. Overriding.
Introduction to Exceptions in Java CS201, SW Development Methods.
EXCEPTIONS. Catching exceptions Whenever a runtime error occurs, it create an exception object. The program stops running at this point and Python prints.
Java Programming: Guided Learning with Early Objects Chapter 9 Inheritance and Polymorphism.
CSC 108H: Introduction to Computer Programming Summer 2012 Marek Janicki.
CSC 108H: Introduction to Computer Programming Summer 2011 Marek Janicki.
CSC 108H: Introduction to Computer Programming Summer 2012 Marek Janicki.
CSC 108H: Introduction to Computer Programming Summer 2012 Marek Janicki.
CSC 108H: Introduction to Computer Programming Summer 2012 Marek Janicki.
CSC 108H: Introduction to Computer Programming Summer 2011 Marek Janicki.
CSC 108H: Introduction to Computer Programming Summer 2011 Marek Janicki.
CSC 108H: Introduction to Computer Programming Summer 2012 Marek Janicki.
Class Inheritance Part II: Overriding and Polymorphism Corresponds with Chapter 10.
CSC 108H: Introduction to Computer Programming Summer 2011 Marek Janicki.
Discussion 4 eecs 183 Hannah Westra.
Generics, Exceptions and Undo Command
Java Language Basics.
CS170 – Week 1 Lecture 3: Foundation Ismail abumuhfouz.
Object-Oriented Programming: Classes and Objects
Inheritance and Polymorphism
CSC 108H: Introduction to Computer Programming
Lecture 9-2: Interacting with the Superclass (super);
Week 4 Object-Oriented Programming (1): Inheritance
Functions.
CSC 108H: Introduction to Computer Programming
CSC 108H: Introduction to Computer Programming
Agenda Warmup Lesson 2.5 (Ascii, Method Overloading)
Object-Oriented Programming: Classes and Objects
Comp 249 Programming Methodology
Fall 2017 CISC124 9/21/2018 CISC124 First onQ quiz this week – write in lab. More details in last Wednesday’s lecture. Repeated: The quiz availability.
Object-oriented programming
Object Oriented Programming
To Get Started Paper sheet
Building Java Programs
Lesson 2: Building Blocks of Programming
Chapter 9 Inheritance and Polymorphism
Exception Handling Chapter 9.
Java Programming Language
MSIS 670 Object-Oriented Software Engineering
Exception Handling Chapter 9 Edited by JJ.
Advanced Java Programming
Building Java Programs
Java – Inheritance.
Exceptions.
OO Design with Inheritance
Java Inheritance.
Problems Debugging is fine and dandy, but remember we divided problems into compile-time problems and runtime problems? Debugging only copes with the former.
Building Java Programs
Object Oriented Programming Review
Notes from Week 5 CSE 115 Spring 2006 February 13, 15 & 17, 2006.
Java Programming Language
Exceptions 10-May-19.
CMSC 202 Exceptions.
Lists Like tuples, but mutable. Formed with brackets: Assignment: >>> a = [1,2,3] Or with a constructor function: a = list(some_other_container) Subscription.
Classes and Methods 15-Aug-19.
Presentation transcript:

CSC 108H: Introduction to Computer Programming Summer 2012 Marek Janicki

Aug Administration ● Assignment 3 is up. ● Has two deadlines. – Wed. Aug 8, Fri. Aug 10 ● Will talk about it at the end of class. ● Final is Thurs. Aug 16, 7-10 in SF 3201 ● Material will be covered next week. ● Office hours next week will be ● T2-4,F4-6. ● Exercise 4 will be optional. ● No time to release it that's not concurrent with the assignment.

Aug Class Review ● Classes are user-made types. ● An instance of a class is called an object. ● A class has instance variables. ● These can have distinct values for each object of the same class. ● A class also has class methods. ● These work the same way as other type methods.

Aug Class Review ● Object Oriented Programming supports ● Inheritance ● Polymorphism ● Encapsulation

Aug Class Naming Conventions ● Classes are named using CamelCase ● Not pothole_case. ● Objects are named using pothole_case. ● Class methods are named using pothole_case. ● Class variables are named using pothole_case.

Aug Classes variables vs. Instance variables ● Each class can have class variables. ● This is a variable that is associated with the class, rather than any specific object. ● To create them, you use an assignment statement as follows: ● ClassName.variable_name = value ● The variable can be evaluated with ● ClassName.variable_name ● x.variable_name if x is an instance of ClassName.

Aug Class variables vs. Instance variables ● If you change the value of a class variable using ClassName.variable_name, the value changes for the ClassName objects. ● ClassName.variable_name = new_value ● If you change the value of a class variable using x.variable_name, then it becomes an instance variable for that particular object. ● x.variable_name = new_value ● The value of ClassName.variable_name does not change, nor does the value of y.variable_name for any other ClassName instance y.

Aug Class variables ● Class variables are generally used to denote constants. ● Altering them via objects leads to complicated code. ● Essentially this results in a higher level of aliasing problems.

Aug Inheritance ● ClassA can inherit the methods and variables of ClassB by defining ClassB as follows: ● class ClassB(ClassA): ● We call ClassA the superclass and ClassB the subclass. ● Every instance of ClassB is also an instance of ClassA. ● Not every instance of ClassA is an instance of ClassB. ● So the set of instances of ClassA is a superset of the instances of ClassB.

Aug Inheritance ● We saw that if we have the same method name in a subclass as in a superclass, and we call subclass_instance.method(), then we superclass' method is overwritten and we evaluate the subclass' method. ● But sometimes we want to mostly reuse the superclass method code, and only modify it a little. ● This comes up particularly commonly in constructors, where if your subclass is only a small change, you would not like to copy and paste the code from the constructor of the superclass.

Aug Inheritance ● It would be really useful if we could call a superclass method inside of a subclass. ● Two ways of doing this, if x is an instance of SubClass. ● SuperClass.method_name(x,...) ● x goes in place of self. ● No longer works in python 3. ● super(SubClass, x).method_name(...) ● super returns x's superclass object. ● self implicitly passed here.

Aug Inheritance ● Inheritance allows us to define new methods, and overwrite already existing ones. ● But even when we overwrite existing ones, we can still access them using super. ● super(SubClass, x) will return the SuperClass object associated with x. ● Requires x to be an instance of SubClass. ● Recall that if x is an instance of a SubClass, it is also an instance of the SuperClass.

Aug Break1

Aug Exceptions ● Python often generates errors. ● We can make our own functions, modules, types. ● We can also make our own errors, and generate our own errors. ● Errors in Python are objects. ● All error are subclasses of Exception. ● This means we can define our own errors by creating subclasses of Exception.

Aug MyError ● class MyError(Exception): ● pass ● We can create instances of MyError by using MyError(). ● But these don't stop the code in the same way that python errors do. ● We can also create instances of python errors. ● TypeError(), NameError(), etc. ● Creating them in this way also doesn't stop the code.

Aug Causing Code to crash ● Done using the keyword raise ● raise TypeError() will cause the code to crash with a TypeError. ● raise MyError() will cause the code to crash with a MyError. ● Passing the constructor a string will cause it to crash with that error massage.

Aug Why do we want code to crash? ● It can be one way of enforcing sanity checks. ● For example if you know that some list needs 10 elements, you can check the length and crash if the length is wrong. ● Sometimes the program might run a very long time before an early error actually breaks the program. – The longer it runs, the harder the error is to source. ● Mostly crashing is undesirable.

Aug Avoiding Crashes. ● Avoiding crashes in python involves two keywords: ● try: ● block1 ● except: ● block2 ● Block1 is executed until an exception is raised. Then block2 is executed. ● If no exection is raised, block2 is not executed.

Aug Not catching some execeptions ● Often you only want to catch some exceptions. ● It's common to design code to produce a specific kind of exception. – It's a common way to enforce parameter requirements. ● But code may also have unplanned errors. – It is desirable for the code to crash in this case to indicate that something is wrong. ● except SpecificException: ● This only catches instances of SpecificException or its subclasses.

Aug Getting information from Exceptions ● As exceptions are objects, it is often useful to give them instance variables. ● In particular, the things that actually went wrong should be added to the exception. ● For this to be useful, we need to be able to access the exception that was raised. ● except MyError e: ● This creates a local variable e that refers to the instance of MyError that was raised. ● This local variable can then be used in the exception block.

Aug Break2

Aug Assignment 3