1 OOP in C#: Object Interaction. Inheritance and Polymorphism OOP in C# - Del 1.

Slides:



Advertisements
Similar presentations
Written by: Dr. JJ Shepherd
Advertisements

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.
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#
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.
Road Map Introduction to object oriented programming. Classes
Classes. Object-Oriented Design Method for designing computer programs Consider “objects” interacting in the program –Example: a zoo, a gradebook.
1 Chapter 7 l Inheritance Basics l Programming with Inheritance l Dynamic Binding and Polymorphism Inheritance.
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.
CSM-Java Programming-I Spring,2005 Objects and Classes Overview Lesson - 1.
Module 7: Object-Oriented Programming in Visual Basic .NET
COP INTERMEDIATE JAVA Designing Classes. Class Template or blueprint for creating objects. Their definition includes the list of properties (fields)
Week 4 Introduction to Computer Science and Object-Oriented Programming COMP 111 George Basham.
Classes CS 21a: Introduction to Computing I First Semester,
Chapter 7 Objects and Classes 1 Fall 2012 CS2302: Programming Principles.
ACO 101: Introduction to Computer Science Anatomy Part 2: Methods.
Inheritance CSC 171 FALL 2004 LECTURE 18. READING Read Horstmann, Chapter 11.
Java Objects and Classes. Overview n Creating objects that belong to the classes in the standard Java library n Creating your own classes.
Java Software Solutions Lewis and Loftus Chapter 4 1 Copyright 1997 by John Lewis and William Loftus. All rights reserved. Objects and Classes -- Introduction.
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.
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.
Chapter 4 Introduction to Classes, Objects, Methods and strings
Session 04: C# OOP 2 OOP in C#: Object-Oriented Design.
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:
Overview The Basics – Python classes and objects Procedural vs OO Programming Entity modelling Operations / methods Program flow OOP Concepts and user-defined.
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.
Bank Account Example public class BankAccount { private double balance; public static int totalAccounts = 0; public BankAccount() { balance = 0; totalAccounts++;
Object-Oriented Programming (OOP) What we did was: (Procedural Programming) a logical procedure that takes input data, processes it, and produces output.
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.
Topics Instance variables, set and get methods Encapsulation
5.1 Basics of defining and using classes A review of class and object definitions A class is a template or blueprint for an object A class defines.
Class Fundamentals BCIS 3680 Enterprise Programming.
Object-Oriented Programming: Classes and Objects Chapter 1 1.
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.
Basic Class Structure. Class vs. Object class - a template for building an object –defines the instance data that the object will hold –defines instance.
Object Oriented Programming. Constructors  Constructors are like special methods that are called implicitly as soon as an object is instantiated (i.e.
Classes (Part 1) Lecture 3
Java OOP Overview Classes and Objects, Members and Class Definition, Access Modifier, Encapsulation Java OOP Overview SoftUni Team Technical Trainers.
Classes and OOP.
Computing with C# and the .NET Framework
Lecture 9 Concepts of Programming Languages
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.
Inheritance Basics Programming with Inheritance
Computer Programming with JAVA
CS2011 Introduction to Programming I Objects and Classes
JAVA CLASSES.
By Rajanikanth B OOP Concepts By Rajanikanth B
Classes CS 21a: Introduction to Computing I
Lecture 9 Concepts of Programming Languages
Chengyu Sun California State University, Los Angeles
Introduction to Computer Science and Object-Oriented Programming
Presentation transcript:

1 OOP in C#: Object Interaction. Inheritance and Polymorphism OOP in C# - Del 1

Object-Oriented Programming “ The Three Pillars of OOP”: Encapsulation Inheritance Polymorphism The Substitution Principle UCN Technology: Computer Science2

3 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, talk 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.

4 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.

5 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.: Account: 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.

6 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.

7 Properties (C# speciality) Are used for getting and setting attribute values. (Replace set- and get-methods in Java). Provide a syntax similiar to direct access of the attributes. (Anders Hejsberg footprint?)

Properties (Auto property) private string name; public string Name { get { return this.name; } set { this.name = value; } Java kursus KMD8

Auto-Implemented Properties Java kursus KMD 9 // This class is mutable. Its data can be modified from // outside the class. class Customer { // Auto-Impl Properties for trivial get and set public double TotalPurchases { get; set; } public string Name { get; set; } public int CustomerID { get; set; } // Constructor public Customer(double purchases, string name, int ID) { TotalPurchases = purchases; Name = name; CustomerID = ID; }

Auto-Implemented Properties // This class is immutable. After an object is created, // it cannot be modified from outside the class. It uses a // constructor to initialize its properties. class Contact { // Read-only properties. public string Name { get; private set; } public string Address { get; private set; } // Public constructor. public Contact(string contactName, string contactAddress) { Name = contactName; Address = contactAddress; } Java kursus KMD10

11 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 Account acc = new Account(); Account() 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

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

13 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

14 The Class Account - attributes and constructor namespace Banking { public class Account { private double balance; private int accNo; private int interestRate; public Account(int no, int ir) { balance = 0; accNo = no; intrestRate = ir; }

FEN Methods public bool Withdraw(double amount) public void Deposite(double amout) public void GiveInterest()

16 Properties public int InterestRate { get{return interestRate;} set{if( value>=0) interestRate = value;} } Lets do in C# using Visual Studio. Source here.here

17 Opgaver Lav et MS VS projekt brug denne kode: EmpProjectV1.rar EmpProjectV1.rar Test kode – understand it.