Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
Community
Ask the community for help and clear up your study doubts
Discover the best universities in your country according to Docsity users
Free resources
Download our free guides on studying techniques, anxiety management strategies, and thesis advice from Docsity tutors
The advance computer programming may not be a piece of cake for every one, but these slides really help you to understand the concept of the programming.Object-Oriented Programming, Major Features, Object-Oriented Languages, Component-Based Development Methods, User-Defined Classes, Abstract Methods, Implement Interfaces, Partial Classes, Differences, Programming
Typology: Study notes
1 / 13
♦ Chapter Overview
♦ Chapter Objectives
♦ Instructor Notes
♦ Quick Quizzes
♦ Discussion Questions
♦ Projects to Assign
♦ Key Terms
Chapter Overview
In this chapter, advanced features of object-oriented design are explored. Students learn about component-based development and are introduced to new ways to write classes and make use of the more than 2,000 classes that make up the Framework Class Library (FCL). Inheritance, interfaces, abstract classes, and polymorphic programming using .NET-supported languages are introduced. Advanced features such as overriding, overloading, and the use of virtual methods are included in this chapter.
Chapter Objectives
In this chapter, students will:
Instructor Notes
For a language to be considered a true object-oriented programming (OOP) language, it must support:
Object-oriented development focuses on designing classes that can be reused in many applications. One way to ensure this reuse is through designing and building components that can be stored in a library and called on when needed.
Quick Quiz
Today, applications are large systems involving development efforts from teams of programmers. These applications often require the packaging together of many different components to respond to business functions. Instead of writing a program that includes all the features in a single file, development today is often geared toward writing multitier applications.
Object-oriented development techniques work well for constructing multitier applications. Think of each of the components in the diagram as independent classes, separate from each other. Classes can be designed and new subclasses created that extend the functionality of the original class. Many different applications can reuse these classes through extending or combining them into new applications.
Components are implemented through classes in C#. You can create class library files with a dynamic link library (DLL) extension. These files can become the components that are referenced from any number of applications.
Quick Quiz
Inheritance is associated with an “is a” relationship. It enables you to create a general class and then define specialized classes that have access to the members of the general class.
Classes can also have a “has a” relationship in which a single class is defined to have instances of other class types. This is a concept called containment or aggregation.
Inheriting from the Object Class Every object created in C# automatically inherits from a base class named object. Object is in the System namespace and has four methods that every class inherits. The methods are Equals( ), GetHashCode( ), GetType( ), and ToString( ).
Inheriting from Other .NET FCL Classes Students have already experienced inheritance when they developed Windows-based programs. The Windows form classes inherit from the System.Windows.Forms.Form class. Extending the Form class enables you to build on the functionality of that class. To inherit from the System.Windows.Forms.Form class, new entries were added to your class definition. This is done by writing “public class derivedClass : baseClass”.
Creating Base Classes for Inheritance You can define your own classes from which other classes can inherit characteristics. This new class becomes the base class. This base class is sometimes called the super or parent class.
Access Modifiers Access to members that have been defined with the private access modifier is restricted to members of the current class. The members are not accessible to classes that derive from this class or that instantiate objects of this class. Using a private access modifier enables the class to protect its data. Access to the data has to occur through its methods or properties. This ensures the data-hiding characteristic of encapsulation.
Constructors Use Public Access Constructors, named the same name as the class name, are defined with public access. It is important to note that if you do not use a public access modifier with constructors, no objects can be instantiated from the class.
Properties Offer Public Access to Data Fields The private member fields can be accessed through these properties. By including properties, there is less need to write accessor (getter) and mutator (setter) methods. When you define a set property, use “value” to reference the value sent into the class to change the value. However, notice you do not declare value. It can be used, almost like magic, to refer to the value that is sent in through an assignment statement.
Overriding Methods When you override a method, you replace the method defined at a higher level. Use the keyword override to override a method that has been defined to include the virtual keyword in the method heading. Overriding a method differs from overloading a method. An overridden method must have exactly the same signature as the base method. Overloaded methods must have a different signature than others with the same name.
Virtual Methods Methods can be overridden when they include the keyword virtual. ToString( ) includes the keyword virtual in its heading. ToString( ) does not have to be overridden. As part of the object class, it returns a string representing the current object. It is often overridden to offer differing functionality based on which object is calling it. This is an example of polymorphism, meaning many forms.
Creating Derived Classes Classes that inherit from a base class are called derived classes. They are also referred to as subclasses or child classes, because they inherit the characteristics of a parent class.
Protected Access Modifiers To have methods in derived classes have access to change data in the base class, define the data members using a protected access instead of a private access. This way, the data is still hidden from other classes but is available for use in derived classes.
Calling the Base Constructor To call the constructor for the base class, an extra entry (:base(argument list)) is added between the constructor heading for the subclass and the opening curly brace. To send data to the base constructor, you must have a matching signature. The order of the arguments being sent to the base constructor is extremely important. They must match the order of the base constructor.
Using Members of the Base Class After objects are instantiated from the derived class, any of the public methods or properties from both the base class and the derived class can be used with a derived object.
Calling Overridden Methods of the Base Class When you have a method that has the same name in both the base and the derived class, the keyword base can be used in the derived class to refer to methods in the base class that are overridden.
Making Stand-Alone Components Classes can be compiled and stored as a dynamic link library (DLL) file. Or, classes can be compiled to create an assembly. Assemblies are the units that are configured and deployed in .NET. The byte code of an assembly can be reused in other applications, and doing so represents the component-based development approach.
Dynamic Link Library (DLL) C# and Visual Studio .NET offer several options for creating components. One option is to compile the source code files into a DLL file instead of into the EXE file. After you have a DLL, any application that will use that component simply adds a reference to the DLL, and that referenced file with the .dll extension becomes part of the application’s private assembly.
Using Visual Studio to Create DLL Files When you first start a new solution or a new project, one of the options is to create a class library using the Class Library template.
Building Instead of Running the Project After you finish typing the class, you do not run the application. To compile and create the DLL, use one of the Build options under the Build menu bar. .NET supports having an application include code from multiple .NET languages. The only requirement for applications with multiple languages is that projects include source code files from only one language.
Adding a Reference to the Base Class In order to use a previously created DLL, add a reference to the DLL. By doing this first, you gain access to the members of the class inside the current class. You can use the Solution Explorer window to do this. Once you select the Project name, click the right mouse button and select Add Reference. Select the Browse button and locate the DLL.
Adding a Using Statement Just adding the reference is not enough. You have to either qualify the class by adding the namespace and a dot before the class name or add a using directive indicating the namespace identifier for the class.
Creating a Client Application to Use the DLL All that is necessary is to add a reference to the components in your program and include a using statement with the appropriate namespace.
Adding a Reference to the DLL A reference must be added for all components that you plan to use in the application.
Adding a Using Statement for the Namespace To avoid typing the fully qualified name, add a using directive.
Declaring an Object of the Component Type Once the DLL component is referenced, you can declare objects of the referenced component.
Instantiating the Object To create or instantiate an actual object of the class, one of the constructors must be used.
Using Members of the Derived and Base Classes Once the DLL component is referenced and objects are instantiated, you can use any of the members of the public members.
Using ILDASM to View the Assembly .NET includes a number of developer tools. Intermediate Language Disassembler (ILDASM) is useful for working with files ending in .dll. The DLL files cannot be modified or viewed as source code, but using ILDASM, you can view the assembly. The assembly shows the signatures of all methods, data fields, and properties. One of the ILDASM options is to display the source code as a comment in the assembly.
Global Assembly Cache The Global Assembly Cache is a machinewide code cache that stores assemblies designated to be shared by several applications.
Quick Quiz
Add the abstract modifier to classes to prohibit other classes from instantiating objects of a base class. You can still inherit characteristics from this base class in subclasses, which enables you to ensure a certain amount of identical functionality from subclasses. This base class can have data and method members.
Abstract Methods An abstract class may contain one or more abstract methods. Abstract methods are only permitted in abstract classes. An abstract method is one that does not include the implementation details for the method. The method has no body. The implementation details of the method are left up to the classes that are derived from the base abstract class. The syntax for creating an abstract method is: [access modifier] abstract returnType MethodIdentifier([parameter list]); // No{ }
Once a class is defined as an abstract class, any and every class that derives from the class must provide the implementation details for every one of its abstract methods.
Quick Quiz
A sealed class cannot be a base class. Sealed classes are defined to prevent derivation. In order to define a sealed class, add the keyword.
Sealed Methods You can prevent subclasses from providing new implementation details for members defined as virtual in a base class. Adding the keyword sealed keeps derived classes from being able to override the method.
When you create a Windows application using Visual Studio, three files are created. Two of the three have class headings that identify the files as partial classes. These two source code files are combined when the application is compiled and run. Partial classes are new to C# 2.0. This is done so that most of the auto designer generated code is separated from user code. The intent is to keep the developer from changing the auto generated code.
As a developer, you can also create multiple partial classes. You are not limited to two per assembly. For large projects, this can be very beneficial. In order to use this feature, files must use the partial keyword, and all of the partial class definitions must be defined in the same assembly.
Quick Quiz
Think of an interface as a class that is totally abstract. Interfaces contain no implementation details for any of their members; all their members are considered abstract. The advantage of using interfaces is the fact that a class can implement any number of interfaces. But, you can only inherit from one class, abstract or nonabstract.
The members of an interface can be methods, properties, or events. No implementation details are provided by the interface for any of its members. The definition of the interface simply specifies a signature for the members that must be supplied by classes implementing it.
Interfaces are usually named using an uppercase I as the first character.
Defining an Interface Interfaces can be defined as members of a namespace or class or by compiling to a DLL. An easy approach is to put the interface in a separate project. In Visual Studio .NET, use the Class Library template to start, and after you go into the Code Editor, replace the class definition with your interface definition. There is no separate template for interfaces.
Unlike an abstract class, it is not necessary to use the abstract keyword, probably because all methods are abstract. Build the interface DLL by using a Build option from the Build menu bar.
Implement the Interface Add a reference to the file ending in .dll. Type a using statement identifying the namespace for the interface. The heading for the class definition can specify not only a base class following the colon, but one or more interfaces separated by commas. When a class implements an interface, the body of the interface’s methods has to be written. A call to the interface methods resembles any other call.
.NET Framework Interfaces Interfaces play an important role in the .NET Framework. Collection classes such as the Array class, ArrayList class, Hashtable class, Stack class, and Queue class implement a number of interfaces. .NET includes these classes to enable you to manage collection of data. Each of the collection classes implements ICloneable.
Quick Quiz
Polymorphism is the ability for classes to provide different implementations of methods that are called by the same name. One quick example is with the ToString( ) method defined as one of the four methods of the Object class. Thus, in .NET, polymorphism is implemented through interfaces, inheritance, and the use of abstract classes.
Polymorphic Programming in .NET The interface describes the methods and the types of parameters each method member needs to receive and return, but it leaves the actual details of the body of the method up to the classes that implement the interface. Every class that implements the interface may have a completely different behavior. Some of the abstract class members can be marked as virtual or as abstract or can be completely implemented.
Through inheritance, polymorphism is made possible by allowing classes to override base class members. This makes dynamic binding possible. The CLR determines which method to call at run time based on which object calls the method. Marking the method with the virtual keyword enables derived classes to write their own functionality for the method.
Inheritance is very useful for adding to the functionality of an existing class without having to reinvent the wheel with each new application. Component programming is probably the way of the future for development. It is a powerful technique that enables you to implement the multiple interfaces of an object easily. The common goal of all these advanced object-oriented features is to enable polymorphic programming.
Quick Quiz
Generics, which are new to C# 2.0, reduce the need to rewrite algorithms for each data type. You identify where data will change in the code segment by putting a placeholder in the code for the type parameters. You can create generic classes, delegates, interfaces, and methods.
Generic Classes A generic class might use placeholder(s) for the data type of its instance data members or placeholders for return types of one or more of its methods. Once the generic class is defined, it could be instantiated using several different types of data.
Generic classes are defined by inserting an identifier between left and right brackets on the class definition line. Then when you create an instance of the class, you specify the actual type to substitute for the type parameters. This eliminates the boxing/unboxing or casting that was necessary when object solutions were developed.
Generic Methods You can define generic methods that are not part of a generic class. A generic method defers the specification of one or more types until the method is invoked by client code. This enables you to provide the same type of functionality to several different data types.
Defining a generic method is similar to defining a generic class. You insert an identifier between left and right brackets on the method definition line to indicate it is a generic method. Then place that identifier either in the parameter list or as a return type or in both places in the method body. When the method is invoked, the actual type for the placeholder is then specified.
Quick Quiz
With C# 4.0 a new data type of dynamic was added to the list of keywords. An object defined using the dynamic keyword can store anything. In most cases, it behaves like an object. No special casting or boxing and unboxing is necessary.
Dynamic data type Use the keyword dynamic. Dynamic types can be used to declare variables, as method parameters or method return types.
var data type var is not the same as dynamic. It is used to implicitly type variables that are declared inside a method. The keyword instructs the compiler to infer the type of the variable from the expression on the right side of the initialization statement.
Encourage students to declare members of a class of the same security level together. Data members should always be private, with most method members declared with public access.
This application demonstrates developing an application using components. It displays the organization’s name and the amount of the activity fees it will receive from the Student Government Organization. An abstract base class is created. An interface is designed. Three classes are derived from the base abstract class; two of those classes implement the interface. To test the design, a Windows application presentation class is created.
Class diagrams are included with the example. A prototype for the desired output is shown. Tables are included documenting the properties that were set during design. The complete program listing is shown in the book and is available as a Visual Studio project to demo for the class.
Discussion Questions
Some interesting topics of discussion in this chapter include:
Projects to Assign
All of the Multiple Choice Exercises, Problems 1- Odd-numbered Short Answer Exercises, Problems 21 and 23 Programming Exercises, Problems 5, 6, 7, and 9
Key Terms
¾ abstraction : manage complex problems by allowing you to abstract or identify the objects involved in the problem ¾ abstract method : one that does not include the implementation details for the method ¾ abstract class: a class that cannot be instantiated ¾ aggregation : classes that have a “has a” relationship in which a single class is defined to have instances of other class types ¾ assemblies : units that are configured and deployed in .NET ¾ client application : an application that instantiates objects of other classes or uses predefined components ¾ containment : classes that have a “has a” relationship in which a single class is defined to have instances of other class types ¾ derived class : new class that gets functionality from a base class; class appears to the left of the colon in the class definition ¾ dynamic binding : determining which method to call at run time based on which object calls the method ¾ dynamic data type : a memory location that can store anything ¾ encapsulation : packaging data attributes and behaviors into a single unit ¾ Global Assembly Cache : machinewide code cache that is available on every machine that has the Common Language Runtime installed ¾ inheritance : features that enable reuse of code through extending the functionality of the program units by creating a general class and then defining specialized classes that have access to the members of the general class ¾ interface : a class that is totally abstract; interfaces contain no implementation details for any of their members ¾ Intermediate Language Disassembler (ILDASM) : tool used to view an assembly ¾ overloaded method : more than one method using the same identifier ¾ parent class : base class from which other classes are derived; sometimes called super class ¾ partial class : a class that is divided into two or more files ¾ polymorphism : multiple implementations of the same behaviors so that the appropriate implementation can be executed based on the situation; polymorphism is the ability for classes to provide different implementations of methods that are called by the same name ¾ sealed class : a class that cannot be derived ¾ single inheritance : a class can extend or derive from at most one class ¾ super class : base class from which other classes are derived; sometimes called parent class ¾ var : a data type that is inferred from the expression on the right side of the initialization statement