Session 07: C# OOP 4 Review of: Inheritance and Polymorphism. Static and dynamic type of an object. Abstract Classes. Interfaces. FEN 2013-04-011AK - IT:

Slides:



Advertisements
Similar presentations
OO Programming in Java Objectives for today: Overriding the toString() method Polymorphism & Dynamic Binding Interfaces Packages and Class Path.
Advertisements

1 l Inheritance Basics l Programming with Inheritance l Dynamic Binding and Polymorphism Inheritance.
1 OOP in C#:Object Interaction. Inheritance and Polymorphism. Session 2: OOP in C#
Inheritance Inheritance Reserved word protected Reserved word super
Chapter 12: Support for Object-Oriented Programming
Inheritance Java permits you to use your user defined classes to create programs using inheritance.
Advanced Programming in Java
Sadegh Aliakbary Sharif University of Technology Fall 2010.
ACM/JETT Workshop - August 4-5, :Inheritance and Interfaces.
Encapsulation, Inheritance & Interfaces CSE 115 Spring 2006 February 27, March 1 & 3, 2006.
1 Chapter 7 l Inheritance Basics l Programming with Inheritance l Dynamic Binding and Polymorphism Inheritance.
1 Evan Korth New York University Inheritance and Polymorphism Professor Evan Korth New York University.
OOP in Java Fawzi Emad Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
Chapter 7 - Generalization/Specialization and Inheritance1 Chapter 7 Generalization/Specialization and Inheritance.
Inheritance using Java
Programming Languages and Paradigms Object-Oriented Programming.
C++ Object Oriented 1. Class and Object The main purpose of C++ programming is to add object orientation to the C programming language and classes are.
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.
OOPs Object oriented programming. Based on ADT principles  Representation of type and operations in a single unit  Available for other units to create.
Lecture 8 Inheritance Richard Gesick. 2 OBJECTIVES How inheritance promotes software reusability. The concepts of base classes and derived classes. To.
CISC6795: Spring Object-Oriented Programming: Polymorphism.
Inheritance and Class Hierarchies Ellen Walker CPSC 201 Data Structures Hiram College.
CS 403 – Programming Languages Class 25 November 28, 2000.
1 Java Inheritance. 2 Inheritance On the surface, inheritance is a code re-use issue. –we can extend code that is already written in a manageable manner.
CSE 501N Fall ‘09 14: Inheritance 20 October 2009 Nick Leidenfrost.
 2002 Prentice Hall. All rights reserved. 1 Introduction to Inheritance Inheritance: –1 of 3 main features of OOP –Form of software reusability –(Derived)
