Applied object oriented programming. 4 th lecture

Similar documents
Object-Oriented Programming in C# (VS 2015)

Object-Oriented Programming in C# (VS 2012)

Object Oriented Programming in Java. Jaanus Pöial, PhD Tallinn, Estonia

9 A Preview of.net 2.0

PROGRAMMING IN VISUAL BASIC WITH MICROSOFT VISUAL STUDIO Course: 10550A; Duration: 5 Days; Instructor-led

And Even More and More C++ Fundamentals of Computer Science

MARKING KEY The University of British Columbia MARKING KEY Computer Science 260 Midterm #2 Examination 12:30 noon, Thursday, March 15, 2012

SERIOUS ABOUT SOFTWARE. Qt Core features. Timo Strömmer, May 26,

Programming in Visual Basic with Microsoft Visual Studio 2010

Framework Fundamentals

Course Hours

Objects and Iterators

CHAPTER 1: INTRODUCING C# 3

COPYRIGHTED MATERIAL. Contents. Part I: C# Fundamentals 1. Chapter 1: The.NET Framework 3. Chapter 2: Getting Started with Visual Studio

G52CPP C++ Programming Lecture 13

C# Generics. Object Oriented Programming (236703) Winter

Object-Oriented Programming

CS/ENGRD 2110 SPRING Lecture 7: Interfaces and Abstract Classes

MCSA Universal Windows Platform. A Success Guide to Prepare- Programming in C# edusum.com

Object Oriented Programming. Java-Lecture 11 Polymorphism

Object Oriented Programming Part II of II. Steve Ryder Session 8352 JSR Systems (JSR)

CMSC 132: Object-Oriented Programming II

What is Inheritance?

Self-review Questions

Chapter 5 Object-Oriented Programming

Programming II (CS300)

CS111: PROGRAMMING LANGUAGE II

29. Method Combination

CS/ENGRD 2110 FALL Lecture 7: Interfaces and Abstract Classes

Introduce C# as Object Oriented programming language. Explain, tokens,

Object oriented programming. Encapsulation. Polymorphism. Inheritance OOP

INHERITANCE. Spring 2019

Starting Savitch Chapter 10. A class is a data type whose variables are objects. Some pre-defined classes in C++ include int,

CPSC Tutorial 4

Introduction to Programming Using Java (98-388)

Object-Oriented Programming

C++ Alexander Baltatzis

Instantiation of Template class

Uka Tarsadia University MCA ( 3rd Semester)/M.Sc.(CA) (1st Semester) Course : / Visual Programming Question Bank

Inheritance. Lecture 11 COP 3252 Summer May 25, 2017

DOT NET SYLLABUS FOR 6 MONTHS

Pointers. A pointer value is the address of the first byte of the pointed object in the memory. A pointer does not know how many bytes it points to.

Wentworth Institute of Technology COMP201 Computer Science II Spring 2015 Derbinsky. C++ Kitchen Sink. Lecture 14.

CS/ENGRD 2110 SPRING 2018

Chapter 15: How to Work with Interfaces and Generics

Lecture Notes on Programming Languages

Inheritance and Interfaces

CPSC 481 Tutorial 4. Intro to Visual Studio and C#

Assoc. Prof. Marenglen Biba. (C) 2010 Pearson Education, Inc. All rights reserved.

OpenACC Course. Office Hour #2 Q&A

CS-202 Introduction to Object Oriented Programming

Written by John Bell for CS 342, Spring 2018

Fast Track to Core Java 8 Programming for OO Developers (TT2101-J8) Day(s): 3. Course Code: GK1965. Overview

THE NAME OF THE CONSTRUCTOR AND DESTRUCTOR(HAVING (~) BEFORE ITS NAME) FUNCTION MUST BE SAME AS THE NAME OF THE CLASS IN WHICH THEY ARE DECLARED.

COP 3330 Final Exam Review

See the CS 2704 notes on C++ Class Basics for more details and examples. Data Structures & OO Development I

Lesson 10A OOP Fundamentals. By John B. Owen All rights reserved 2011, revised 2014

Programming Languages and Techniques (CIS120)

02 Features of C#, Part 1. Jerry Nixon Microsoft Developer Evangelist Daren May President & Co-founder, Crank211

This wouldn t work without the previous declaration of X. This wouldn t work without the previous declaration of y

Outline. Java Models for variables Types and type checking, type safety Interpretation vs. compilation. Reasoning about code. CSCI 2600 Spring

Practice for Chapter 11

Learning C# 3.0. Jesse Liberty and Brian MacDonald O'REILLY. Beijing Cambridge Farnham Köln Sebastopol Taipei Tokyo

Osp::Base::Collection

D Programming Language

Classes and Objects. Class scope: - private members are only accessible by the class methods.

CSE 401/M501 Compilers

CS 251 Intermediate Programming Inheritance

Advanced Object-Oriented Programming. 11 Features. C# Programming: From Problem Analysis to Program Design. 4th Edition

Chapter 11 Inheritance and Polymorphism. Motivations. Suppose you will define classes to model circles,

