Download Advanced Object-Oriented Programming - Fundamentals of System Development - Lecture Slides and more Slides Artificial Intelligence in PDF only on Docsity!
Advanced
Object-Oriented
Programming Features
C# Programming: From Problem Analysis to Program Design 3rd Edition
Part II
Abstract Methods
- Only permitted in abstract classes
- Method has no body
- Implementation details of the method are left up to classes derived from the base abstract class
[access modifier] abstract returnType
MethodIdentifier([parameter list]) ; // No { } included
- Declaration for abstract method ends with
semicolon; NO method body or curly braces
Abstract Methods ( continued)
- Every class that derives from the abstract
class must provide implementation details
- Sign a contract that details how to implement its abstract methods
- Syntax error if you use the keyword static or virtual when defining an abstract method
- No additional special keywords are used
when a new class is defined to inherit from the abstract base class
Interfaces
- All .NET languages only support single inheritance
- Think of an interface as a class that is totally
abstract; all methods are abstract
- Abstract classes can have abstract and regular methods
- Implementing interface agrees to define details for all of the interface’s methods
- Classes can implement any number of interfaces
- Only inherit from one class, abstract or nonabstract
Interfaces (continued)
[modifier] interface InterfaceIdentifier { // members - no access modifiers are used }
- Members can be methods, properties, or
events
- No implementations details are provided for any of its members
Defining an Interface (continued)
Figure 11-15 ITraveler interface
Build the interface DLL using Build option from Build menu bar
Implement the Interface
- Follow the same steps as with the Person and
Student DLLs
- Add a reference to the file ending in .dll
- Type a using statement
- Heading for the class definition specifies base
class and one or more interfaces following the
colon (:)
[modifier] class ClassIdentifier : identifier [, identifier]
.NET Framework Interfaces
- Play an important role in the .NET
Framework
- Collection classes such as Array class and HashTable class implement a number of interfaces
.NET Framework Interfaces ( continued)
- .NET Array class is an abstract class
- Implements several interfaces (ICloneable; IList; ICollection; and IEnumerable)
- Includes methods for manipulating arrays, such as:
- Iterating through the elements
- Searching by adding elements to the array
- Copying, cloning, clearing, and removing elements from the array
- Reversing elements
- Sorting
Polymorphism
- Ability for classes to provide different
implementations of methods called by the same name
- ToString( ) method
- Dynamic binding
- Determines which method to call at run time based on which object calls the method
Polymorphic Programming in
.NET
- Multiple classes can implement the same
interface, each providing different implementation details for its abstract methods
- “Black box” concept
- Abstract classes, classes that derive from
them, are forced to include implementation details for any abstract method
Generic Classes
- Defined by inserting an identifier between
left and right brackets on the class definition line
public class GenericClass { public T dataMember; }
- //To instantiate an object, replace the T with data type Docsity.com
Generic Methods
- Similar to defining a generic class
- Insert identifier between left and right brackets on the method definition line to indicate it is a generic method
- Example
public void SwapData (ref T first, ref T second) { T temp; temp = first; first = second; second = temp; } //To call the method, specify the type following method name SwapData < string > ( ref firstValue, ref secondValue);