Object Orientation Yaodong Bi, Ph.D. Department of Computer Sciences University of Scranton October 18, 2015October 18, 2015October 18, 2015.
Chris Kiekintveld CS 2401 (Fall 2010) Elementary Data Structures and Algorithms Inheritance and Polymorphism.
Chapter 3 Inheritance and Polymorphism Goals: 1.Superclasses and subclasses 2.Inheritance Hierarchy 3.Polymorphism 4.Type Compatibility 5.Abstract Classes.
Programming in Java CSCI-2220 Object Oriented Programming.
Session 2: OOP in C# OOP in C#: –Object Interaction. –Inheritance and Polymorphism. Autumn 20121UCN Technology: IT/Computer Science.
Object Oriented Software Development
Session 02 and 03: C# OOP 1 OOP in C#: Classes and Objects in C#. Object-Oriented Design. UML Class Diagram. Object Interaction. FEN AK IT:
8. Inheritance “Is-a” Relationship. Topics Creating Subclasses Overriding Methods Class Hierarchies Abstract Class Inheritance and GUIs The Timer Class.
Lecture 10 Concepts of Programming Languages Arne Kutzner Hanyang University / Seoul Korea.
Inheritance. Inheritance - Introduction Idea behind is to create new classes that are built on existing classes – you reuse the methods and fields and.
Object-Oriented Programming Chapter Chapter
OOPs Object oriented programming. Abstract data types  Representationof type and operations in a single unit  Available for other units to create variables.
Object Oriented Programming
ISBN Object-Oriented Programming Chapter Chapter
1 Inheritance Reserved word protected Reserved word super Overriding methods Class Hierarchies Reading for this lecture: L&L 9.1 – 9.4.
COM S 228 Introduction to Data Structures Instructor: Ying Cai Department of Computer Science Iowa State University Office: Atanasoff.
Inheritance and Class Hierarchies Chapter 3. Chapter 3: Inheritance and Class Hierarchies2 Chapter Objectives To understand inheritance and how it facilitates.
Session 06: C# OOP-3 Inheritance and Polymorphism. Static and dynamic type of an object. FEN AK - IT: Softwarekonstruktion.
FEN 2014UCN Teknologi/act2learn1 Object-Oriented Programming “ The Three Pillars of OOP”: Encapsulation Inheritance Polymorphism The Substitution Principle.
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)
Class Relationships Lecture Oo08 Polymorphism. References n Booch, et al, The Unified Modeling Language User Guide, Chapt 10 p.125 n Fowler & Scott, UML.
OOP in C# - part 1 OOP in C#: –Object Interaction. –Inheritance and Polymorphism (next module). FEN 20121UCN Technology: Computer Science.
C++ General Characteristics: - Mixed typing system - Constructors and destructors - Elaborate access controls to class entities.
COMPUTER SCIENCE & TECHNOLOGY DEGREE PROGRAMME FACULTY OF SCIENCE & TECHNOLOGY UNIVERSITY OF UVA WELLASSA ‏ Properties of Object Oriented Programming.
ISBN Chapter 12 Support for Object-Oriented Programming.
Geoff Holmes and Bernhard Pfahringer COMP206-08S General Programming 2.
Inheritance a subclass extends the functionality of a superclass a subclass inherits all the functionality of a superclass don't reinvent the wheel – "stand.
Java Programming: Guided Learning with Early Objects Chapter 9 Inheritance and Polymorphism.
Inheritance Modern object-oriented (OO) programming languages provide 3 capabilities: encapsulation inheritance polymorphism which can improve the design,
Modern Programming Tools And Techniques-I
Advanced Programming in Java
Advanced Programming in Java
Inheritance and Polymorphism
Java Programming Language
Inheritance, Polymorphism, and Interfaces. Oh My
Advanced Programming in Java
Java Inheritance.
Advanced Programming in Java
Fundaments of Game Design
Lecture 10 Concepts of Programming Languages
Jim Fawcett CSE687 – Object Oriented Design Spring 2014
Computer Science II for Majors
Presentation transcript:

Session 07: C# OOP 4 Review of: Inheritance and Polymorphism. Static and dynamic type of an object. Abstract Classes. Interfaces. FEN AK - IT: Softwarekonstruktion

FEN AK - IT: Softwarekonstruktion2 Object-Oriented Programming “ The Three Pillars of OOP”: Encapsulation Inheritance Polymorphism The Substitution Principle

Recall: Quality Factors FEN AK - IT: Softwarekonstruktion3 The most important ones: –Reliability: –Correctness –Robustness –Modularity: –Extendibility –Reusability This is addressed through: –Inheritance and polymorphism

FEN AK - IT: Softwarekonstruktion4 Inheritance in C# Every method and property is inherited – constructors are not. private members are inherited, but are not directly accessible in the subclass. Every protected member of the base class is visible in subclasses, but hidden from other parts of the program. Members may be added in the subclass. Multiple inheritance is not supported (by classes). In C# every class inherits from Object.

FEN AK - IT: Softwarekonstruktion5 OO-Principles -inheritance Supports code-reuse. Existing classes may be extended through inheritance. Inheritance is to used as type specialisation, that is: we are modelling an “is-a” relationship between super- and subclass. For instance: CheckAccount is an Account, a special kind of account, but an account. So when we want to add specialised functionality to our system, inheritance may used by adding specialised classes. E.g.: CheckAccount may inherit BankAccount.

FEN AK - IT: Softwarekonstruktion6 Inheritance - redefining methods A method inherited from the base-class may be redefined (“overridden”) in the sub-class. For instance the Withdraw-method on Account/CheckAccout. In C# the must be specified “virtual” in the base-class and “override” in sub-class. The method in the sub-class has the same signature (name and parameter list) and the same return type as the method in the base-class. In the sub-class the redefined method in the base-class may be called using base.methodName();

FEN AK - IT: Softwarekonstruktion7 Inheritance - polymorphism All reference variables in C# may refer to objects of subtypes. In the case of virtual methods it is first at execution time it is determined which method exactly is to be called. The method called is the one defined on the object that the reference currently is referring to. This is called dynamic binding – the call is bound to an actual code segment at runtime (dynamically).

