Neal Stublen Tonight’s Agenda  Indexers  Delegates and events  Operator overloading  Class inheritance  Q&A.

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.
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.
CSE 1302 Lecture 8 Inheritance Richard Gesick Figures from Deitel, “Visual C#”, Pearson.
Inheritance Inheritance Reserved word protected Reserved word super
Object-Oriented Application Development Using VB.NET 1 Chapter 8 Understanding Inheritance and Interfaces.
Session 07: C# OOP 4 Review of: Inheritance and Polymorphism. Static and dynamic type of an object. Abstract Classes. Interfaces. FEN AK - IT:
© 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.
Inheritance and Class Hierarchies Chapter 3. Chapter 3: Inheritance and Class Hierarchies2 Chapter Objectives To understand inheritance and how it facilitates.
Fall 2007CS 2251 Inheritance and Class Hierarchies Chapter 3.
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.
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.
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.
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.
What is inheritance? It is the ability to create a new class from an existing class.
Chris Kiekintveld CS 2401 (Fall 2010) Elementary Data Structures and Algorithms Inheritance and Polymorphism.
Neal Stublen What’s an indexer?  An array has an indexer… int[] myArray = new int[5]; for (int index = 0; index < 5; ++index) {
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.
Inheritance and Access Control CS 162 (Summer 2009)
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.
1 COSC2007 Data Structures II Chapter 9 Class Relationships.
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,
Introduction to Object-Oriented Programming Lesson 2.
Coming up: Inheritance
1 Inheritance Reserved word protected Reserved word super Overriding methods Class Hierarchies Reading for this lecture: L&L 9.1 – 9.4.
Interfaces F What is an Interface? F Creating an Interface F Implementing an Interface F What is Marker Interface?
Inheritance and Class Hierarchies Chapter 3. Chapter 3: Inheritance and Class Hierarchies2 Chapter Objectives To understand inheritance and how it facilitates.
© 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.
Inheritance and Class Hierarchies Chapter 3. Chapter Objectives  To understand inheritance and how it facilitates code reuse  To understand how Java.
Chapter 11: Advanced Inheritance Concepts. Objectives Create and use abstract classes Use dynamic method binding Create arrays of subclass objects Use.
Classes, Interfaces and Packages
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.
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
Classes and Inheritance
Inheritance and Polymorphism
Lecture 23 Polymorphism Richard Gesick.
Fundaments of Game Design
Fundaments of Game Design
CIS 199 Final Review.
Chapter 11 Class Inheritance
Inheritance Lakshmish Ramaswamy.
Presentation transcript:

Neal Stublen

Tonight’s Agenda  Indexers  Delegates and events  Operator overloading  Class inheritance  Q&A

Review  What methods can you use to get data into and out of a form? Form.Tag property Add methods and properties to the form

Review  What issues might make you think twice about using the Form.Tag property? It’s a generic Object type, so there are no compiler checks on the data type

Review  Using the Debug class to trace program execution Stepping through class construction ○ Multiple constructors Stepping through properties ○ get and set Stepping through form construction and initialization ○ Construction and initialization

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

What’s an indexer?  Indexers can create classes that act like “virtual arrays” They have the array syntax, but they are not actually allocated as arrays  A special class property: public this[ ]  The index_type is often an integer, but can be another data type

Invalid Indexes  Using an invalid index value throws an ArgumentException ArgumentNullException ArgumentOutOfRangeException

Files as Byte Arrays class FileArray { public byte this[long index] { get { // read byte at index } set { // write byte value at index }

Books as Page Arrays class Book { public Page this[long index] { get { // return page “index” } set { // insert page at “index” }

What’s a delegate?  A delegate specifes a method signature for an event  Declare a delegate 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, new EventArgs()); } } }

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

An Event Example  Reuse the SportsTeam class  One form updates a team’s record  Another form reports the team’s record

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  Reference base class from a derived class using “base.”

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

Public vs. Private  “public” indicates a method or property can be accessed from outside a class  “private” indicates a method or property can be accessed only from within a class  “protected” indicates a method or property can be accessed from within a class or a derived class

Public/Protected/Private class MyClass { public void MyPublicMethod() { } private void MyPrivateMethod() { } protected void MyProtectedMethod() { } } class YourClass { public void Sample() { MyClass test; // Access from anywhere test.MyPublicMethod(); } }

Public/Protected/Private class MyClass { public void MyPublicMethod() { } private void MyPrivateMethod() { } protected void MyProtectedMethod() { } } class MyDerivedClass : MyClass { public void Sample() { // Access from a derived class MyProtectedMethod(); }

Public/Protected/Private class MyClass { public void MyPublicMethod() { } private void MyPrivateMethod() { } protected void MyProtectedMethod() { // Access from within the class MyPrivateMethod(); }

What about “internal”?  The internal keyword provides access to methods and properties, but only from within other files in the same.NET assembly

Casting Operations  A derived class type can be implicitly cast to any of its base class types  Base class types must be explicitly cast to a derived class type

Implicit Casting class Base { } class Derived : Base { } int DoSomething(Base inObject) { } Derived derivedObject = new Derived(); // Implicitly cast to Base class type DoSomething(derivedObject);

Explicit Casting class Base { } class Derived : Base { } int DoSomething(Derived inObject) { } Base someObject = new Derived(); // Explicit cast to Derived class type DoSomething((Derived)someObject); // May throw an exception

Explicit Casting class Base { } class Derived : Base { } int DoSomething(Derived inObject) { } Base someObject = new Derived(); // Explicit cast to Derived class type DoSomething(someObject as Derived); // May return null

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

Inheritance Example  Example product heirarchy p. 429

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 All Objects have a ToString() implementation, but the implementations are all independent of one another

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)

Inheritance Walkthrough