Inheritance & Polymorphism

Slides:



Advertisements
Similar presentations
Containers CMPS Reusable containers Simple data structures in almost all nontrivial programs Examples: vectors, linked lists, stacks, queues, binary.
Advertisements

Inheritance. Many objects have a hierarchical relationship –Examples: zoo, car/vehicle, card game, airline reservation system Inheritance allows software.
Language Fundamentals in brief C# - Introduction.
March Ron McFadyen1 Composite Used to compose objects into tree structures to represent part-whole hierarchies Composite lets clients treat.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter N - 1 Chapter 13 Polymorphism is-a relationships Interfaces.
Arrays. A group of data with same type stored under one variable. It is assumed that elements in that group are ordered in series. In C# language arrays.
Aalborg Media Lab 23-Jun-15 Inheritance Lecture 10 Chapter 8.
Inheritance and Polymorphism Recitation 04/10/2009 CS 180 Department of Computer Science, Purdue 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.
Differences between C# and C++ Dr. Catherine Stringfellow Dr. Stewart Carpenter.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Taken from slides of Starting Out with C++ Early Objects Seventh Edition.
Chapter 15 – Inheritance, Virtual Functions, and Polymorphism
“is a”  Define a new class DerivedClass which extends BaseClass class BaseClass { // class contents } class DerivedClass : BaseClass { // class.
Class & Object 蔡柏灃.
FEN 2012 UCN Technology: Computer Science1 C# - Introduction Language Fundamentals in Brief.
CIS3023: Programming Fundamentals for CIS Majors II Summer 2010 Viswanathan Inheritance and Polymorphism Course Lecture Slides 2 nd June 2010 “ We are.
Object Oriented Programming: Java Edition By: Samuel Robinson.
Inheritance and Polymorphism Daniel Liang, Introduction to Java Programming.
ILM Proprietary and Confidential -
CIS 3301 C# Lesson 7 Introduction to Classes. CIS 3302 Objectives Implement Constructors. Know the difference between instance and static members. Understand.
C# F 1 CSC 298 Object Oriented Programming (Part 1)
These materials where developed by Martin Schray. Please feel free to use and modify them for non-commercial purposes. If you find them useful or would.
15440 Distributed Systems Recitation 1 Objected-Oriented Java Programming.
Neal Stublen What’s an indexer?  An array has an indexer… int[] myArray = new int[5]; for (int index = 0; index < 5; ++index) {
Inheritance (Part 4) Polymorphism and Abstract Classes 1.
1 Interfaces and Abstract Classes Chapter Objectives You will be able to: Write Interface definitions and class definitions that implement them.
Static Attributes and Inheritance  static attributes behave the same as non-static attributes in inheritance  public and protected static attributes.
Neal Stublen Tonight’s Agenda  Indexers  Delegates and events  Operator overloading  Class inheritance  Q&A.
Generics Generics vs. heterogeneous collections Doing your own generics FEN 2014UCN Teknologi/act2learn1.
COMPUTER PROGRAMMING 2 ArrayLists. Objective/Essential Standard Essential Standard 3.00Apply Advanced Properties of Arrays Essential Indicator 3.02 Apply.
Class Inheritance SWE 344 Internet Protocols & Client Server 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.
Inheritance ndex.html ndex.htmland “Java.
Chapter  Array-like data structures  ArrayList  Queue  Stack  Hashtable  SortedList  Offer programming convenience for specific access.
Exposure Java 2011 APCS Edition
Inheritance Modern object-oriented (OO) programming languages provide 3 capabilities: encapsulation inheritance polymorphism which can improve the design,
CMSC 202 ArrayList Aug 9, 2007.
Introduction to .NET Florin Olariu
Chapter 11 Inheritance and Polymorphism
Ch 10- Advanced Object-Oriented Programming Features
Inheritance and Polymorphism
COP 3503 FALL 2012 Shayan Javed Lecture 8
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.
Interfaces and Inheritance
CS360 Windows Programming
.NET and .NET Core 5.2 Type Operations Pan Wuming 2016.
OOP’S Concepts in C#.Net
CS Week 13 Jim Williams, PhD.
The University of Texas Rio Grande Valley
© A+ Computer Science - Arrays and Lists © A+ Computer Science -
CMSC 202 ArrayList Aug 9, 2007.
Data Structures and Database Applications Arrays And Lists
CMSC 202 ArrayList Aug 9, 2007.
Java Inheritance.
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.
1D Arrays and Lots of Brackets
Chapter 9 Carrano Chapter 10 Small Java
CECS 220: OOP design with java
Inheritance in Java CS 3331 Fall 2009.
1.4 ทบทวน JAVA OO Programming Concepts Declaring and Creating Objects
CIS 199 Final Review.
Creating and Modifying Text part 3
Final Exam Review Inheritance Template Functions and Classes
Ps Module 7 – Part II 2D Arrays and LISTS 5/26/2019 CSE 1321 Module 7.
Chapter 11 Inheritance and Encapsulation and Polymorphism
四時讀書樂 (春) ~ 翁森 山光照檻水繞廊,舞雩歸詠春風香。 好鳥枝頭亦朋友,落花水面皆文章。 蹉跎莫遣韶光老,人生唯有讀書好。
Ps Module 7 – Part II 2D Arrays and LISTS 8/29/2019 CSE 1321 Module 7.
Chengyu Sun California State University, Los Angeles
Interfaces, Enumerations, Boxing, and Unboxing
Lecture Set 9 Arrays, Collections, and Repetition
Presentation transcript:

Inheritance & Polymorphism

Specialization & Generalization The is-a relationship The dog is a mammal The cat is a mammal Dog and cat specialize mammal Mammal generalize dog and cat To extract the common parts In C#, the specialization relationship is typically implemented using inheritance.

Example Inheritance_demo Use : for inheritance Access modifiers Private, protected, public Override inherited methods new vs virtual-override Overload vs overwrite Observe the which method is called

Exercise 13 Please develop a Dog and a Cat class derives from mammal class and specialize them respectively The mammal class is given as the next slide

class Mammal { protected string speciesName = "Mammal"; public Mammal(string speciesName) this.speciesName = speciesName; } public void Sound() Console.WriteLine("The sound of {0} => hmm...hmm...", speciesName);

The root of all classes - Object Every thing is an object of Object in C# !!

Example (5-4) bad naming

Exercise 14 What will the message be if you don’t override the toString() in example 5-4? To understand why and when we need to do this.

Polymorphism The way you look at things Dog aDog = new Dog(); A dog is just a dog A dog is a mammal That dog is a hound Dog aDog = new Dog(); Mammal aMammal = (Mammal) aDog; ex13

Boxing & Un-Boxing The conversion between value types and reference types stack vs heap Boxing is implicit Un-Boxing is explicit

Boxing & Un-Boxing (5-5) implicit explicit

Interfaces An interface is a contract that guarantees to a client how a class will behave Doing nothing but putting a constraint on the implementing class Ex:

Example 8-1 Constructor Methods Property

Arrays An array is an indexed collection of objects, all of the same type Phone book (exercise 10) myFriend1, myFriend2, … , myFriend100 ? Expression :

Example (ArrayDemo) The phone book exercise can be improved further Array size static void Main(string[] args) { PersonalData[] myFriends = new PersonalData[2]; myFriends[0] = new PersonalData(); myFriends[0].Name = "Mandy Tsai"; myFriends[0].Phone = "0912345678"; myFriends[0].display(); PersonalData.Counter++; myFriends[1] = new PersonalData("Claire","0913245678"); myFriends[1].display(); } Array indexer

Exercise 15 Please try to declare and initialize an integer array from 0 to 100 myArray[0] = 0; myArray[1] = 1; … myArray[100] = 100; and prints them out 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 … 19

Some useful stuff Length property System.Array.Sort() myArray.Lngth System.Array.Sort() System.Array.Sort(myArray,0,101); System.Array.Reverse() System.Array.Reverse(myArray,0,101); The foreach loop foreach(int i in myArray)

Multi-dimensional array & ArrayList You can cast a multi-dimensional array simply by int[,] myArray = new int[6,8]; ArrayList is used to provide you an array with dynamic size Using System.Collections; Add(), Insert() Remove(), RemoveAt() Count (property)

Example (ex15_modified_2) ArrayList myArray = new ArrayList(); for(int i = 0; i < 101; i++) myArray.Add(i); { if(i != 0 && i%10 == 0) Console.WriteLine(); Console.Write(myArray[i]+"\t"); } Console.WriteLine("\n\n"); It is not necessary to provide the size of your array Add values into our array