cs313d: advanced programming language · chapter 13 : 13.1 13.3 exception handling dr. amal...
TRANSCRIPT
CS313D: ADVANCED
PROGRAMMING LANGUAGE
Lecture 6 : Abstraction Computer Science
department
Lecture Contents
dr. Amal Khalifa, Spr 17
Abstract classes
Abstract methods
Case study: Polymorphic processing
Sealed methods & classes
Interfaces
Exception handling
2
Chapter 12 : 12.4, 12.5, 12.6
Abstraction 3
dr. Amal Khalifa, Spr 17
Abstract Classes and Methods
Abstract classes, or abstract base classes cannot
be used to instantiate objects.
too general, specify only what is common among
derived classes.
contains one or more abstract methods
keyword abstract in their declaration
do not provide implementations.
Constructors and static methods cannot be declared abstract.
Classes that can be used to instantiate objects are
called concrete classes.
provide implementations to the abstract methods. dr. Amal Khalifa, Spr 17
4
Abstract Properties
public abstract PropertyType MyProperty
{ get; set;
} // end abstract property
An abstract property omits implementations for the get
accessor and/or the set accessor.
Concrete derived classes must provide implementations for
every accessor declared in the abstract property.
dr. Amal Khalifa, Spr 17
5
Tips !!
dr. Amal Khalifa, Spr 17
6
Case Study: Payroll System
A company pays its employees on a weekly basis. The employees
are of four types: Salaried employees are paid a fixed weekly
salary regardless of the number of hours worked, hourly
employees are paid by the hour and receive "time-and-a-half"
overtime pay for all hours worked in excess of 40 hours,
commission employees are paid a percentage of their sales, and
salaried-commission employees receive a base salary plus a
percentage of their sales. For the current pay period, the
company has decided to reward salaried-commission employees
by adding 10% to their base salaries. The company wants to
implement an app that performs its payroll calculations
polymorphically.
dr. Amal Khalifa, Spr 17
7
Design
dr. Amal Khalifa, Spr 17
8
dr. Amal Khalifa, Spr 17 9
dr. Amal Khalifa, Spr 17 10
dr. Amal Khalifa, Spr 17 11
dr. Amal Khalifa, Spr 17 12
dr. Amal Khalifa, Spr 17 13
dr. Amal Khalifa, Spr 17 14
dr. Amal Khalifa, Spr 17 15
dr. Amal Khalifa, Spr 17 16
dr. Amal Khalifa, Spr 17 17
dr. Amal Khalifa, Spr 17 18
dr. Amal Khalifa, Spr 17 19
dr. Amal Khalifa, Spr 17 20
dr. Amal Khalifa, Spr 17 21
dr. Amal Khalifa, Spr 17 22
dr. Amal Khalifa, Spr 17 23
sealed Methods and Classes
dr. Amal Khalifa, Spr 17
A method declared sealed in a base class cannot be
overridden in a derived class.
Methods that are declared private are implicitly sealed.
Methods that are declared static also are implicitly sealed,
because static methods cannot be overridden either.
A derived-class method declared both override and
sealed can override a base-class method, but cannot be
overridden in classes further down the inheritance hierarchy.
Calls to sealed methods (and non-virtual methods) are
resolved at compile time—this is known as static binding.
24
sealed Methods and Classes
dr. Amal Khalifa, Spr 17
A class that is declared sealed cannot be a base
class (i.e., a class cannot extend a sealed class).
All methods in a sealed class are implicitly sealed.
Class string is a sealed class. This class cannot
be extended, so apps that use strings can rely
on the functionality of string objects as specified
in the Framework Class Library.
25
Interfaces 26
dr. Amal Khalifa, Spr 17
Interfaces
An interface declaration begins with the keyword
interface and can contain only abstract methods and
abstract properties
All interface members are implicitly declared both public
and abstract.
An interface can extend one or more other interfaces to create
a more elaborate interface that other classes can implement.
specify that it implements the interface
must declare each member of the interface with the signature
specified in the interface declaration.
If any interface member is not implemented declare as
abstract dr. Amal Khalifa, Spr 17
27
Case study
dr. Amal Khalifa, Spr 17
28
dr. Amal Khalifa, Spr 17 29
dr. Amal Khalifa, Spr 17 30
dr. Amal Khalifa, Spr 17 31
dr. Amal Khalifa, Spr 17 32
dr. Amal Khalifa, Spr 17 33
dr. Amal Khalifa, Spr 17 34
Chapter 12 : 12.4, 12.5, 12.6, 12.7
Text Book
dr. Amal Khalifa, Spr 17
35
Chapter 13 : 13.1 13.3
Exception Handling
dr. Amal Khalifa, Spr 17
36
Introduction
An exception is an indication of a problem that
occurred during a program’s execution.
Exception handling enables you to create apps that
can handle exceptions—in many cases allowing a
program to continue executing as if no problems were
encountered.
Exception handling enables you to write clear, robust
and more fault-tolerant programs.
dr. Amal Khalifa, Spr 17
37
Example
dr. Amal Khalifa, Spr 17
an exception
is thrown (i.e.,
an exception
occurs) when a
method
detects a
problem.
38
dr. Amal Khalifa, Spr 17 39
Example revisited
dr. Amal Khalifa, Spr 17
40
catch and
handle
exceptions —
displaying an
error message
and allowing
the user to
enter another
set of values.
You may also
use
Int32.TryParse
Example revisited
dr. Amal Khalifa, Spr 17
41
dr. Amal Khalifa, Spr 17 42
•At least one catch block must immediately follow a try block.
•The statements in the finally clause are guaranteed to execute
regardless of whether an exception occurs.
Exception Handling mechanism 43
dr. Amal Khalifa, Spr 17
try-catch
dr. Amal Khalifa, Spr 17
44
The point at which an exception occurs is called the throw
point.
If an exception occurs in a try block, program control
immediately transfers to the first catch block matching
the type of the thrown exception.
After the exception is handled, program control resumes
after the last catch block.
An uncaught exception (or unhandled exception) is an
exception for which there is no matching catch block.
When an exception is thrown, CLR begins searching an exception handler in the call-stack starting from the method that has thrown the exception. This is repeated for each of the methods down the call-stack until a handler is found which catches the exception. If Main(…) is reached and no handler is found, CLR catches the exception and usually displays an error message
method call and exception handling process 45
dr. Amal Khalifa, Spr 17
.NET Exception Hierarchy
dr. Amal Khalifa, Spr 17
46
In C#, only objects of class Exception and its
derived classes may be thrown and caught.
If a program attempts to access an out-of-range array
index CLR throws IndexOutOfRangeException.
Attempting to use a null reference causes a
NullReferenceException.
A catch block that specifies a parameter of type
Exception can catch all exceptions.
makes sense only if the handling behavior is the same for a
base class and all derived classes.
Be careful !! 47
dr. Amal Khalifa, Spr 17
Case Study 48
dr. Amal Khalifa, Spr 17
dr. Amal Khalifa, Spr 17 49