Download C# Programming: Object-Oriented Features and Component-Based Development and more Slides Advanced Computer Programming in PDF only on Docsity!
C# Programming: From Problem Analysis to Program Design 1
Advanced
Object-Oriented
Programming
Features
C# Programming: From Problem Analysis to Program Design 3rd Edition
C# Programming: From Problem Analysis to Program Design 2
Chapter Objectives
- 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
C# Programming: From Problem Analysis to Program Design 4
Chapter Objectives ( continued )
- 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
C# Programming: From Problem Analysis to Program Design 5
Object-Oriented Language
Features
- Abstraction
- Abstract or identify the objects involved in the problem
- Encapsulation
- Packaging data and behaviors into a single unit
- Inheritance
- Reuse of code through extending program units
- Polymorphism
- Multiple implementations of the same behaviors
C# Programming: From Problem Analysis to Program Design 7
Component-Based Development
( continued )
- Multitier applications
- Data access tier for accessing data from text files and databases
- Graphical user interface tier for user interaction
- Components implemented through classes in C#
- Class library files with a dynamic link library (DLL) extension
C# Programming: From Problem Analysis to Program Design 8
Inheritance
- Enables you to:
- Create a general class and then 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
- Classes can also have a "has a" relationship, not associated with inheritance - "has a" relationship is associated with containment or aggregation
C# Programming: From Problem Analysis to Program Design 10
Inheriting from Other .NET FCL
Classes
- Add functionality to programs with minimal programming
- Extend System.Windows.Forms.Form class to build GUIs (Button, Label, TextBox, ListBox) Base class
Derived class
Figure 11-3 Derived class
C# Programming: From Problem Analysis to Program Design 11
Creating Base Classes for
Inheritance
- Can define your own classes from which other classes can inherit
- Base class is called the super or parent class
- Data members are defined with a private access modifier
- Constructors are defined with public access modifiers
- Properties offer public access to data fields
C# Programming: From Problem Analysis to Program Design 13
Overriding Methods ( continued )
- Example of polymorphism
- ToString( ) method can have many different definitions
- ToString( ) uses the virtual modifier, implying that any class can override it
- Derived classes inherit from a base class
- Also called subclasses or child classes
- Protected access modifiers
- Access only to classes that derived from them
- Access to change data in the base class
C# Programming: From Problem Analysis to Program Design 14
Calling the Base Constructor
- To call the constructor for the base class, add keyword :base between the constructor heading for the subclass and the opening curly brace public Student(string id, string fname, string lname, string maj, int sId) :base (id, lname, fname) // base constructor arguments {...
- Base constructor must have a constructor with matching signature
C# Programming: From Problem Analysis to Program Design 16
Relationship
between the Person and Student Classes
Figure 11-5 Inheritance class diagram
C# Programming: From Problem Analysis to Program Design 17
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
- Adds a reference to the DLL
- That referenced file with the .dll extension becomes part of the application’s private assembly
C# Programming: From Problem Analysis to Program Design 19
Build Instead of Run to Create DLL
- Create the parent class first in order to use IntelliSense
- Create the subclass class in the same way as usual, except Build instead of Run the project to create the DLL
Figure 11-7 Attempting to run a class library file
C# Programming: From Problem Analysis to Program Design 20
Add Reference to Base Class
Figure 11-8 Adding a reference to a DLL
One of the first things to do is Add a Reference to the Parent DLL