C# Programming: From Problem Analysis to Program Design1 Advanced Object-Oriented Programming Features C# Programming: From Problem Analysis to Program.

Slides:



Advertisements
Similar presentations
Generics, Lists, Interfaces
Advertisements

OO Programming in Java Objectives for today: Overriding the toString() method Polymorphism & Dynamic Binding Interfaces Packages and Class Path.
Copyright © 2014 Pearson Addison-Wesley. All rights reserved. Chapter 17 Templates.
Generics and the ArrayList Class
The Web Warrior Guide to Web Design Technologies
Programming with Java © 2002 The McGraw-Hill Companies, Inc. All rights reserved. 1 Chapter 12 More OOP, Interfaces, and Inner Classes.
Programming Based on Events
OOP in Java Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
C# Programming: From Problem Analysis to Program Design1 Creating Your Own Classes C# Programming: From Problem Analysis to Program Design 3rd Edition.
C#.NET C# language. C# A modern, general-purpose object-oriented language Part of the.NET family of languages ECMA standard Based on C and C++
Advanced Object-Oriented Programming Features
Chapter 14 Generics and the ArrayList Class Copyright © 2010 Pearson Addison-Wesley. All rights reserved.
About the Presentations The presentations cover the objectives found in the opening of each chapter. All chapter objectives are listed in the beginning.
C# Programming: From Problem Analysis to Program Design1 10 Advanced Object-Oriented Programming Features C# Programming: From Problem Analysis to Program.
ASP.NET Programming with C# and SQL Server First Edition
OOP in Java Fawzi Emad Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
1 An Introduction to Visual Basic Objectives Explain the history of programming languages Define the terminology used in object-oriented programming.
CC1007NI: Further Programming Week Dhruba Sen Module Leader (Islington College)
C++ fundamentals.
1 Chapter One A First Program Using C#. 2 Objectives Learn about programming tasks Learn object-oriented programming concepts Learn about the C# programming.
McGraw-Hill© 2007 The McGraw-Hill Companies, Inc. All rights reserved. 1-1.
A First Program Using C#
CSM-Java Programming-I Spring,2005 Introduction to Objects and Classes Lesson - 1.
Chapter 12: Adding Functionality to Your Classes.
C++ Object Oriented 1. Class and Object The main purpose of C++ programming is to add object orientation to the C programming language and classes are.
Microsoft Visual Basic 2005: Reloaded Second Edition
Chapter 8: Writing Graphical User Interfaces
JavaScript, Fourth Edition
CSCI-383 Object-Oriented Programming & Design Lecture 13.
Chapter 6 Object-Oriented Java Script JavaScript, Third Edition.
Tuc Goodwin  Object and Component-Oriented Programming  Classes in C#  Scope and Accessibility  Methods and Properties  Nested.
Session 08 Module 14: Generics and Iterator Module 15: Anonymous & partial class & Nullable type.
Modern Software Development Using C#.NET Chapter 5: More Advanced Class Construction.
An Object-Oriented Approach to Programming Logic and Design Fourth Edition Chapter 6 Using Methods.
Data Structures Using C++ 2E1 Inheritance An “is-a” relationship –Example: “every employee is a person” Allows new class creation from existing classes.
An Object-Oriented Approach to Programming Logic and Design Chapter 3 Using Methods and Parameters.
C++ Programming Basic Learning Prepared By The Smartpath Information systems
Object Oriented Software Development
C# Programming: From Problem Analysis to Program Design1 10 Advanced Object-Oriented Programming Features C# Programming: From Problem Analysis to Program.
Standard Template Library The Standard Template Library was recently added to standard C++. –The STL contains generic template classes. –The STL permits.
C# Interfaces C# Class Version 1.0. Copyright © 2012 by Dennis A. Fairclough all rights reserved. 2 Interface  “A surface forming a common boundary between.
Introduction to c++ programming - object oriented programming concepts - Structured Vs OOP. Classes and objects - class definition - Objects - class scope.
1.
Programming with Java © 2002 The McGraw-Hill Companies, Inc. All rights reserved. 1 McGraw-Hill/Irwin Chapter 5 Creating Classes.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 26 - Java Object-Based Programming Outline 26.1Introduction.
Interfaces Chapter 9. 9 Creating Interfaces An interface is a contract. Every class that implements the interface must provide the interface’s defined.
PROGRAMMING IN C#. Collection Classes (C# Programming Guide) The.NET Framework provides specialized classes for data storage and retrieval. These classes.
Chapter 8 Class Inheritance and Interfaces F Superclasses and Subclasses  Keywords: super F Overriding methods  The Object Class  Modifiers: protected,
Introduction to Object-Oriented Programming Lesson 2.
Interfaces F What is an Interface? F Creating an Interface F Implementing an Interface F What is Marker Interface?
Java Programming, Second Edition Chapter Twelve Advanced Inheritance Concepts.
Chapter 11: Advanced Inheritance Concepts. Objectives Create and use abstract classes Use dynamic method binding Create arrays of subclass objects Use.
Programming in java Packages Access Protection Importing packages Java program structure Interfaces Why interface Defining interface Accessing impln thru.
CSCI-383 Object-Oriented Programming & Design Lecture 25.
1 C# - Inheritance and Polymorphism. 2 1.Inheritance 2.Implementing Inheritance in C# 3.Constructor calls in Inheritance 4.Protected Access Modifier 5.The.
Chapter  Array-like data structures  ArrayList  Queue  Stack  Hashtable  SortedList  Offer programming convenience for specific access.
Java Programming: Guided Learning with Early Objects Chapter 9 Inheritance and Polymorphism.
TK1924 Program Design & Problem Solving Session 2011/2012
Chapter 2: The Visual Studio .NET Development Environment
INF230 Basics in C# Programming
Ch 10- Advanced Object-Oriented Programming Features
Advanced Object-Oriented Programming Features
Advanced Object-Oriented Programming Features
Array Array is a variable which holds multiple values (elements) of similar data types. All the values are having their own index with an array. Index.
Advanced Object-Oriented Programming Features
C# Programming: From Problem Analysis to Program Design
Java – Inheritance.
Lecture Set 11 Creating and Using Classes
Abstract Classes and Interfaces
Interface 11: Interface Programming C# © 2003 DevelopMentor, Inc.
Presentation transcript:

