







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
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
This page cannot be seen from the preview
Don't miss anything!








♦ 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.
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.
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
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.
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