Session 02 and 03: C# OOP 1 OOP in C#: Classes and Objects in C#. Object-Oriented Design. UML Class Diagram. Object Interaction. FEN 2013-01-271AK IT:

Slides:



Advertisements
Similar presentations
IMPLEMENTING CLASSES Chapter 3. Black Box  Something that magically does its thing!  You know what it does but not how.  You really don’t care how.
Advertisements

1 OOP in C#:Object Interaction. Inheritance and Polymorphism. Session 2: OOP in C#
Introduction to Object-Oriented Programming CS 21a: Introduction to Computing I First Semester,
Chapter 3 – Implementing Classes. Chapter Goals To become familiar with the process of implementing classes To be able to implement simple methods To.
Chapter 3 Implementing Classes. Instance Variables Instance variables store the data of an object; the fields of an object. Instance of a class: an object.
Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved. Chapter Three - Implementing Classes.
Session 07: C# OOP 4 Review of: Inheritance and Polymorphism. Static and dynamic type of an object. Abstract Classes. Interfaces. FEN AK - IT:
Classes. Object-Oriented Design Method for designing computer programs Consider “objects” interacting in the program –Example: a zoo, a gradebook.
Classes. Object-Oriented Design Method for designing computer programs Consider “objects” interacting in the program –Example: a zoo, a gradebook.
Object Oriented Programming.  OOP Basic Principles  C++ Classes  September 2004  John Edgar 22.
Chapter 3 Implementing Classes. Chapter Goals To become familiar with the process of implementing classes To be able to implement simple methods To understand.
Object Oriented Software Development
Programming Languages and Paradigms Object-Oriented Programming.
CSM-Java Programming-I Spring,2005 Introduction to Objects and Classes Lesson - 1.
Practical Object-Oriented Design with UML 2e Slide 1/1 ©The McGraw-Hill Companies, 2004 PRACTICAL OBJECT-ORIENTED DESIGN WITH UML 2e Chapter 2: Modelling.
Object Orientation An Object oriented approach views systems and programs as a collection of interacting objects. An object is a thing in a computer system.
Writing Classes (Chapter 4)
CSM-Java Programming-I Spring,2005 Objects and Classes Overview Lesson - 1.
Module 7: Object-Oriented Programming in Visual Basic .NET
OBJECT ORIENTED PROGRAMMING CONCEPTS ISC 560. Object-oriented Concepts  Objects – things names with nouns  Classes – classifications (groups) of similar.
Introduction to Object-Oriented Programming
1 OOP in C#: Object Interaction. Inheritance and Polymorphism OOP in C# - Del 1.
Programming in Java Unit 2. Class and variable declaration A class is best thought of as a template from which objects are created. You can create many.
Week 4 Introduction to Computer Science and Object-Oriented Programming COMP 111 George Basham.
An Introduction to Java Chapter 11 Object-Oriented Application Development: Part I.
Object-Oriented Programming in C++
Classes CS 21a: Introduction to Computing I First Semester,
Chapter 7 Objects and Classes 1 Fall 2012 CS2302: Programming Principles.
Java Objects and Classes. Overview n Creating objects that belong to the classes in the standard Java library n Creating your own classes.
P Chapter 2 introduces Object Oriented Programming. p OOP is a relatively new approach to programming which supports the creation of new data types and.
Week 4 Introduction to Computer Science and Object-Oriented Programming COMP 111 George Basham.
BCS 2143 Object Oriented Design Using UML. Objectives Objects Interactions Finding Classes Relationship Between Classes Attribute and Operation Class.
Topic 1 Object Oriented Programming. 1-2 Objectives To review the concepts and terminology of object-oriented programming To discuss some features of.
Session 2: OOP in C# OOP in C#: –Object Interaction. –Inheritance and Polymorphism. Autumn 20121UCN Technology: IT/Computer Science.
1 Announcements Note from admins: Edit.cshrc.solaris instead of.tcshrc Note from admins: Do not use delta.ece.
Session 04: C# OOP 2 OOP in C#: Object-Oriented Design.
CS0007: Introduction to Computer Programming Classes: Documentation, Method Overloading, Scope, Packages, and “Finding the Classes”
CS451 - Lecture 2 1 CS451 Lecture 2: Introduction to Object Orientation Yugi Lee STB #555 (816) * Acknowledgement:
Classes. Constructor A constructor is a special method whose purpose is to construct and initialize objects. Constructor name must be the same as the.
Chapter 4&5 Defining Classes Copyright © 2010 Pearson Addison-Wesley. All rights reserved.
Introduction to Object-Oriented Programming Lesson 2.
FEN 2014UCN Teknologi/act2learn1 Object-Oriented Programming “ The Three Pillars of OOP”: Encapsulation Inheritance Polymorphism The Substitution Principle.
Object Oriented Programming. OOP  The fundamental idea behind object-oriented programming is:  The real world consists of objects. Computer programs.
Chapter 7 Classes and Methods III: Static Methods and Variables Lecture Slides to Accompany An Introduction to Computer Science Using Java (2nd Edition)
YG - CS Concept of Encapsulation What is encapsulation? - data and functions/methods are packaged together in the class normally.
Classes CSCI 201L Jeffrey Miller, Ph.D. HTTP :// WWW - SCF. USC. EDU /~ CSCI 201 USC CSCI 201L.
Author: DoanNX Time: 45’.  OOP concepts  OOP in Java.
OOPS CONCEPT.  OOPS  Benefits of OOPs  OOPs Principles  Class  Object Objectives.
Topics Instance variables, set and get methods Encapsulation
CS 116 OBJECT ORIENTED PROGRAMMING II LECTURE 6 Acknowledgement: Contains materials provided by George Koutsogiannakis and Matt Bauer.
Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved. Chapter Three - Implementing Classes.
OOP in C# - part 1 OOP in C#: –Object Interaction. –Inheritance and Polymorphism (next module). FEN 20121UCN Technology: Computer Science.
Chapter 3 Implementing Classes
COMPUTER SCIENCE & TECHNOLOGY DEGREE PROGRAMME FACULTY OF SCIENCE & TECHNOLOGY UNIVERSITY OF UVA WELLASSA ‏ Properties of Object Oriented Programming.
Computer Programming II Lecture 5. Introduction to Object Oriented Programming (OOP) - There are two common programming methods : procedural programming.
Classes (Part 1) Lecture 3
Classes C++ representation of an object
Java OOP Overview Classes and Objects, Members and Class Definition, Access Modifier, Encapsulation Java OOP Overview SoftUni Team Technical Trainers.
Classes and OOP.
OBJECT ORIENTED PROGRAMMING II LECTURE 7 GEORGE KOUTSOGIANNAKIS
Creating and Using Classes
Chapter Three - Implementing Classes
What is an object? An object represents an individual, identifiable item, unit, or entity, either real or abstract, with a well-defined role in the.
OBJECT ORIENTED PROGRAMMING II LECTURE 8 GEORGE KOUTSOGIANNAKIS
Dr. Bhargavi Dept of CS CHRIST
CS2011 Introduction to Programming I Objects and Classes
JAVA CLASSES.
Introduction to Object-Oriented Programming
Introduction to Computer Science and Object-Oriented Programming
Presentation transcript:

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: Softwarekonstruktion

