230 likes | 599 Views
Policy Modification . Please do not use the internet during the first hour of class Thanx!. Chapter 10 OO Features Chapter Objectives. Use inheritance to extend the functionality of user-defined classes Create abstract classes that include abstract methods Design and implement interfaces.
E N D
Policy Modification • Please do not use the internet during the first hour of class • Thanx!
Chapter 10 OO Features Chapter Objectives • Use inheritance to extend the functionality of user-defined classes • Create abstract classes that include abstract methods • Design and implement interfaces
Object-Oriented Language Features • Abstraction • Abstract or identify the objects involved in the problem • “thought of apart from concrete realities, specific objects, or actual instances “ • Encapsulation • Packaging data and behaviors into a single unit • Inheritance • Reuse of code through extension of class structure • Polymorphism – Many Forms • Multiple implementations of the same behaviors • Shape.Draw() • Shape.Area()
Inheritance • Enables you to • create a general class • define specialized classes that have access to the members of the general class • Associated with an "is a" relationship • Specialized class “is a” form of the general class • AirVehicle “is a” Vehicle • Classes can also have a "has a" relationship—not associated with inheritance • "has a" relationship associated with containmentor aggregation • Airplane “has a” Wing
Every object inherits from System.Object • Object Class • Supports all classes in the .NET Framework class hierarchy and provides low-level services to derived classes. This is the ultimate base class of all classes in the .NET Framework; it is the root of the type hierarchy. • http://msdn2.microsoft.com/en-us/library/system.object.aspx • Equals • The default implementation of Equals supports reference equality for reference types, and bitwise equality for value types. Reference equality means the object references that are compared refer to the same object. Bitwise equality means the objects that are compared have the same binary representation. • http://msdn2.microsoft.com/en-us/library/bsc2ak47.aspx • See documentation for • ToString, GetHashCode, GetType
Inheriting From Other .NET FCL Classes • Easily add base class functionality to programs • public class MyGame: Microsoft.Xna.Framework.Game • Extend System.Windows.Forms.Form class to build GUIs (Button, Label, TextBox, ListBox) Base class Derived class
Base Classes and Inheritance • Define classes from which other classes can inherit • Base class is called the super or parent class • Define data members with a protected access modifier • Define constructors with public access modifiers base/parent class derived/child class • public class Vehicle : AirVehicle
MethodsVirtual and Override • Use virtual to indicate that a method or property in a base class may be overridden in a derived class • i.e. override Equals method to support value equality • Use override with a method or property in a derived class. You then provide the new code for the method or property in the derived class. This code is executed instead of the code in the base class whose method or property is overridden. • public override bool Equals(object obj)
Overriding Methods • Replace the method defined at a higher level • Keyword override included in derived class • Base method includes virtual, abstract, or override keyword • Overriding differs from overloading a method • Overridden methods have exactly the same signature • Overloaded methods each have a different signature
Overriding Methods (continued) • Overriding the ToString method is an example of polymorphism • many forms • ToString( ) method can have many different definitions • ToString( ) uses the virtual modifier implying any class can override it • public virtual string ToString() • Override with • public override string ToString()
Using Members of the Base Class • Scope • Methods defined in subclass take precedence when named the same name as member of a parent class • Can call an overridden method of the base class • Use keyword base before the method name returnbase.GetSleepAmt( ) // Calls GetSleepAmt( ) in // parent class
Making Stand-alone Components • Compile class and create an assembly • Assemblies are units configured and deployed in .NET • Classes can be compiled and stored as a dynamic link library (DLL) instead of into the EXE file type • Add a reference to the DLL • That referenced file with the .dll extension becomes part of the application’s private assembly
Dynamic Link Libraries • Dynamic Link Libraries • Loaded “dynamically” at run time • May be loaded by multiple applications • Cannot be run directly
Creating a Client Application To Use the DLL • DLL components can be reused with many different applications • Two Steps • Add a reference to the DLL components • Include a using statement with the namespace • Then declare an objects of the component type(s) • Use members of the derived, base or referenced classes
In Class Exercise • Create a Bubble Sort DLL • csc /out:BubSortLib.dll /target:library BubSort.cs • Create a Bubble Sort Test Driver • Add a using statement • using MyBubSortLibNamespace • Compile with: csc /r:BubSortLib.dll BubSortTest.cs • Make sure BubSortLib.dll is in the same directory
Assignment • Create an AirVehicle class and a GroundVehicle class • These classes inherit from the Vehicle class (previous homework) • Add virtual methods to your Vehicle class • Accelerate • Brake • Override these methods in the AirVehicle and GroundVehicle classes • Build a dynamic link library using the command line compiler • Create a project using Visual C# Express or the command line compiler • Add reference to Vehicle Library your project • Copy dll to project directory • Your project should not include the vehicle library cs files • Create a driver that uses your Vehicle.dll library • You can create a console application that simply uses Console.WriteLine to identify execution of the overridden Accelerate and Brake methods • Demonstrate project in class next week
Summary • Major features of object-oriented languages • Abstraction • Encapsulation • Inheritance • Polymorphism • Multitier applications using component-based development methods
Chapter Summary (continued) • Use inheritance to extend the functionality of user defined classes • virtual – indicates the method may be overriden in a derived class • override – indicates base class functionality being replaced by derived class functionality • new – hides base class member in a derived class • public new void MyMethod()
Adding a Reference to the Base Class One of the first things to do is Add a Reference to the Parent DLL
Adding a Reference to the Base Class (continued) Use Browse button to locate DLL
Adding a New Using Statement • In the subclass class, if you simply type the following, you receive an error message publicclass Student : Person
Adding a New Using Statement (continued) Notice fully qualified name • To avoid error, could type publicclass Student : PersonNamespace.Person • Better option is to add a using directive using PersonNamespace; // Use whatever name you // typed for the namespace for Person • After typing program statements, build the DLL from the Build option under the Build menu bar