CSCI 3328 Object Oriented Programming in C# Chapter 9: Classes and Objects: A Deeper Look UTPA – Fall 2012 This set of slides is revised from lecture.

Slides:



Advertisements
Similar presentations
CSCI 3328 Object Oriented Programming in C# Chapter 9: Classes and Objects: A Deeper Look 1 Xiang Lian The University of Texas – Pan American Edinburg,
Advertisements

IEG 3080 Tutorial 2 Jack Chan. Prepared by Jack Chan, Spring 2007 Outline.Net Platform Object Oriented Concepts Encapsulation Inheritance Polymorphism.
1 Chapter 6: Extending classes and Inheritance. 2 Basics of Inheritance One of the basic objectives of Inheritance is code reuse If you want to extend.
1 l Inheritance Basics l Programming with Inheritance l Dynamic Binding and Polymorphism Inheritance.
CS 211 Inheritance AAA.
CSE 1302 Lecture 8 Inheritance Richard Gesick Figures from Deitel, “Visual C#”, Pearson.
Inheritance Inheritance Reserved word protected Reserved word super
Section 5 – Classes. Object-Oriented Language Features Abstraction –Abstract or identify the objects involved in the problem Encapsulation –Packaging.
1 Chapter 7 l Inheritance Basics l Programming with Inheritance l Dynamic Binding and Polymorphism Inheritance.
Classes Mark Hennessy Dept. Computer Science NUI Maynooth C++ Workshop 18 th – 22 nd Spetember 2006.
“is a”  Define a new class DerivedClass which extends BaseClass class BaseClass { // class contents } class DerivedClass : BaseClass { // class.
Lecture 8 Inheritance Richard Gesick. 2 OBJECTIVES How inheritance promotes software reusability. The concepts of base classes and derived classes. To.
Object Oriented Concepts Classes II. Object Oriented concepts Encapsulation Composition Inheritance Polymorphism.
CIS 3301 C# Lesson 7 Introduction to Classes. CIS 3302 Objectives Implement Constructors. Know the difference between instance and static members. Understand.
Programming using C# for Teachers Introduction to Objects Reference Types Functions of Classes Attributes and Types to a class LECTURE 2.
Object Based Programming Chapter 8. 2 In This Chapter We will learn about classes Garbage Collection Data Abstraction and encapsulation.
Visual C# 2012 for Programmers © by Pearson Education, Inc. All Rights Reserved.
CIS 270—Application Development II Chapter 8—Classes and Objects: A Deeper Look.
CSCI 3328 Object Oriented Programming in C# Chapter 9: Classes and Objects: A Deeper Look – Exercises 1 Xiang Lian The University of Texas Rio Grande Valley.
Polymorphism, Virtual Methods and Interfaces Version 1.1.
Classes Methods and Properties. Introduction to Classes and Objects In object-oriented programming terminology, a class is defined as a kind of programmer-defined.
Class Inheritance SWE 344 Internet Protocols & Client Server Programming.
Inheritance ndex.html ndex.htmland “Java.
Spring 2008 Mark Fontenot CSE Honors Principles of Computer Science I Note Set 12.
POLYMORPHISM Chapter 6. Chapter Polymorphism  Polymorphism concept  Abstract classes and methods  Method overriding  Concrete sub classes and.
Programming Fundamentals1 Chapter 7 INTRODUCTION TO CLASSES.
Section 3: Component/Object Oriented Programming, Events, and Delegates in.NET Jim Fiddelke, Greenbrier & Russel.
Object Based Programming Chapter 8. 2 Contrast ____________________ Languages –Action oriented –Concentrate on writing ________________ –Data supports.
Java Programming: Guided Learning with Early Objects Chapter 9 Inheritance and Polymorphism.
CompSci 230 S Programming Techniques
Inheritance Modern object-oriented (OO) programming languages provide 3 capabilities: encapsulation inheritance polymorphism which can improve the design,
Inheritance Chapter 7 Inheritance Basics Programming with Inheritance
Chapter - 4 OOP with C# S. Nandagopalan.
Inheritance ITI1121 Nour El Kadri.
Object-Oriented Programming: Classes and Objects
Inheritance and Polymorphism
Object Oriented Programming using Java - Class Instance Variables
Object-Oriented Programming & Design Lecture 18 Martin van Bommel
Polymorphism.
Inheritance Modern object-oriented (OO) programming languages provide 3 capabilities: encapsulation inheritance polymorphism which can improve the design,
Object-Oriented Programming: Classes and Objects
Inheritance Chapter 7 Inheritance Basics Programming with Inheritance
OOP’S Concepts in C#.Net
The University of Texas Rio Grande Valley
Object Based Programming
Inheritance Basics Programming with Inheritance
Java Inheritance.
Lecture 22 Inheritance Richard Gesick.
د.سناء الصايغ الفصل الأول البرمجة الشيئية
CSCI 3328 Object Oriented Programming in C# Chapter 9: Classes and Objects: A Deeper Look – Exercises UTPA – Fall 2012 This set of slides is revised from.
Object Oriented Programming
Packages From Deitel & Deitel.
The University of Texas Rio Grande Valley
Computer Programming with JAVA
CSCI 3328 Object Oriented Programming in C# Chapter 3: Introduction to Classes and Objects – Exercises UTPA – Fall 2012 This set of slides is revised.
9: POLYMORPHISM Programming Technique II (SCSJ1023) Jumail Bin Taliba
The University of Texas – Pan American
The University of Texas – Pan American
Inheritance Chapter 7 Inheritance Basics Programming with Inheritance
Object-Oriented Programming: Inheritance and Polymorphism
Fundaments of Game Design
CS360 Client/Server Programming Using Java
CIS 199 Final Review.
Classes & Objects A deeper Look Chapter 10 & 11
CSCI 3328 Object Oriented Programming in C# Chapter 8: LINQ and Generic Collections – Exercises UTPA – Fall 2012 This set of slides is revised from lecture.
Chapter 11 Inheritance and Encapsulation and Polymorphism
Java Inheritance.
C++ Object Oriented 1.
CSCI 3328 Object Oriented Programming in C# Chapter 7: Arrays – Exercises UTPA – Fall 2012 This set of slides is revised from lecture slides of Prof.
Presentation transcript:

CSCI 3328 Object Oriented Programming in C# Chapter 9: Classes and Objects: A Deeper Look UTPA – Fall 2012 This set of slides is revised from lecture slides of Prof. John Abraham. -- Xiang Lian

Objectives In this chapter, you will Learn some examples of classes and objects Know how to use keywords: this, static, readonly Get familiar with the concept of data encapsulation and data hiding

Example: Time Class Contains 3 int variables declared as private Also, 3 public methods: SetTime, ToUniversalString and ToString Does not have a constructor Instead time is passed in using public setTime, where the three integers are validated to be within range, if not 0 is assigned When constructor is not defined, the compiler adds a default one, assigning 0’s to all three int variables

Code for Time1 Class public class Time1 { private int hour; private int minute; private int second; public void SetTime(int h, int m, int s) hour = ((h>=0 &&h<24)? h:0); minute = ((m>=0 &&m<60)? m:0); second = ((s>=0 &&s<60)? s:0); } public string ToUniversalString() return string.Format("{0:D2}:{1:D2}:{2:D2}", hour, minute, second); public override string ToString() return string.Format("{0}:{1:D2}:{2:D2} {3}", ((hour == 0|| hour == 12) ? 12 : hour%12), minute, second, (hour<12) ? "AM" : "PM"));