Programming Languages and Techniques (CIS120)

Intro to OOP Visibility/protection levels and constructors Friend, convert constructor, destructor Operator overloading a<=b a.

Introduction to Visual Basic and Visual C++ Introduction to Java. JDK Editions. Overview. Lesson 13. Overview

C++ (Non for C Programmer) (BT307) 40 Hours

ITI Introduction to Computing II

Graphical Interface and Application (I3305) Semester: 1 Academic Year: 2017/2018 Dr Antoun Yaacoub

Simplest version of DayOfYear

Object-Oriented Principles and Practice / C++

Overview. ITI Introduction to Computing II. Interface 1. Problem 1. Problem 1: Array sorting. Problem 1: Array sorting. Problem 1: Array sorting

Lecture 4: Extending Classes. Concept

Objects Managing a Resource

Microsoft Visual C# Step by Step. John Sharp

PROGRAMMING LANGUAGE 2

CLASSES AND OBJECTS IN JAVA

Real-Time and Concurrent Programming Lecture 4 (F4): Monitors: synchronized, wait and notify

Microsoft Visual Basic 2005: Reloaded

Inheritance and Polymorphism

Argument Passing All primitive data types (int etc.) are passed by value and all reference types (arrays, strings, objects) are used through refs.

1B1b Inheritance. Inheritance. Agenda. Subclass and Superclass. Superclass. Generalisation & Specialisation. Shapes and Squares. 1B1b Lecture Slides

Core Java - SCJP. Q2Technologies, Rajajinagar. Course content

Logistics. Final Exam on Friday at 3pm in CHEM 102

Java 8 Programming for OO Experienced Developers

Data Structures (list, dictionary, tuples, sets, strings)

Study Guide to Exam 2

Informatik II. Tutorial 6. Mihai Bâce Mihai Bâce. April 5,

CGS 2405 Advanced Programming with C++ Course Justification

CS 251 Intermediate Programming Methods and Classes

CS 251 Intermediate Programming Methods and More

C# s A Doddle. Steve Love. ACCU April 2013

CPS 506 Comparative Programming Languages. Programming Language

Transcription:

Applied object oriented programming 4 th lecture

Today Constructors in depth Class inheritance Interfaces Standard.NET interfaces IComparable IComparer IEquatable IEnumerable ICloneable (and cloning) Kahoot

Constructors Constructor it is a method that is being called first when an instance of a class or struct being created. Same for static constructor but only for the first time. What constructors can do: 1. Overload

Constructor What constructors can do: 2. Call base class constructor using keyword : base (look constructor(intro).cs) 3. If there are no explicit constructor defined the default constructor is being created implicitly: There are no parameters. Field values are initialized to default value. Causes problems when changes are needed.

Constructor What constructors can do: 4. Call same class different constructors using: this Look constructor(good).cs constructor(bad).cs

Constructor What constructors cant do: 5. Can not call multiple other constructors. Look constructor(bad2).cs, constructor(good2).cs

Can constructor be non-public?

Constructors What constructors can do: 6. Private/public constructors: 1. Public is standard 2. Private constructors are not allowed to be called from other classes, so if we want to create an instance of such class, there is a special implementation that we have to provide. Look singleton.cs

What is still wrong with this implementation?

Best way to implement singleton

Constructors What constructors can do: 7. Static constructor (Look static.cs) Is called implicitly when: Class instance is created Class static fields or methods are used for the first time Class can have only one static constructor Has to be parameter-less, becase CLR is calling it Can access only static fields/methods of this class Static constructor does not have access modifiers Slow

Initializer Explicit creation of an object by setting all the properties manually. Only the standard constructor is called Example in the next slide

Questions about constructors?

Inheritance C# allowed C# not allowed:

Multiple inheritance Diamond problem: If A has a method, which B and C classes have overridden, but D did not, then which method will D inherit from B or from C? From A method is called successfully, but from D not necessarily. C# solution: interface

Interface Interface exposes a contract, specifying characteristics that a class must implement. Can state required: properties, methods and actions. Interface can not contain any static members Interface can not have implementation of the methods (different from abstract class, because abstract class can have implementation).

Interface Since it is similar to inheritance, sometimes it is being called interface inheritance Class can inherit from ONE base class, and MANY interfaces Look interface.cs TeachingAssistant

SOCRATIVE ROOM NAME: TOP2018

Can you implement in a class two interfaces which has methods with same signatures?

Explicit and implicit interface implementation If class implements an interface explicitly, then to access implemented method you will need a object of interface type, if and interface is implemented explicitly then you can access method with class type object. Explicitly implementing interface requires to write interface method before method name like: void Interface.Method In interface.cs look at TeachingAssistant3 which implements Istudent interface implicitly, and TeachingAssistant4 explicitly.

Explicit and implicit interface Explicit is better, because: implementation When working with interface type there is no coupling with a class that implements it. Loose coupling allow to scale and change system easier. You can have members in your class with same names as in implemented interface: If we have a class with property Name, and we want to implement interface, which has in a contract property Name we can do it by using explicit interface implementation.