FEN AK - IT: Softwarekonstruktion8 Polymorphism/Dynamic Binding The way it used to be: Employee programmer = new Employee(“H. Acker","Programmer",22222); Static type = Dynamic type Static method call Static type Dynamic type Using polymorphism: Employee boss = new Manager(”Big Boss",”CEO",52525, 500); Dynamic type must be the same or a sub-type of the static type. The compiler checks method-calls on the static type. Runtime the call is bonded to the dynamic type (dynamic binding). Dynamic binding requires methods to be specified virtual in the base-class and explicitly overridden in the sub-classes. Dynamic type Statisc type

FEN AK - IT: Softwarekonstruktion 9 Example Let’s look at the implementation of the model from earlier Now: Employees at the cantina get double bonus. View Source (EmpProjectV2.rar)EmpProjectV2.rar

FEN AK - IT: Softwarekonstruktion10 Exercises – Solutions?..\lektion06\Session06.docx

Abstract Classes In many cases we have an inheritance hierarchy where every subclass must implement the same method, but differently. In this case we would like to declare the signature of the method in the base class, so that the compiler is able to perform static checking of calls to the method. This can be done by declaring the method abstract. When a class has one or more abstract methods, the class it selves must be declared abstract. FEN AK - IT: Softwarekonstruktion11

Class Diagram and Code In Client: AbstractBase a = new Concrete1(); AbstractBase b= new Concrete2(); a.foo(); b.foo(); FEN AK - IT: Softwarekonstruktion12 The dynamic type must be a subtype of the static type The compiler checks that foo() is defined on the static type Dynamic method lookup finds the right implementation of foo()

In C#: The definition of an abstract class FEN AK - IT: Softwarekonstruktion13 The implementation

In C#: In the client: FEN AK - IT: Softwarekonstruktion14 Cannot instantiate an abstract class.

Exercise Exercise 1 on Session07.docx.Session07.docx FEN AK - IT: Softwarekonstruktion15

FEN AK - IT: Softwarekonstruktion16 Inheritance - abstract classes A class that defines one or more methods that are not implemented is called abstract. And the non-implemented methods are specified abstract. An abstract class can not be instantiated. It can only serve as base-class. An abstract class can only be used as static type, not dynamic type for object. Abstract methods must be implemented in the sub- classes. Otherwise the subclass itself becomes abstract. So an abstract method defines or specifies functionality (but does not implement) what must be implemented in the subclasses. Constructors in an abstract class are only used by the constructors in the subclasses

FEN AK - IT: Softwarekonstruktion17 Inheritance - design considerations If we need a class to hold a list of employees, and it should be possible to add employees at the end of list, but nowhere else. Should we inherit Array or List or…? We are not to inherit at all!!! But use delegation. Inheritance is not to be used senselessly trying to reuse code! Inheritance is to be seen as sub typing! Inheritance models “is-a” relationships. Code-reuse is obtainable by using existing classes (composition, delegation etc.)

Interfaces An interface may be seen as a purely abstract class. –Interface: only method signatures, no implementation! (All methods are abstract) An interface represents a design. Example: –Using interfaces in the Dome example: –(In C# interfaces are normally named “ISomething” – here IItem) FEN AK - IT: Softwarekonstruktion

Interfaces The Item must implement the interface (inherit it): FEN AK - IT: Softwarekonstruktion

Interfaces The interface is now used as static type: FEN AK - IT: Softwarekonstruktion

Interfaces We can also create an interface to the DomeDatabase class: FEN AK - IT: Softwarekonstruktion Using the IItem interface as static type

Interfaces DomeDatabase class must implement the interface: FEN AK - IT: Softwarekonstruktion

Interfaces In the main program: FEN AK - IT: Softwarekonstruktion We only need to know the interface as static type

Why use interfaces? Formalise system design before implementation –especially useful with large systems. Contract-based programming –the interface represents the contract between client and object. Low coupling! –decouples specification and implementation. –facilitates reusable client code. –client code will work with both existing and future objects as long as the interface is not changed. Multiple inheritance –A class may implement several interfaces, but only inherit one class. (No competing implementations). FEN AK - IT: Softwarekonstruktion

FEN AK - IT: Softwarekonstruktion25 Exercises Exercise 2 and 3 on Session07.docx.Session07.docx