C# Programming: From Problem Analysis to Program Design1 Advanced Object-Oriented Programming Features C# Programming: From Problem Analysis to Program Design 3rd Edition 11

Part II C# Programming: From Problem Analysis to Program Design2

3 Abstract Classes Used to prohibit other classes from instantiating objects of the base class Still inherit characteristics from base class in subclasses Base class can have data and method members [access modifier] abstract class ClassIdentifier { } // Base class

C# Programming: From Problem Analysis to Program Design4 Abstract Methods Only permitted in abstract classes Method has no body –Implementation details of the method are left up to classes derived from the base abstract class [access modifier] abstract returnType MethodIdentifier([parameter list]) ; // No { } included Declaration for abstract method ends with semicolon; NO method body or curly braces

C# Programming: From Problem Analysis to Program Design5 Abstract Methods ( continued ) Every class that derives from the abstract class must provide implementation details –Sign a contract that details how to implement its abstract methods –Syntax error if you use the keyword static or virtual when defining an abstract method No additional special keywords are used when a new class is defined to inherit from the abstract base class

C# Programming: From Problem Analysis to Program Design6 Partial Classes Break class up into two or more files –Each file uses partial class designation New features of C# 2.0 Used by Visual Studio for Windows applications –Code to initialize controls and set properties is placed in a somewhat hidden file in a region labeled “Windows Form Designer generated code” –File is created following a naming convention of “FormName.Designer.cs” or “xxx.Designer.cs” –Second file stores programmer code

C# Programming: From Problem Analysis to Program Design7 Interfaces All.NET languages only support single inheritance Think of an interface as a class that is totally abstract; all methods are abstract –Abstract classes can have abstract and regular methods –Implementing interface agrees to define details for all of the interface’s methods Classes can implement any number of interfaces –Only inherit from one class, abstract or nonabstract

C# Programming: From Problem Analysis to Program Design8 Interfaces ( continued ) General form [modifier] interface InterfaceIdentifier { // members - no access modifiers are used } Members can be methods, properties, or events –No implementations details are provided for any of its members

C# Programming: From Problem Analysis to Program Design9 Defining an Interface Can be defined as members of a namespace or class or by compiling to a DLL Easy approach is to put the interface in a separate project –Use the Class Library template Unlike abstract classes, it is not necessary to use the abstract keyword with methods –Because all methods are abstract

C# Programming: From Problem Analysis to Program Design10 Defining an Interface ( continued ) Figure ITraveler interface Build the interface DLL using Build option from Build menu bar

C# Programming: From Problem Analysis to Program Design11 Implement the Interface Follow the same steps as with the Person and Student DLLs –Add a reference to the file ending in.dll –Type a using statement Heading for the class definition specifies base class and one or more interfaces following the colon (:) [modifier] class ClassIdentifier : identifier [, identifier] Base class comes first

C# Programming: From Problem Analysis to Program Design12 Implement the Interface: PresentationGUI Application Figure PresentationGUI output using interface methods

C# Programming: From Problem Analysis to Program Design13.NET Framework Interfaces Play an important role in the.NET Framework –Collection classes such as Array class and HashTable class implement a number of interfaces

