Neal Stublen What’s an indexer?  An array has an indexer… int[] myArray = new int[5]; for (int index = 0; index < 5; ++index) {

Slides:



Advertisements
Similar presentations
OOP Abstraction Classes Class Members: Properties & Methods Instance (object) Encapsulation Interfaces Inheritance Composition Polymorphism Using Inheritance.
Advertisements

CHARLES UNIVERSITY IN PRAGUE faculty of mathematics and physics C# Language &.NET Platform 12 th -13 th Lecture Pavel Ježek.
Intro to CS – Honors I Inheritance and Polymorphism GEORGIOS PORTOKALIDIS
Inheritance Inheritance Reserved word protected Reserved word super
Object-Oriented Application Development Using VB.NET 1 Chapter 8 Understanding Inheritance and Interfaces.
SE-1020 Dr. Mark L. Hornick 1 Inheritance and Polymorphism: Abstract Classes The “not quite” classes.
© 2006 Pearson Addison-Wesley. All rights reserved9 A-1 Chapter 9 Advanced Java Topics (inheritance review + Java generics)
© 2006 Pearson Addison-Wesley. All rights reserved9 A-1 Chapter 9 Advanced Java Topics.
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
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter N - 1 Chapter 13 Polymorphism is-a relationships Interfaces.
1 Evan Korth New York University Inheritance and Polymorphism Professor Evan Korth New York University.
Aalborg Media Lab 23-Jun-15 Inheritance Lecture 10 Chapter 8.
1 Evan Korth New York University Inheritance and Polymorphism Professor Evan Korth New York University.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter N - 1 Chapter 13 Polymorphism is-a relationships Interfaces.
8.1 Classes & Inheritance Inheritance Objects are created to model ‘things’ Sometimes, ‘things’ may be different, but still have many attributes.
Object-Oriented Application Development Using VB.NET 1 Chapter 8 Understanding Inheritance and Interfaces.
Types in programming languages What are types, and why do we need them? Types in programming languages1.
Differences between C# and C++ Dr. Catherine Stringfellow Dr. Stewart Carpenter.
Inheritance. Types of Inheritance Implementation inheritance means that a type derives from a base type, taking all the base type’s member fields and.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Taken from slides of Starting Out with C++ Early Objects Seventh Edition.
“is a”  Define a new class DerivedClass which extends BaseClass class BaseClass { // class contents } class DerivedClass : BaseClass { // class.
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.
Inheritance and Class Hierarchies Ellen Walker CPSC 201 Data Structures Hiram College.
Introduction to Java Prepared by: Ahmed Hefny. Outline Classes Access Levels Member Initialization Inheritance and Polymorphism Interfaces Inner Classes.
Chris Kiekintveld CS 2401 (Fall 2010) Elementary Data Structures and Algorithms Inheritance and Polymorphism.
ILM Proprietary and Confidential -
E FFECTIVE C# 50 Specific Ways to Improve Your C# Second Edition Bill Wagner محمد حسین سلطانی.
Programming in Java CSCI-2220 Object Oriented Programming.
Object Oriented Software Development
Session 04 Module 8: Abstract classes and Interface Module 9: Properties and Indexers.
Module 10: Inheritance in C#. Overview Deriving Classes Implementing Methods Using Sealed Classes Using Interfaces Using Abstract Classes.
Types in programming languages1 What are types, and why do we need them?
Inheritance and Access Control CS 162 (Summer 2009)
1 COSC2007 Data Structures II Chapter 9 Class Relationships.
Neal Stublen Tonight’s Agenda  Indexers  Delegates and events  Operator overloading  Class inheritance  Q&A.
Application development with Java Lecture 21. Inheritance Subclasses Overriding Object class.
 In the java programming language, a keyword is one of 50 reserved words which have a predefined meaning in the language; because of this,
Coming up: Inheritance
© 2007 Lawrenceville Press Slide 1 Chapter 9 Inheritance  One class is an extension of another.  Allows a class to define a specialized type of an existing.
Access Specifier. Anything declared public can be accessed from anywhere. Anything declared private cannot be seen outside of its class. When a member.
FEN 2014UCN Teknologi/act2learn1 Object-Oriented Programming “ The Three Pillars of OOP”: Encapsulation Inheritance Polymorphism The Substitution Principle.
Presented by Ted Higgins, SQL Server DBA An Introduction to Object – Oriented Programming.
Object orientation and Packaging in Java Object Orientation and Packaging Introduction: After completing this chapter, you will be able to identify.
1 C# - Inheritance and Polymorphism. 2 1.Inheritance 2.Implementing Inheritance in C# 3.Constructor calls in Inheritance 4.Protected Access Modifier 5.The.
Terms and Rules II Professor Evan Korth New York University (All rights reserved)
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Outline Creating Subclasses Overriding Methods Class Hierarchies Inheritance.
COP INTERMEDIATE JAVA Inheritance, Polymorphism, Interfaces.
C#.Net Software Development Version 1.0. Overview Inheritance Member Access Constructors Polymorphism (Name Hiding) Multilevel Hierarchy Virtual and VTable.
Modern Programming Tools And Techniques-I
Polymorphism, Abstract Classes & Interfaces
Inheritance and Polymorphism
One class is an extension of another.
Methods Attributes Method Modifiers ‘static’
Inheritance AKEEL AHMED.
Inheritance & Polymorphism
5.1 Being Objects and A Glimpse into Coding
Implementing Polymorphism
Lecture 23 Polymorphism Richard Gesick.
One class is an extension of another.
Abstract Classes AKEEL AHMED.
Polymorphism, Abstract Classes & Interfaces
Fundaments of Game Design
CIS 199 Final Review.
Based on Murach (ch 14) and Deitel 2012
Binding 10: Binding Programming C# © 2003 DevelopMentor, Inc.
Inheritance Lakshmish Ramaswamy.
Presentation transcript:

Neal Stublen

What’s an indexer?  An array has an indexer… int[] myArray = new int[5]; for (int index = 0; index < 5; ++index) { myArray[index] = 3; }  The [ ] is the indexer.

What’s an indexer?  An indexer accesses one value by using another value  A special property: public this[ ]  The index_type is often an integer, but can be another data type  Implemented on collection classes  Throws an ArgumentException ArgumentNullException ArgumentOutOfRangeException

Indexer Example class MyThing { } class MyCollection { public void AddThing(MyThing inThing) {... } public MyThing this[int index] { get {... } set {... } }

Start with an Example

What’s a delegate?  A delegate specifes a method signature for an event  A special data type: public delegate DelegateName([parameters_list]);

Delegate Example // The delegate type declaration public delegate void NameChangedEventHandler(object sender, EventArgs e); class NameObject { // The event member public event NameChangedEventHandler NameChanged; public void setName(string inName) { mName = inName; if (NameChanged != null) { NameChanged(this); }

Connecting to an Event class SomeObject { private NameObject mTheName = new NameObject(); public void Init() { mTheName.NameChanged += new NameChangedEventHandler(EventHandler) } private void EventHandler(object sender) { }

What operations?  Unary operators +, -, !, ++, --, true, false public static operator ( )  Binary operators +, -, *, /, %, &, |, ==, !=, >, =, <= public static operator (, )

Operator Example class MyCollection { private List mList; public static MyCollection operator + (MyCollection col, object newObject) { col.mList.Add(newObject); return col; } collection += new object();

Overloading ==  Must also overload Equals()  Must also overload GetHashCode()  Must also overload !=  Must overload relational operators in pairs ( ), ( =), (==, !=)

Practice Exercise  Exercise 13-1, p. 418

What is inheritance?  One class inherits the attributes and behaviors of another class  The base class should be a larger classification of the derived class (ex. a Control is a larger classification for Button) A Button “is-a” Control; a Button “has-a” BackColor A Book “is-a” Product; a Book “has-a” Publisher  The derived class extends or overrides behavior

.NET Inheritance  All classes implicitly inherit from System.Object GetType() ToString() Equals() ReferenceEquals() GetHashCode() Finalize() MemberwiseClone()

How does inheritance work? class Fourthclass Thirdclass Secondclass First methodA methodB methodC methodD methodE Fourth fourth = new Fourth(); fourth.methodA(); fourth.methodB(); fourth.methodC(); fourth.methodD(); fourth.methodE(); First first = new Fourth(); first.methodA(); first.methodB(); first.methodC(); first.methodD(); first.methodE(); “Polymorphism” The base class can take many different forms.

Polymorphism List myList = new List (); myList.Add(new First()); myList.Add(new Second()); myList.Add(new Third()); myList.Add(new Fourth()); foreach (First item in myList) { item.methodB(); item.methodC(); }

How do we “do” inheritance?  Keywords: virtual, override  A base class declares a method as virtual  A derived class declares a method as override  public/private => public/private/protected/internal  Reference base class using “base.”

Inherited Classes class First { public virtual void methodA() { } class Second : First { public override void methodA() { base.methodA(); }

Considerations for Inheritance  Confirm “is-a” versus “has-a” relationship  Does adding one or more properties make sense?  Would an interface be more beneficial?  Implicit and explicit casting between inherited types  Using the “as” operator instead of casting to avoid exceptions

Abstract Classes  Abstract classes cannot be instantiated, and can only serve as a base class  Abstract methods and properties must be overridden in a derived class You know the method or property exists for every object of this type, but there is no implementation at this level of abstraction.

Sealed Classes  Sealed classes cannot be inherited  Sealed methods and properties cannot be overridden

Start thinking about how objects you need to model may inherit from one another. Are there any obvious heirarchies, common attributes, or shared behavior?

Suggestions  Try to work through Exercises 14-1 and 14-2 in the book (p. 457)