Presentation is loading. Please wait.

Presentation is loading. Please wait.

C# Programming: From Problem Analysis to Program Design1 Advanced Object-Oriented Programming Features C# Programming: From Problem Analysis to Program.

Similar presentations


Presentation on theme: "C# Programming: From Problem Analysis to Program Design1 Advanced Object-Oriented Programming Features C# Programming: From Problem Analysis to Program."— Presentation transcript:

1 C# Programming: From Problem Analysis to Program Design1 Advanced Object-Oriented Programming Features C# Programming: From Problem Analysis to Program Design 3rd Edition 11

2 Part II C# Programming: From Problem Analysis to Program Design2

3 3 Abstract Classes Used to prohibit other classes from instantiating objects of the base class Still inherit characteristics from base class in subclasses Base class can have data and method members [access modifier] abstract class ClassIdentifier { } // Base class

4 C# Programming: From Problem Analysis to Program Design4 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

5 C# Programming: From Problem Analysis to Program Design5 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

6 C# Programming: From Problem Analysis to Program Design6 Partial Classes Break class up into two or more files –Each file uses partial class designation New features of C# 2.0 Used by Visual Studio for Windows applications –Code to initialize controls and set properties is placed in a somewhat hidden file in a region labeled “Windows Form Designer generated code” –File is created following a naming convention of “FormName.Designer.cs” or “xxx.Designer.cs” –Second file stores programmer code

7 C# Programming: From Problem Analysis to Program Design7 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

8 C# Programming: From Problem Analysis to Program Design8 Interfaces ( continued ) General form [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

9 C# Programming: From Problem Analysis to Program Design9 Defining an Interface Can be defined as members of a namespace or class or by compiling to a DLL Easy approach is to put the interface in a separate project –Use the Class Library template Unlike abstract classes, it is not necessary to use the abstract keyword with methods –Because all methods are abstract

10 C# Programming: From Problem Analysis to Program Design10 Defining an Interface ( continued ) Figure 11-15 ITraveler interface Build the interface DLL using Build option from Build menu bar

11 C# Programming: From Problem Analysis to Program Design11 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] Base class comes first

12 C# Programming: From Problem Analysis to Program Design12 Implement the Interface: PresentationGUI Application Figure 11-16 PresentationGUI output using interface methods

13 C# Programming: From Problem Analysis to Program Design13.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

14 C# Programming: From Problem Analysis to Program Design14.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

15 C# Programming: From Problem Analysis to Program Design15 NET Framework Interfaces ( continued ) HashTable is not abstract –Implements a number of interfaces public class Hashtable : IDictionary, ICollection, IEnumerable, ISerializable, IDeserializationCallback, ICloneable Implements the IDeserializationCallback interface Explore the documentation for these classes and interfaces

16 C# Programming: From Problem Analysis to Program Design16 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

17 C# Programming: From Problem Analysis to Program Design17 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

18 C# Programming: From Problem Analysis to Program Design18 Generics Reduce the need to rewrite algorithms for each data type Create generic classes, delegates, interfaces, and methods Identify where data will change in the code segment by putting a placeholder in the code for the type parameters

19 C# Programming: From Problem Analysis to Program Design19 Generic Classes Defined by inserting an identifier between left and right brackets on the class definition line Example public class GenericClass { public T dataMember; } – //To instantiate an object, replace the T with data type GenericClass anIdentifer = new GenericClass ( );

20 C# Programming: From Problem Analysis to Program Design20 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 (ref firstValue, ref secondValue);

21 Dynamic C# was originally characterized as being a strongly typed language –Compiler checks to ensure that only compatible values are attempting to be stored –Variables can still be defined as objects and then cast as different data types during run time Additional boxing/unboxing is needed C# Programming: From Problem Analysis to Program Design21

22 Dynamic Data Type Object defined using the dynamic keyword can store anything –No unboxing or casting is necessary prior to their use –With dynamic data types, the type checking occurs at run time –Once defined as dynamic, the memory location can hold any value C# Programming: From Problem Analysis to Program Design22

23 var Data Type Variables declared inside a method can be declared using the var keyword –Implicitly typed by the compiler - compiler determines the type One primary difference between dynamic and var is that var data items must be initialized when they are declared –Can declare a dynamic memory location and later associate values with it C# Programming: From Problem Analysis to Program Design23

24 C# Programming: From Problem Analysis to Program Design24 StudentGov Application Example Figure 11-18 Problem specification for StudentGov example

25 C# Programming: From Problem Analysis to Program Design25 StudentGov Application Example ( continued )

26 C# Programming: From Problem Analysis to Program Design26 StudentGov Application Example ( continued ) Figure 11-19 Prototype for StudentGov example

27 C# Programming: From Problem Analysis to Program Design27 StudentGov Application Example ( continued ) Figure 11-20 Class diagrams for StudentGov example

28 C# Programming: From Problem Analysis to Program Design28 StudentGov Application Example ( continued ) Figure 11-21 References added to StudentGov example

29 C# Programming: From Problem Analysis to Program Design29 Properties: StudentGov Application

30 C# Programming: From Problem Analysis to Program Design30 Properties: StudentGov Application ( continued )

31 C# Programming: From Problem Analysis to Program Design31 StudentGov Application Example ( continued ) Figure 11-22 Setting the StartUp Project

32 C# Programming: From Problem Analysis to Program Design32 StudentGov Application Example ( continued ) Figure 11-23 Part of the PresentationGUI assembly

33 C# Programming: From Problem Analysis to Program Design33 StudentGov Application Example ( continued ) Figure 11-24 Output from StudentGov example

34 Coding Standards Declare members of a class of the same security level together When declaring methods that have too many arguments to fit on the same line, the leading parenthesis and the first argument should be written on the same line –Additional arguments are written on the following line and indented C# Programming: From Problem Analysis to Program Design34

35 C# Programming: From Problem Analysis to Program Design35 Chapter Summary Major features of object-oriented languages –Abstraction –Encapsulation –Inheritance –Polymorphism Multitier applications using component-based development methods

36 C# Programming: From Problem Analysis to Program Design36 Chapter Summary ( continued ) Use inheritance to extend the functionality of user- defined classes Abstract classes –Abstract methods Partial classes Interfaces

37 C# Programming: From Problem Analysis to Program Design37 Chapter Summary ( continued ) Why polymorphic programming? Generics –Generic classes –Generic methods


Download ppt "C# Programming: From Problem Analysis to Program Design1 Advanced Object-Oriented Programming Features C# Programming: From Problem Analysis to Program."

Similar presentations


Ads by Google