5. OOP. 2 Microsoft Objectives “Classes, objects and object-oriented programming (OOP) play a fundamental role in.NET. C# features full support for the.

Slides:



Advertisements
Similar presentations
Generics, Lists, Interfaces
Advertisements

CHARLES UNIVERSITY IN PRAGUE faculty of mathematics and physics C# Language &.NET Platform 12 th -13 th Lecture Pavel Ježek.
Language Fundamentals in brief C# - Introduction.
Priority Queue Erick, Eka, Reddy © Sekolah Tinggi Teknik Surabaya 1.
CSE 1302 Lecture 8 Inheritance Richard Gesick Figures from Deitel, “Visual C#”, Pearson.
Interfaces Reference: Joe Hummel. 2 UCN Technology: Computer Science 2012 Objectives “Good class design starts with good application design — how many.
OBJECT-ORIENTED PROGRAMMING. What is an “object”? Abstract entity that contains data and actions Attributes (characteristics) and methods (functions)
Section 5 – Classes. Object-Oriented Language Features Abstraction –Abstract or identify the objects involved in the problem Encapsulation –Packaging.
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
C# Programming: From Problem Analysis to Program Design1 Advanced Object-Oriented Programming Features C# Programming: From Problem Analysis to Program.
3. Data Types. 2 Microsoft Objectives “.NET is designed around the CTS, or Common Type System. The CTS is what allows assemblies, written in different.
Data Abstraction and Object- Oriented Programming CS351 – Programming Paradigms.
C# Programming: From Problem Analysis to Program Design1 10 Advanced Object-Oriented Programming Features C# Programming: From Problem Analysis to Program.
4. Statements and Methods. 2 Microsoft Objectives “With regards to programming statements and methods, C# offers what you would come to expect from a.
Inheritance and Polymorphism CS351 – Programming Paradigms.
Differences between C# and C++ Dr. Catherine Stringfellow Dr. Stewart Carpenter.
OOP Languages: Java vs C++
To define a class in Visual Basic.NET, you can follow this general procedure: 1. Add a class to the project. 2. Provide an appropriate file name for.
Programming Languages and Paradigms Object-Oriented Programming.
Chapter 4 Inheritance Bernard Chen Spring Objective IS-A relationships and the allowable changes for derived classes The concept of polymorphism.
Classes and Class Members Chapter 3. 3 Public Interface Contract between class and its clients to fulfill certain responsibilities The client is an object.
Lecture 8 Inheritance Richard Gesick. 2 OBJECTIVES How inheritance promotes software reusability. The concepts of base classes and derived classes. To.
Object Oriented Concepts
Object Oriented Concepts Classes II. Object Oriented concepts Encapsulation Composition Inheritance Polymorphism.
Lecture 9 Polymorphism Richard Gesick.
Introduction to Object Oriented Programming. Object Oriented Programming Technique used to develop programs revolving around the real world entities In.
BIM313 – Advanced Programming Techniques Object-Oriented Programming 1.
FEN 2012 UCN Technology: Computer Science1 C# - Introduction Language Fundamentals in Brief.
C# Programming Fundamentals of Object-Oriented Programming Fundamentals of Object-Oriented Programming Introducing Microsoft.NET Introducing Microsoft.NET.
1 Chapter 10: Data Abstraction and Object Orientation Aaron Bloomfield CS 415 Fall 2005.
Object Oriented Programming: Java Edition By: Samuel Robinson.
Programming Languages and Paradigms Object-Oriented Programming (Part II)
Programming Languages and Paradigms Object-Oriented Programming.
Lecture 8: Object-Oriented Design. 8-2 MicrosoftIntroducing CS using.NETJ# in Visual Studio.NET Objectives “Good object-oriented programming is really.
CS200 Algorithms and Data StructuresColorado State University Part 4. Advanced Java Topics Instructor: Sangmi Pallickara
Chapter 7 Objects and Classes 1 Fall 2012 CS2302: Programming Principles.
Interfaces 1. Interfaces are (parts of) contracts Interfaces are contracts between implementers and consumers Consumers: Programmers using a class implementing.
Modern Software Development Using C#.NET Chapter 5: More Advanced Class Construction.
C# F 1 CSC 298 Object Oriented Programming (Part 1)
Object-Oriented Programming. An object is anything that can be represented by data in a computer’s memory and manipulated by a computer program.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver Chapter 8: Class Relationships Data Abstraction & Problem Solving.
Chapter 3 Inheritance and Polymorphism Goals: 1.Superclasses and subclasses 2.Inheritance Hierarchy 3.Polymorphism 4.Type Compatibility 5.Abstract Classes.
Topic 1 Object Oriented Programming. 1-2 Objectives To review the concepts and terminology of object-oriented programming To discuss some features of.
C# Programming: From Problem Analysis to Program Design1 10 Advanced Object-Oriented Programming Features C# Programming: From Problem Analysis to Program.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 26 - Java Object-Based Programming Outline 26.1Introduction.
Introduction to Java Chapter 7 - Classes & Object-oriented Programming1 Chapter 7 Classes and Object-Oriented Programming.
Inheritance. Inheritance - Introduction Idea behind is to create new classes that are built on existing classes – you reuse the methods and fields and.
Classes. Constructor A constructor is a special method whose purpose is to construct and initialize objects. Constructor name must be the same as the.
Session 07: C# Design Patterns
PROGRAMMING IN C#. Collection Classes (C# Programming Guide) The.NET Framework provides specialized classes for data storage and retrieval. These classes.
Introduction to Object-Oriented Programming Lesson 2.
.NET Mobile Application Development XML Web Services.
Presented by Ted Higgins, SQL Server DBA An Introduction to Object – Oriented Programming.
Inheritance and Polymorphism
C# Interfaces and RTTI CNS 3260 C#.NET Software Development.
Module 13: Properties and Indexers. Overview Using Properties Using Indexers.
C# Fundamentals An Introduction. Before we begin How to get started writing C# – Quick tour of the dev. Environment – The current C# version is 5.0 –
Lecture 8: Collections, Comparisons and Conversions. Svetla Boytcheva AUBG, Spring COS 240 Object-Oriented Languages.
Classes (Part 1) Lecture 3
C# - OOP TTU IT College , Autumn SEMESTER Andres käver.
Advanced Object-Oriented Programming Features
Module 5: Common Type System
Implementing Polymorphism
Lecture 23 Polymorphism Richard Gesick.
Chapter 8: Class Relationships
CIS 199 Final Review.
Quiz Points 1 Rules Raise your hand if you know the question
2.1 Introduction to Object-Oriented Programming
5. OOP OOP © 2003 Microsoft.
Presentation transcript:

5. OOP

2 Microsoft Objectives “Classes, objects and object-oriented programming (OOP) play a fundamental role in.NET. C# features full support for the object- oriented programming paradigm…” Designing your own classes Destroying objects and garbage collection Inheritance Interfaces

3 Microsoft Part 1 Designing your own classes…

4 Microsoft Motivation.NET contains thousands of prebuilt classes in the FCL So why design your own? –to model entities unique to your application domain… Examples: –employees –customers –products –orders –documents –business units –etc.

5 Microsoft Simple class members C# supports standard fields, methods and constructors –with standard access control: public, private, protected public class Person { public string Name; // fields public int Age; public Person() // default constructor { this.Name = "?"; this.Age = -1; } public Person(string name, int age) // parameterized ctor { this.Name = name; this.Age = age; } public override string ToString() // method { return this.Name; } }//class

6 Microsoft Basic design rules Provide constructor(s) Omit default constructor for parameterized initialization Override ToString, Equals and GetHashCode Data hiding: "hide as many details as you can" –enable access when necessary via accessors and mutators –.NET provides a cleaner mechanism via properties…

7 Microsoft Properties Goal: –to allow our class users to safely write code like this: –provides field-like access with method-like semantics… –… enabling access control, validation, data persistence, screen updating, etc. Person p; p = new Person("joe hummel", 40); p.Age = p.Age + 1;

8 Microsoft Observation Read of value ("Get") vs. Write of value ("Set") Person p; p = new Person("joe hummel", 40); p.Age = p.Age + 1; Get age Set age

9 Microsoft Property implementation Implementation options: –read-only –write-only –read-write public class Person { private string m_Name; private int m_Age;. public string Name { get {... } } public int Age { get {... } set {... } } } read-only read-write