Explicit and implicit interface implementation If you are implementing interface implicitly then the methods will be available for class that implements this interface type objects, and for interface type objects. Sometimes this is not a desired functionality. If you are implementing interface explicitly, then access modifier must be private, because your method can only be accessed via interface. When implementing explicitly, we don t have duplicate names problems. In reality 90% of implicit implementation.

Interface delegation If both Student and TeachingAssistant implements IStudent interface, then both have a code,which ensures that contract is fulfilled. Duplication of code can be avoided by using interface delegation. That means that implementation of interface in TeachingAssistant class is being delegated to Student class.

Interface delegation In the delegation process a object of type Student is being created in TeachingAssistant class. When TeachingAssistant object has to perform methods, which are in IStudent interface, then Student object is called to do that. Look interface.cs: TeachingAssistant (bad, because of code duplication), TeachingAssistant2

Indexers in interfaces Differs from indexers in the class: No access modifiers. No implementation. Class can implement multiple interfaces with indexers only if interfaces are being implemented explicitly.

Interface is a TYPE You can specify it as a parameter to a method interface2.cs takespeaker() If you are passing a class object, that implement an interface, then this object is implicitly being casted to a interface type. Return type can be an interface: interface2.cs givespeaker() Casting operators, to check if interface type is implemented: AS: ICat cat = objsomething as ICat; IS: if (possiblecat is ICat)

Interfaces advantages Question: why should we define interface, implement it in a class and then create interface type of object, instead of class type object? Answer:

Generic Interface Interface can be generic (have a type passed as parameter) public class Farmer : IRememberMostRecent<Joke> Class can only implement generic interface, if the class itself is generic. In that case type to an interface is passed when constructing class: public class Dog<T> : ICanEat<T> Look interfacegeneric.cs

Generic Interface where is used to specify constraints of the types new() specifies that new instances can be created

Standard interface implementation Benefit contract implementation.net behaves better with types, that implement: IComparable interface, Array.Sort() method can sort an array of that class members. IEquatable interface, then list.contains() can check, whether an object is really in the list(instead of checking if same pointer is in the list)

IComparable Used for comparing this object to a given object. Has one method: CompareTo (one param., obj) Has both simple and generic version Value Negative Zero Greater than zero Meaning This instance precedes obj in the sort order. This instance occurs in the same position in the sort order as obj. This instance follows obj in the sort order.

IComparable Simple: look IComparable.cs Generics: Errors better seen by compiler

IComparer IComparable<T> says I m comparable. IComparer<T> says I m comparer. Method: compare(two params) Value Less than zero Zero Greater than zero Meaning First object is less than the second. Both object are equal. First object is more than the second.

IComparer Look IComparer.cs

IEquatable Is used for comparing if two objects are equal. Has method Equals. Generic collections: List, Dictionary, Stack, Queue (etc.) has Contains method, which compares objects for equality. If Iequatable interface is implemented then List.Contains check by using our implemented Equals method. Microsoft recommends that every class that has a possibility to be added to a list would implement IEquatable interface.

IEquatable If IEquatable<> would be removed Contains method would not work. Look IEquatable.cs

IEnumerable Allows to iterate (e.g. using foreach) through collection Has simple and generic version:

IEnumerable Has method GetEnumerator, which returns an object, which implements an interface IEnumerator. IEnumerator has: Current property, which returns current object from the list MoveNext method, which moves enumerator one position forward. Reset which moves enumerator to the initial position. Dispose (only generics) inherited from IDisposable. Look IEnumerator.cs

IEnumerable Can be simplified with yield. Look Ienumerable*.cs Must: Return IEnumerable type Be called from iteration loop(e.g foreach)

ICloneable From JAVA lectures: new object copy creationis when object is same type as a type that it is being cloned from and has same state. Possible: Shallow cloning Deep cloning C#: class that implements ICloneable interface must implement Clone method. Returns cloned object (seriously, object type)

ICloneable Deep vs shallow (Look ICloneable.cs)

ICloneable Since Clone method returns object type object, then whoever called Clone method has to take care of casting returned object to required type. Implementation is hidden (deep vs shallow): Microsoft does not recommend to implement Icloneable for exposed APIs, because consumers will not know how your Clone method will behave. More: MSDN ICloneable Interface.

Other popular.net interfaces IQueryable (or IQueryProvider): allows to form queries for datasources, that are queryable. INotifyPropertyChange: is used to display data in WPF, Windows Forms and Silverlight applications. IEqualityComparer (similar to IEquatable) IList and ICollection: for collections IDictionary: for collections, in which you can search using key/value principle. ISerializable allows for an object to control how it is being serialized/deserialized. IFormatter / IFormatProvider used for formatting.

Next time Manipulating with strings Formating values Input validation -> Validating data Working with data collections

Literature for own reading MCSD sertification toolkit: 5 th chapter until Managing object lifecycle MSDN On you own: IEnumerable and IEnumerator How simple and generic version are different?

Questions