FEN AK IT: Softwarekonstruktion2 Object-Oriented Programming “ The Three Pillars of OOP”: Encapsulation Inheritance Polymorphism In later sessions

FEN AK IT: Softwarekonstruktion3 OO-Principles -encapsulation Seen from the outside an object is an entity offering a number of services (public methods and properties). The implementation and data representation are hidden or encapsulated. – This makes it possible to change implementation without affecting other parts of the program. – Also it becomes possible to think and talk about and use the object without knowing the implementation. – Hiding data behind methods and properties are called encapsulation or data abstraction. Encapsulation or data abstraction is one the fundamental principles of object-oriented programming.

FEN AK IT: Softwarekonstruktion4 Definition of Object and Class Object – Represents a real-world concept, realised by data (attributtes) associated with the concept and a number of operations (methods)that may be used to access the attributes of the object. Class – A type that defines the attributes and methods of a set of objects that all represent instances of the same real-world concept. The class describes the structure of the concept, and the objects are the actual instances of the class. The class is static – exists only compile time. Objects are dynamic – exist runtime.

FEN AK IT: Softwarekonstruktion5 Attributes (data) Attributes define the data to be stored (name and type). Attributes are defined in the class, and are assign values in the objects. E.g.: – BankAccount: accountNo, balance, maxLimit, interestRate etc. – Employee: name, departmentNo, salery, jobTitle etc. The state of an object is given by the value of its attributes at a given time.