Example: Time Class (cont'd) Method ToUniversalString returns a string in unversal-time format. Return string.Format(“{0:D2}:{1:D2}:{2:D2}”,hour, minute, second); //like 13:27:06 Method ToString() overrides string Return string.Format (“{0:D2}:{1:D2}:{2:D2}”,((hour==0||hour==12)?12:hour%12), etc.); // if hour is 0 or 12 it appears as 12 AM, or PM, else it appears from 1 to 11, if hour is <12 it is AM.

Invoke Methods of Time Object Time1 time = new Time1(); Console.WriteLine("The initial universal time is" + time.ToUniversalString()); 00:00:00 Console.WriteLine("The initial universal time is" + time.ToString()); 12:00:00 AM time.SetTime (13, 27, 6); 13:27:06 1:27:06 PM time.SetTime(99,99,99);

Indexers The indices can be either integer (as conventional arrays) or non-integer such as the data element name Indexers are defined like properties in a class See the example

class IntIndexer { private string[] myData; public IntIndexer(int size) myData = new string[size]; for (int i = 0; i < size; i++) myData[i] = "empty"; } public string this[int pos] get return myData[pos]; set myData[pos] = value;

static void Main(string[] args) { int size = 10; IntIndexer myInd = new IntIndexer(size); myInd[0] = "Artem Chebotko"; myInd[1] = "Robert Scheweller"; myInd[2] = "Pearl Brazier"; myInd[3] = "Laura Grabowski"; myInd[4] = "John Abraham"; myInd[5] = "Emmet Tomai"; myInd[6] = "Yang Liu"; myInd[7] = "Bin Fu"; myInd[8] = "Xiang Lian"; Console.WriteLine("\nIndexer Output\n"); for (int i = 0; i < size; i++) Console.WriteLine("myInd[{0}]: {1}", i, myInd[i]); } Console.ReadKey();

Keyword: this public class SimpleTime { private int hour; private int minute; private int second; public void SetTime(int hour, int minute, int second) this.hour = hour; this.minute = minute this.second = second; } pubic void Reset() this.SetTime(0, 0, 0);

More Keywords: readonly private readonly int INCREMENT; By convention, readonly variables are with capital letters like constants Constructors can initialize readonly variables for multiple times

More Keywords: static private static int count = 0; Maintain the number of objects for a class Static variables belong to the class, not objects Static variables can be shared by multiple objects Static variables can be referred by properties public static int Count { get {return count;} }

Encapsulation Let the program hide some of the data and operation of a class while exposing others private Implementation of a method is hidden from the user, so it appears like a black box Think of a person driving a car He does not need to know the internal working of the engine or the way gear changes work, to be able to drive the car (Encapsulation) Instead, he needs to know things such as how much turning the steering wheel needs, etc. (Abstraction)

Inheritance A new class is created by absorbing an existing class’s members and enhancing them with new or modified capabilities. Base class : the existing class from which a new class inherits members. Derived class : the new class that inherited from the base class. Each new class can become the base class for a future derived class. Is-a relationship and has-a relationship. Is-a represents inheritance. Has-a represents a composition. Inheritance allows you to reuse code

Inheritance (cont'd) public class ParentClass { public ParentClass() Console.WriteLine("Parent Constructor."); } public void print() Console.WriteLine("I'm a Parent Class."); public class ChildClass : ParentClass public ChildClass() Console.WriteLine("Child Constructor."); public static void Main() ChildClass child = new ChildClass(); child.print(); Console.ReadKey();

Polymorphism Enables you to “program in general” than “program in specific” Enables us to write applications that process objects that share the same base class in a class hierarchy as if they were all objects of the base class It allows you to invoke derived class methods through a base class reference during run-time overrides means they provide their own definition and implementation. At run-time, when client code calls the method, the CLR looks up the run-time type of the object, and invokes that override of the virtual method Thus in your source code you can call a method on a base class, and cause a derived class's version of the method to be executed