Ch 10- Advanced Object-Oriented Programming Features

Slides:



Advertisements
Similar presentations
Microsoft.NET Fundamentals. Introduction Name Company affiliation Title/function Job responsibility Database & Developer experience Your expectations.
Advertisements

C# DownCast vs UpCast.
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.
C# Programming: From Problem Analysis to Program Design1 10 Advanced Object-Oriented Programming Features C# Programming: From Problem Analysis to Program.
Virtual Functions Junaed Sattar November 10, 2008 Lecture 10.
“is a”  Define a new class DerivedClass which extends BaseClass class BaseClass { // class contents } class DerivedClass : BaseClass { // class.
MIT AITI 2002 Abstract Classes, Interfaces. Abstract Classes What is an abstract class? An abstract class is a class in which one or more methods is declared,
Lecture 8 Inheritance Richard Gesick. 2 OBJECTIVES How inheritance promotes software reusability. The concepts of base classes and derived classes. To.
1 Abstract Class There are some situations in which it is useful to define base classes that are never instantiated. Such classes are called abstract classes.
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 - Polymorphism ITI 1121 Nour El Kadri.
Programming in Java CSCI-2220 Object Oriented Programming.
Chapter 9 Questions 1. What are the difference between constructors and member functions? 2. Design and implement a simple class as you want, with constructors.
1 Interfaces and Abstract Classes Chapter Objectives You will be able to: Write Interface definitions and class definitions that implement them.
Inheritance. Inheritance - Introduction Idea behind is to create new classes that are built on existing classes – you reuse the methods and fields and.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Chapter 9 Inheritance and.
Object Oriented Programming
Chapter 8 Class Inheritance and Interfaces F Superclasses and Subclasses  Keywords: super F Overriding methods  The Object Class  Modifiers: protected,
Introduction to Object-Oriented Programming Lesson 2.
Object Oriented Programming in C++ Chapter 7 Dynamic Binding.
OOP in Java : © W. Milner 2005 : Slide 1 Java and OOP Part 3 – Extending classes.
POLYMORPHISM Chapter 6. Chapter Polymorphism  Polymorphism concept  Abstract classes and methods  Method overriding  Concrete sub classes and.
CSC241 Object-Oriented Programming (OOP) Lecture No. 17.
C++ How to Program, 7/e. © by Pearson Education, Inc. All Rights Reserved.2.
C# Programming: From Problem Analysis to Program Design1 Creating Your Own Classes C# Programming: From Problem Analysis to Program Design 4th Edition.
Abstract classes only used as base class from which other classes can be inherit cannot be used to instantiate any objects are incomplete Classes that.
1 More About Derived Classes and Inheritance Chapter 9.
Object-Oriented Programming (part 1 – Data Encapsulation)
Inheritance Modern object-oriented (OO) programming languages provide 3 capabilities: encapsulation inheritance polymorphism which can improve the design,
Modern Programming Tools And Techniques-I
Creating Your Own Classes
Polymorphism.
Chapter 11 Inheritance and Polymorphism
Advanced Object-Oriented Programming Features
Object-Oriented Programming (OOP) Lecture No. 45
Final and Abstract Classes
Inheritance and Polymorphism
Object-Oriented Programming
Polymorphism.
IST311 Advanced Issues in OOP: Inheritance and Polymorphism
IST311 Advanced Issues in OOP: Inheritance and Polymorphism
Inheritance "Question: What is the object oriented way of getting rich? Answer: Inheritance.“ “Inheritance is new code that reuses old code. Polymorphism.
Lecture 23 Polymorphism Richard Gesick.
OOP’S Concepts in C#.Net
Chapter 9 Inheritance and Polymorphism
Lecture 22 Inheritance Richard Gesick.
Week 6 Object-Oriented Programming (2): Polymorphism
Inheritance Virtual Functions, Dynamic Binding, and Polymorphism
Inheritance: Polymorphism and Virtual Functions
Inheritance Inheritance is a fundamental Object Oriented concept
CISC/CMPE320 - Prof. McLeod
Polymorphism CMSC 202, Version 4/02.
Polymorphism Professor Hugh C. Lauer CS-2303, System Programming Concepts (Slides include materials from The C Programming Language, 2nd edition, by Kernighan.
Inheritance: Polymorphism and Virtual Functions
Fundaments of Game Design
Inheritance Virtual Functions, Dynamic Binding, and Polymorphism
Today’s Objectives 10-Jul-2006 Announcements Quiz #3
Chapter 14 Abstract Classes and Interfaces
Chapter 8 Inheritance Part 2.
Chapter 11 Inheritance and Polymorphism Part 1
Inheritance: Polymorphism and Virtual Functions
CS410 – Software Engineering Lecture #8: Advanced C++ III
Binding 10: Binding Programming C# © 2003 DevelopMentor, Inc.
C++ Object Oriented 1.
Static Binding Static binding chooses the function in the class of the base class pointer, ignoring any versions in the class of the object actually.
Computer Science II for Majors
Chengyu Sun California State University, Los Angeles
INTERFACES Explained By: Sarbjit Kaur. Lecturer, Department of Computer Application, PGG.C.G., Sector: 42, Chandigarh.
Presentation transcript:

Ch 10- Advanced Object-Oriented Programming Features Una Donnelly

We Will Learn: Using Inheritance Dynamic Link Library (DLL) Abstract, Partial, and Generic Classes Polymorphic Programming

Inheritance Create a general class → specialized classes with access to members of general class General class = Base class Override Method to change it in a certain specialized class: Public override string ToString() {return firstName + “ “ + lastName;} class: Person Public Name, age, etc. Specialized class: Student Private Student ID, year Specialized class: Worker Private Title, Company

Inheritance: Derived Classes public class Person { protected string name = "John Smith"; public virtual void GetInfo() { Console.WriteLine("Name: {0}", name); Console.WriteLine("SSN: {0}", ssn); } } class Employee : Person { public string id = "ABC567EFG"; public override void GetInfo() { // Calling the base class GetInfo method: base.GetInfo(); Console.WriteLine("Employee ID: {0}", id); } } Base class is identified Can be referenced in a method or arguments

DLL Any application can reference a class if it is stored in DLL Select the Class Library Template To use DLL in an application add using statement to top of code: using <Namespace of MyClass>;

Abstract Classes Add abstract modifier to stop other classes from instantiating base class objects May specify common methods w/out defining implementation Name: public abstract class A Cannot be used; Only derived from

Compiled to Single Class Partial Classes A class split into two files For example: Windows Forms Useful for sharing work All files must use partial keyword All modifier (public/private) must match Developer 1: Class1.cs Compile Compiled to Single Class Developer 2: Class2.cs

Generic Classes Generics provide the solution to a limitation in earlier versions of the common language runtime and the C# language in which generalization is accomplished by casting types to and from the universal base type Object. By creating a generic class, you can create a collection that is type-safe at compile-time. Encapsulate operations that are not specific to a particular data type No need to alter code for each data type Identify where method implementations will change and put in placeholder Prototype that gives the function but not how it will be implemented Starting with an existing concrete class, and changing types into type parameters one at a time

Generic Classes: Sample Code class BaseNode { } class BaseNodeGeneric<T> { } // concrete type class NodeConcrete<T> : BaseNode { } //closed constructed type class NodeClosed<T> : BaseNodeGeneric<int> { } //open constructed type class NodeOpen<T> : BaseNodeGeneric<T> { } Generic classes that inherit from open constructed types must supply type arguments for any base class type parameters that are not shared by the inheriting class: Class BaseNodeMultiple<T, U> { } //No error class Node4<T> : BaseNodeMultiple<T, int> { } //No error class Node5<T, U> : BaseNodeMultiple<T, U> { } //Generates an error //class Node6<T> : BaseNodeMultiple<T, U> {} ← Closed-Parameter Specified ← Open-Parameter not Specified

Polymorphism Polymorphism means "many-shaped" and it has two distinct aspects Objects of a derived class may be treated as objects of a base class at runtime e.g. method parameters Derived classes can override the virtual methods of the base class - thus at runtime you can call the method on the base class and invoke the method of the derived class Virtual methods enable you to work with groups of related objects in a uniform way

Polymorphism: Sample Code public class Shape { // A few example members public int X { get; private set; } public int Y { get; private set; } public int Height { get; set; } public int Width { get; set; } // Virtual method public virtual void Draw() { Console.WriteLine("Performing base class drawing tasks"); } } class Circle : Shape { public override void Draw() { // Code to draw a circle... Console.WriteLine("Drawing a circle"); base.Draw(); } } class Rectangle : Shape { public override void Draw() { // Code to draw a rectangle... Console.WriteLine("Drawing a rectangle"); base.Draw(); } } class Triangle : Shape { public override void Draw() { // Code to draw a triangle... Console.WriteLine("Drawing a triangle"); base.Draw(); } } class Program { static void Main(string[] args) { // Polymorphism part #1: Rectangle, Triangle and Circle // can all be used where ever a Shape is expected. No cast // required because an implicit conversion exists from a // derived class to its base class. System.Collections.Generic.List<Shape> shapes = new System.Collections.Generic.List<Shape>(); shapes.Add(new Rectangle()); shapes.Add(new Triangle()); shapes.Add(new Circle()); // Polymorphism part #2: the virtual method Draw is invoked // on each of the derived classes, not the base class. foreach (Shape s in shapes) { s.Draw(); } // Keep the console open in debug mode. Console.WriteLine("Press any key to exit."); Console.ReadKey(); } } /* Output: Drawing a rectangle Performing base class drawing tasks Drawing a triangle Performing base class drawing tasks Drawing a circle Performing base class drawing tasks */