Docsity
Docsity

Prepare for your exams
Prepare for your exams

Study with the several resources on Docsity


Earn points to download
Earn points to download

Earn points by helping other students or get them with a premium plan


Guidelines and tips
Guidelines and tips

Object-Oriented Programming - Advanced Programming - Lecture Notes, Study notes of Advanced Computer Programming

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

2012/2013

Uploaded on 04/30/2013

archa
archa 🇮🇳

4.3

(21)

143 documents

1 / 13

Toggle sidebar

Related documents


Partial preview of the text

Download Object-Oriented Programming - Advanced Programming - Lecture Notes and more Study notes Advanced Computer Programming in PDF only on Docsity! C# Programming: From Problem Analysis to Program Design, 3rd ed. 11-1 Chapter 11 Advanced Object-Oriented Programming Features At a Glance Instructor’s Manual Table of Contents ♦ Chapter Overview ♦ Chapter Objectives ♦ Instructor Notes ♦ Quick Quizzes ♦ Discussion Questions ♦ Projects to Assign ♦ Key Terms Docsity.com C# Programming: From Problem Analysis to Program Design, 3rd ed. 11-2 Lecture Notes 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: • Learn the major features of object-oriented languages • Design and develop multitier applications using component-based development methods • Use inheritance to extend the functionality of user-defined classes • Create abstract classes that include abstract methods • Distinguish the differences between sealed and abstract classes • Become aware of partial classes • Design and implement interfaces • Understand why polymorphic programming is a common goal in .NET • Explore generics and learn how to create generic classes and generic methods • Investigate static versus dynamic typing and become aware of when dynamic and var types are used • Work through a programming example that illustrates the chapter’s concepts Instructor Notes OBJECT-ORIENTED LANGUAGE FEATURES For a language to be considered a true object-oriented programming (OOP) language, it must support: • Abstraction • Encapsulation • Inheritance • Polymorphism 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. Docsity.com C# Programming: From Problem Analysis to Program Design, 3rd ed. 11-5 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. Docsity.com C# Programming: From Problem Analysis to Program Design, 3rd ed. 11-6 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. Docsity.com C# Programming: From Problem Analysis to Program Design, 3rd ed. 11-7 Global Assembly Cache The Global Assembly Cache is a machinewide code cache that stores assemblies designated to be shared by several applications. Quick Quiz 1. True or False: In order to reference a DLL component, you need to include a using directive that includes using className, wherein className is replaced by the name of the DLL class. Answer: False 2. True or False: To avoid typing the fully qualified name of the namespace, add a using directive. Answer: True 3. The ____________ tool enables you to see the source code of a DLL file. Answer: ILDASM 4. How does an overridden method differ from an overloaded method? Answer: An overridden method must have exactly the same signature as the base method. They provide a different implementation for the method. Overloaded methods must have a different signature than others with the same name. They often provide similar functionality with different data types. ABSTRACT CLASSES 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. Docsity.com C# Programming: From Problem Analysis to Program Design, 3rd ed. 11-10 3. True or False: In Visual Studio, create an interface using the Interface template. Answer: True 4. What advantage does the interface offer over the abstract class? Answer: You can implement multiple interfaces. You can derive from at most one class. POLYMORPHISM 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 1. ToString is an (overloaded/overridden) _________ method. Answer: overridden 2. True or False: All of the abstract class members must be defined as abstract. Answer: False 3. Polymorphism is the ability for classes to provide different ____________of methods that are called by the same name. Answer: implementations 4. How is polymorphism implemented in C#? Answer: Through interfaces, inheritance, and the use of abstract classes Docsity.com C# Programming: From Problem Analysis to Program Design, 3rd ed. 11-11 GENERICS 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 1. Generic classes are defined by inserting an identifier between left and right _________ on the class definition line. Answer: brackets 2. True or False: In order to define a generic method, you must tag the class as generic. Answer: False 3. No boxing/unboxing or ___________ is necessary when a generic method is created. Answer: casting 4. What type of generic structures can be created in C#? Answer: Classes, delegates, interfaces, and methods DYNAMIC 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. Docsity.com C# Programming: From Problem Analysis to Program Design, 3rd ed. 11-12 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. CODING STANDARDS 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. PROGRAMMING EXAMPLE: STUDENTGOV APPLICATION 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: • When would you use an abstract class versus an interface? • How do abstract classes differ from sealed classes? • How do “has a” relationships differ from “is a” relationships? • How does the dynamic data type differ from using var to declare variables? • How does creating a DLL differ from creating an EXE? Projects to Assign All of the Multiple Choice Exercises, Problems 1-20 Odd-numbered Short Answer Exercises, Problems 21 and 23 Programming Exercises, Problems 5, 6, 7, and 9 Docsity.com
Docsity logo



Copyright © 2024 Ladybird Srl - Via Leonardo da Vinci 16, 10126, Torino, Italy - VAT 10816460017 - All rights reserved