C# Programming: From Problem Analysis to Program Design14.NET Framework Interfaces ( continued ).NET Array class is an abstract class –Implements several interfaces (ICloneable; IList; ICollection; and IEnumerable) –Includes methods for manipulating arrays, such as: Iterating through the elements Searching by adding elements to the array Copying, cloning, clearing, and removing elements from the array Reversing elements Sorting

C# Programming: From Problem Analysis to Program Design15 NET Framework Interfaces ( continued ) HashTable is not abstract –Implements a number of interfaces public class Hashtable : IDictionary, ICollection, IEnumerable, ISerializable, IDeserializationCallback, ICloneable Implements the IDeserializationCallback interface Explore the documentation for these classes and interfaces

C# Programming: From Problem Analysis to Program Design16 Polymorphism Ability for classes to provide different implementations of methods called by the same name –ToString( ) method Dynamic binding –Determines which method to call at run time based on which object calls the method

C# Programming: From Problem Analysis to Program Design17 Polymorphic Programming in.NET Multiple classes can implement the same interface, each providing different implementation details for its abstract methods –“Black box” concept Abstract classes, classes that derive from them, are forced to include implementation details for any abstract method

C# Programming: From Problem Analysis to Program Design18 Generics Reduce the need to rewrite algorithms for each data type Create generic classes, delegates, interfaces, and methods Identify where data will change in the code segment by putting a placeholder in the code for the type parameters

C# Programming: From Problem Analysis to Program Design19 Generic Classes Defined by inserting an identifier between left and right brackets on the class definition line Example public class GenericClass { public T dataMember; } – //To instantiate an object, replace the T with data type GenericClass anIdentifer = new GenericClass ( );

C# Programming: From Problem Analysis to Program Design20 Generic Methods Similar to defining a generic class Insert identifier between left and right brackets on the method definition line to indicate it is a generic method Example public void SwapData (ref T first, ref T second) { T temp; temp = first; first = second; second = temp; } //To call the method, specify the type following method name SwapData (ref firstValue, ref secondValue);

Dynamic C# was originally characterized as being a strongly typed language –Compiler checks to ensure that only compatible values are attempting to be stored –Variables can still be defined as objects and then cast as different data types during run time Additional boxing/unboxing is needed C# Programming: From Problem Analysis to Program Design21

Dynamic Data Type Object defined using the dynamic keyword can store anything –No unboxing or casting is necessary prior to their use –With dynamic data types, the type checking occurs at run time –Once defined as dynamic, the memory location can hold any value C# Programming: From Problem Analysis to Program Design22

var Data Type Variables declared inside a method can be declared using the var keyword –Implicitly typed by the compiler - compiler determines the type One primary difference between dynamic and var is that var data items must be initialized when they are declared –Can declare a dynamic memory location and later associate values with it C# Programming: From Problem Analysis to Program Design23

C# Programming: From Problem Analysis to Program Design24 StudentGov Application Example Figure Problem specification for StudentGov example

C# Programming: From Problem Analysis to Program Design25 StudentGov Application Example ( continued )

C# Programming: From Problem Analysis to Program Design26 StudentGov Application Example ( continued ) Figure Prototype for StudentGov example

C# Programming: From Problem Analysis to Program Design27 StudentGov Application Example ( continued ) Figure Class diagrams for StudentGov example

C# Programming: From Problem Analysis to Program Design28 StudentGov Application Example ( continued ) Figure References added to StudentGov example

C# Programming: From Problem Analysis to Program Design29 Properties: StudentGov Application

C# Programming: From Problem Analysis to Program Design30 Properties: StudentGov Application ( continued )

C# Programming: From Problem Analysis to Program Design31 StudentGov Application Example ( continued ) Figure Setting the StartUp Project

C# Programming: From Problem Analysis to Program Design32 StudentGov Application Example ( continued ) Figure Part of the PresentationGUI assembly

C# Programming: From Problem Analysis to Program Design33 StudentGov Application Example ( continued ) Figure Output from StudentGov example

Coding Standards Declare members of a class of the same security level together When declaring methods that have too many arguments to fit on the same line, the leading parenthesis and the first argument should be written on the same line –Additional arguments are written on the following line and indented C# Programming: From Problem Analysis to Program Design34

C# Programming: From Problem Analysis to Program Design35 Chapter Summary Major features of object-oriented languages –Abstraction –Encapsulation –Inheritance –Polymorphism Multitier applications using component-based development methods

C# Programming: From Problem Analysis to Program Design36 Chapter Summary ( continued ) Use inheritance to extend the functionality of user- defined classes Abstract classes –Abstract methods Partial classes Interfaces

C# Programming: From Problem Analysis to Program Design37 Chapter Summary ( continued ) Why polymorphic programming? Generics –Generic classes –Generic methods