10 Microsoft Example Simplest implementation just reads / writes private field: public class Person { private string m_Name; private int m_Age;. public string Name // Name property { get { return this.m_Name; } } public int Age // Age property { get { return this.m_Age; } set { this.m_Age = value; } }

11 Microsoft Indexers Enable array-like access with method-like semantics –great for data structure classes, collections, etc. People p; // collection of Person objects p = new People(); p[0] = new Person("joe hummel", 40);. age = p[0].Age; Set Get

12 Microsoft Example Implemented like properties, with Get and Set methods: public class People { private Person[] m_people; // underlying array. public Person this[int i] // int indexer { get { return this.m_people[i]; } set { this.m_people[i] = value; } } public Person this[string name] // string indexer { get { return...; } } read-only read-write

13 Microsoft Part 2 Destroying objects and garbage collection…

14 Microsoft Object creation and destruction Objects are explicitly created via new Objects are never explicitly destroyed! –.NET relies upon garbage collection to destroy objects –garbage collector runs unpredictably…

15 Microsoft Finalization Objects can be notified when they are garbage collected Garbage collector (GC) will call object's finalizer public class Person {. ~Person() // finalizer {... }

16 Microsoft Should you rely upon finalization? No! –it's unpredictable –it's expensive (.NET tracks object on special queue, etc.) Alternatives? –design classes so that timely finalization is unnecessary –provide Close / Dispose method for class users to call ** Warning ** As a.NET programmer, you are responsible for calling Dispose / Close. Rule of thumb: if you call Open, you need to call Close / Dispose for correct execution. Common examples are file I/O, database I/O, and XML processing.

17 Microsoft Part 3 Inheritance…

18 Microsoft Inheritance Use in the small, when a derived class "is-a" base class –enables code reuse –enables design reuse & polymorphic programming Example: –a Student is-a Person Undergraduate Person StudentEmployee GraduateStaffFaculty

19 Microsoft Implementation C# supports single inheritance –public inheritance only (C++ parlance) – base keyword gives you access to base class's members public class Student : Person { private int m_ID; public Student(string name, int age, int id) // constructor :base(name, age) { this.m_ID = id; } Student Person

20 Microsoft Binding C# supports both static and dynamic binding –determined by absence or presence of virtual keyword –derived class must acknowledge with new or override public class Person {. // statically-bound public string HomeAddress() { … } // dynamically-bound public virtual decimal Salary() { … } } public class Student : Person {. public new string HomeAddress() { … } public override decimal Salary() { … } }

21 Microsoft All classes inherit from System.Object

22 Microsoft Part 4 Interfaces…

23 Microsoft Interfaces An interface represents a design Example: –the design of an object for iterating across a data structure –interface = method signatures only, no implementation details! –this is how foreach loop works… public interface IEnumerator { void Reset(); // reset iterator to beginning bool MoveNext(); // advance to next element object Current { get; } // retrieve current element }

24 Microsoft Why use interfaces? Formalize system design before implementation –especially helpful for PITL (programming in the large) Design by contract –interface represents contract between client and object Decoupling –interface specifies interaction between class A and B –by decoupling A from B, A can easily interact with C, D, …

25 Microsoft.NET is heavily influenced by interfaces IComparable ICloneable IDisposable IEnumerable & IEnumerator IList ISerializable IDBConnection, IDBCommand, IDataReader etc.

26 Microsoft Example Sorting –FCL contains methods that sort for you –sort any kind of object –object must implement IComparable object[] students; students = new object[n]; students[0] = new Student(…); students[1] = new Student(…);. Array.Sort(students); public interface IComparable { int CompareTo(object obj); }

27 Microsoft To be a sortable object… Sortable objects must implement IComparable Example: –Student objects sort by id public class Student : Person, IComparable { private int m_ID;. int IComparable.CompareTo(Object obj) { Student other; other = (Student) obj; return this.m_ID – other.m_ID; } base classinterface Student Person

28 Microsoft Summary Object-oriented programming is *the* paradigm of.NET C# is a fully object-oriented programming language –fields, properties, indexers, methods, constructors –garbage collection –single inheritance –interfaces Inheritance? –consider when class A "is-a" class B –but you only get single-inheritance, so make it count Interfaces? –consider when class C interacts with classes D, E, F, … –a class can implement any number of interfaces

29 Microsoft References Books: –I. Pohl, "C# by Dissection" –S. Lippman, "C# Primer" –J. Mayo, "C# Unleashed"