FEN AK IT: Softwarekonstruktion6 Methods (operations) The operations of an object are defined by the methods implemented in the class. Calls to methods either return information about the state of the object (accessors) or change the state of the object (mutators). BankAccount – WithDraw(), Deposite(), GetBalance() etc. Employee – GiveASaleryRaise (), SetTitle() etc.

FEN AK IT: Softwarekonstruktion7 Properties (C# speciality) Are used for getting and setting attribute values. (Replace set- and get-methods in Java and C++). Provide a syntax similar to direct access of the attributes. (Anders Hejsberg footprint?)

FEN AK IT: Softwarekonstruktion8 Constructor Method(s) with the same name as the class and no return type.The job of a constructor is to initialise the attributes of the object during object creation. E.g. Creating an object – BankAccount acc = new BankAccount(); BankAccount() is a call to the constructor. The new command – Allocates memory for the object. – Assigns the variable (the reference) acc to the allocated block of memory – new is actually a function that returns an address in the heap. Constructors may be overloaded

FEN AK IT: Softwarekonstruktion9 The Anatomy of a Class Classes are usually written by this pattern: class ClassName { declaration of attributes constructors properties methods }

FEN AK IT: Softwarekonstruktion10 Methods Metodenavn (parameter list) { statements } public int SumOf2Ints (int int1, int int2) { int sum; sum = int1 + int2; return sum; } Acessmodifier: public/protected /private Local variable return Parameters

FEN AK IT: Softwarekonstruktion11 The Class BankAccount - attributes and constructor namespace Banking { public class BankAccount { private double balance; private int accNo; private int interestRate; public BankAccount(int no, int ir) { balance = 0; accNo = no; intrestRate = ir; }

FEN AK IT: Softwarekonstruktion12 Methods public bool Withdraw(double amount) public void Deposite(double amout) public void GiveInterest()

FEN AK IT: Softwarekonstruktion13 Properties public int InterestRate { get{return interestRate;} set{if( value>=0) interestRate = value;} } Lets do it in C# using Visual Studio. Source hereSource here.

UML class diagrams Can be used for different things: – Domain model (or conceptual model) showing objects in the users world ((problem) domain) and their relationships. – Design class diagram showing the structure of the code. For the moment our class diagrams will do both. FEN AK IT: Softwarekonstruktion14

UML class diagram A class is described by: – Its name – Its attributes (field variables, data members) – Its methods (operations) FEN AK IT: Softwarekonstruktion15 Class name Attributes Methods For simplicity attributes and/or methods may be omitted, so only structure is shown

UML class diagram The classes are connected – in UML: FEN AK IT: Softwarekonstruktion16 The connection is called an association, and is shown as a line. The multiplicity or cardinality of the association is indicated by numbers. 1 means exactly one, so a bank account belongs to exactly one customer Multiplicity may have a minimum and a maximum: 0..1 means zero or one, so a customer may be associated with zero or one bank account.

Object Interaction The Banking example shows object interaction: – The classes are connected – in UML: FEN AK IT: Softwarekonstruktion17

In the Code public class Customer{ //… private BankAccount account; //… public void CreateAccount(int no, double ir, double bal){ account= new BankAccount(no, ir, bal); } //… FEN AK IT: Softwarekonstruktion18 Customer is responsible for creating BankAccount objects. The association is implemented by an object reference (attribute).

In the Code public class Program{ //… Customer c = new Customer(1, "Peter Thomsen"); //… Console.WriteLine("Customer: "+ c.Name +" has DKK “ + c.Account.Balance + " in the bank"); FEN AK IT: Softwarekonstruktion19 Methods in the other class is called using the reference.

Cardinality or Multiplicity Tells how many objects an object may be associated with: – One customer may have one account, an account must belong to a customer. – One customer may have many accounts, an account must belong to one customer. – A customer may have zero or more accounts, an account may belong to one or more customers. FEN AK IT: Softwarekonstruktion20

Exercise Do the first parts of the ForestExercise (classes Owner and Forest, no 1 – 13). The rest of the ForestExercise. FEN AK IT: Softwarekonstruktion21