CS313D: ADVANCED PROGRAMMING LANGUAGE Computer Science department Lecture 6 : Abstraction
Lecture Contents 2 Abstract classes Abstract methods Case study: Polymorphic processing Sealed methods & classes Exception handling Interfaces
3 Abstraction Chapter 12 : 12.4, 12.5, 12.6
Abstract Classes and Methods 4 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.
Abstract Properties 5 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.
6 Tips!!
Case Study: Payroll System 7 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.
Design 8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
sealed Methods and Classes 24 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.
sealed Methods and Classes 25 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.
26 Text Book Chapter 12 : 12.4, 12.5, 12.6
27 Exception Handling
Introduction 28 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.
Example 29 an exception is thrown (i.e., an exception occurs) when a method detects a problem.
30
Example revisited 31 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 32
33
34 Exception Handling mechanism 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.
try-catch 35 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.
36 method call and exception handling process 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 dr. Amal and Khalifa, no Spr handler 17 is found, CLR catches the exception and usually displays an error message
.NET Exception Hierarchy 37 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.
38 Be careful!!
39 Text Book Chapter 13 : 13.1 13.3
40 Interfaces
Interfaces 41 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
Case study 42
43
44
45
46
47
48
49 Text Book Chapter 12 : 12.4, 12.5, 12.6, 12.7
50 Case Study
51