[ISRAR ALI] Hammad Khan. The namespace keyword is used to declare a scope. Making software components reusable can result in naming collisions (two classes.

Slides:



Advertisements
Similar presentations
OOP Abstraction Classes Class Members: Properties & Methods Instance (object) Encapsulation Interfaces Inheritance Composition Polymorphism Using Inheritance.
Advertisements

1 l Inheritance Basics l Programming with Inheritance l Dynamic Binding and Polymorphism Inheritance.
Road Map Introduction to object oriented programming. Classes
Inheritance and Class Hierarchies Chapter 3. Chapter 3: Inheritance and Class Hierarchies2 Chapter Objectives To understand inheritance and how it facilitates.
OOP in Java Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
C#.NET C# language. C# A modern, general-purpose object-oriented language Part of the.NET family of languages ECMA standard Based on C and C++
C# Programming: From Problem Analysis to Program Design1 Advanced Object-Oriented Programming Features C# Programming: From Problem Analysis to Program.
Data Abstraction and Object- Oriented Programming CS351 – Programming Paradigms.
Lecture 9 Concepts of Programming Languages
OOP in Java Fawzi Emad Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
C++ fundamentals.
Differences between C# and C++ Dr. Catherine Stringfellow Dr. Stewart Carpenter.
OOP Languages: Java vs C++
Inheritance. Types of Inheritance Implementation inheritance means that a type derives from a base type, taking all the base type’s member fields and.
Object Based Programming. Summary Slide  Instantiating An Object  Encapsulation  Inheritance  Polymorphism –Overriding Methods –Overloading vs. Overriding.
Object Oriented Software Development
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.
“is a”  Define a new class DerivedClass which extends BaseClass class BaseClass { // class contents } class DerivedClass : BaseClass { // class.
Lecture 8 Inheritance Richard Gesick. 2 OBJECTIVES How inheritance promotes software reusability. The concepts of base classes and derived classes. To.
Writing Classes (Chapter 4)
CSM-Java Programming-I Spring,2005 Objects and Classes Overview Lesson - 1.
Lecture 9 Polymorphism Richard Gesick.
An Object-Oriented Approach to Programming Logic and Design
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.
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.
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.
Programming Languages and Paradigms Object-Oriented Programming.
CS200 Algorithms and Data StructuresColorado State University Part 4. Advanced Java Topics Instructor: Sangmi Pallickara
Chapter 6 OOP: Creating Object-Oriented Programs Programming in C#.NET © 2003 by The McGraw-Hill Companies, Inc. All rights reserved.
Tuc Goodwin  Object and Component-Oriented Programming  Classes in C#  Scope and Accessibility  Methods and Properties  Nested.
Session 08 Module 14: Generics and Iterator Module 15: Anonymous & partial class & Nullable type.
Modern Software Development Using C#.NET Chapter 5: More Advanced Class Construction.
Hoang Anh Viet Hà Nội University of Technology Chapter 1. Introduction to C# Programming.
CS212: Object Oriented Analysis and Design Lecture 9: Function Overloading in C++
1 Chapter 8 – Classes and Object: A Deeper Look Outline 1 Introduction 2 Implementing a Time Abstract Data Type with a Class 3 Class Scope 4 Controlling.
OOP: Encapsulation,Abstraction & Polymorphism. What is Encapsulation Described as a protective barrier that prevents the code and data being randomly.
C# 2.0 and Future Directions Anders Hejlsberg Technical Fellow Microsoft Corporation.
Programming in Java CSCI-2220 Object Oriented Programming.
C# Classes and Inheritance CNS 3260 C#.NET Software Development.
1 Interfaces and Abstract Classes Chapter Objectives You will be able to: Write Interface definitions and class definitions that implement them.
Programming Paradigms Lecturer Hamza Azeem. What is PP ? Revision of Programming concepts learned in CPLB Learning how to perform “Object-Oriented Programming”
Programming with Java © 2002 The McGraw-Hill Companies, Inc. All rights reserved. 1 McGraw-Hill/Irwin Chapter 5 Creating Classes.
Object Oriented Programming
PROGRAMMING IN C#. Collection Classes (C# Programming Guide) The.NET Framework provides specialized classes for data storage and retrieval. These classes.
Introduction to Object-Oriented Programming Lesson 2.
Classes Methods and Properties. Introduction to Classes and Objects In object-oriented programming terminology, a class is defined as a kind of programmer-defined.
Method Overriding Remember inheritance: when a child class inherits methods, variables, etc from a parent class. Example: public class Dictionary extends.
Inheritance and Class Hierarchies Chapter 3. Chapter 3: Inheritance and Class Hierarchies2 Chapter Objectives To understand inheritance and how it facilitates.
Inheritance and Class Hierarchies Chapter 3. Chapter Objectives  To understand inheritance and how it facilitates code reuse  To understand how Java.
Object Oriented Programming Session # 03.  Abstraction: Process of forming of general and relevant information from a complex scenarios.  Encapsulation:
Object Oriented Programming. OOP  The fundamental idea behind object-oriented programming is:  The real world consists of objects. Computer programs.
1 C# - Inheritance and Polymorphism. 2 1.Inheritance 2.Implementing Inheritance in C# 3.Constructor calls in Inheritance 4.Protected Access Modifier 5.The.
Object-Oriented Programming: Inheritance and Polymorphism.
Author: DoanNX Time: 45’.  OOP concepts  OOP in Java.
Lecture 2 Intro. To Software Engineering and Object-Oriented Programming (2/2)
Chapter  Array-like data structures  ArrayList  Queue  Stack  Hashtable  SortedList  Offer programming convenience for specific access.
Object-oriented programming (OOP) is a programming paradigm using "objects" – data structures consisting of data fields and methods together with their.
Inheritance Modern object-oriented (OO) programming languages provide 3 capabilities: encapsulation inheritance polymorphism which can improve the design,
Modern Programming Tools And Techniques-I
Object-Oriented Programming Concepts
Static data members Constructors and Destructors
Inheritance Modern object-oriented (OO) programming languages provide 3 capabilities: encapsulation inheritance polymorphism which can improve the design,
PRINCIPALES OF OBJECT ORIENTED PROGRAMMING
OOP’S Concepts in C#.Net
Pass by Reference, const, readonly, struct
Object-Oriented Programming
Java – Inheritance.
Java Inheritance.
Presentation transcript:

[ISRAR ALI] Hammad Khan

The namespace keyword is used to declare a scope. Making software components reusable can result in naming collisions (two classes defined by two programmers can have the same name). using System.Collections; An assembly is a dynamic link library created when compiling a project. The assembly can be found in the bin\Debug folder of the project. By default, the assembly name will include the namespace name

Example // Namespace Declaration using System; // The C# Station Tutorial Namespace namespace csharp_station { namespace tutorial { // Program start class class NamespaceCSS { // Main begins program execution. public static void Main() { // Write to console Console.WriteLine("This is the new C# Station Tutorial Namespace."); } } } }

Methods Are Actions methods can be implemented within the scope of classes or structures prototyped within interface types may be decorated with various keywords (internal, virtual, public, new, etc.) // static returnVal MethodName(args) {...} class Program { static int Add(int x, int y) { return x + y; }

static int Add(int x, int y) { int ans = x + y; // Caller will not see these changes as you are modifying a copy of the original data. x = 10000; y = 88888; return ans; } static void Main(string[] args) { Console.WriteLine("***** Fun with Methods *****"); // Pass two variables in by value. int x = 9, y = 10; Console.WriteLine("Before call: X: {0}, Y: {1}", x, y); Console.WriteLine("Answer is: {0}", Add(x, y)); Console.WriteLine("After call: X: {0}, Y: {1}", x, y); Console.ReadLine(); }

void MyMethod() { int num1 = 7, num2 = 9; Swap(ref num1, ref num2); // num1 = 9, num2 = 7 } void Swap(ref int x, ref int y) { int temp = x; x = y; y = temp; }

void MyMethod() { int num1 = 7, num2; Subtraction(num1, out num2); // num1 = 7, num2 = 5 } void Subtraction(int x, out int y) { y = x - 2; // y must be assigned a value } uninitialised

void MyMethod() { int sum = Addition(1, 2, 3); // sum = 6 } int Addition(params int[] integers) { int result = 0; for (int i = 0; i < integers.Length; i++) result += integers[i]; return result; }

class Program { static void Main(string[] args) { } // Overloaded Add() method. static int Add(int x, int y) { return x + y; } static double Add(double x, double y) { return x + y; } static long Add(long x, long y) { return x + y; } }

Object-oriented programming (OOP) is a programming paradigm that uses objects and data structures consisting of data fields and methods together with their interactions to design applications and computer programs. Programming techniques may include features such as Information Hiding, Data Abstraction, Encapsulation, Modularity, Polymorphism, and Inheritance

Everything is an object. Think of an object as a fancy variable. it stores data, but you can “make requests” to that object, asking it to perform operations on itself. A program is a bunch of objects telling each other what to do by sending messages. Each object has its own memory made up of other objects. Every object has a type. All objects of a particular type can receive the same messages. An object has state, behavior and identity This means that an object can have internal data (which gives it state), methods (to produce behavior), and each object can be uniquely identified.

The ability to generalize an object as a data type that has a specific set of characteristics and is able to perform a set of actions. Object-oriented languages provide abstraction via classes. Classes define the properties and methods of an object type. ▫You can create an abstraction of a dog with characteristics, such as color, height, and weight, and actions such as run and bite. The characteristics are called properties, and the actions are called methods. ▫Classes are blueprints for Object and Objects are instance of classes.

public class MyArray { private const int MAX_LENGTH = 100; private int length; public MyArray() { length = 0; } public int Length { get { return length; } set { length = value; }

None or Internal Public Abstract or internal abstract Public abstract Sealed or internal sealed Public sealed

A constructor is called automatically right after the creation of an object to initialize it. Constructors have the same name as their class names Default constructor: if no constructor is declared, a parameterless constructor can be declared A class can contain default constructor and overloaded constructors to provide multiple ways to initialise objects. Static constructor: similar to static method. It must be parameterless and must not have an access modifier (private, public).

public class Languages { static Languages() // static constructor {...} public Languages() // default constructor {...} public Languages(string lang) // overloaded constructor {...} } public class MyClass() { Languages langs = new Languages(); }

public class CSharp { public CSharp () {...} public static void StaticMethod() {...} public void NonStaticMethod() {...} } public class MyClass() { CSharp cs = new CSharp(); cs.NonStaticMethod(); CSharp.StaticMethod(); }

public class CSharp { private int count = 0;... public int Count { get { return count; } internal set { count = value; } public class MyClass() { CSharp cs = new CSharp(); cs.Count = 5; int num = cs.Count; }

public class CSharp { int [] vals = new int [10];... public int this [int i] { get { return vals[i]; } public class MyClass() { CSharp cs = new CSharp(); int num = cs[2]; }

Nested types can see all the members of their closing type, both private and public ones. External types only see nested types if they are declared as public. Fields of inner types and outer types belong to different objects. We must specify the object to which they belong when we want to access them. In Java, inner classes can access fields of outer class public class MyClass { private class NestedClass { }

Inheritance is a form of software reusability in which classes are created by reusing data and behaviours of an existing class with new capabilities. Inheritance combines with object composition to create the primary techniques for reusing software A class inheritance hierarchy begins with a base class that defines a set of common attributes and operations that it shares with derived classes. A derived class inherits the resources of the base class and overrides or enhances their functionality with new capabilities. The classes are separate, but related

public: access is not restricted private: access is limited to the containing type protected: access is limited to the containing class or types derived from the containing class internal: access is limited to the current assembly protected internal: access is limited to the current assembly or types derived from the containing class

Abstract class is normally used as a base class and never intended to instantiate any objects. The use of polymorphism requires declaring virtual or abstract methods and properties in the abstract base class and overriding them in the derived classes. Any class with an abstract method in it must be declared abstract. Abstract methods do not have an implementation in the abstract base class and every concrete derived class must override all base-class abstract methods and properties using keyword override. Virtual methods have an implementation in the abstract base class and its derived classes have an option of overriding the method

Abstract class: public abstract class AbstractClass { public AbstractClass() { } public abstract int AbstractMethod(); public virtual int VirtualMethod() { return 0; }

Derived class: public class DerivedClass : AbstractClass { public DerivedClass() { } public override int AbstractMethod() { return 0; } public override int VirtualMethod() { return base.VirtualMethod (); }

The keyword sealed is applied to classes and methods to prevent overriding and inheritance. A method declared sealed cannot be overridden in a derived class Static methods and private methods are implicitly sealed. Recall that a method can be overridden if it is declared either virtual or abstract. The keyword sealed is not applied to these cases. However, we can use the keyword sealed for methods we have overridden and we do not want to override it in further derived classes. A sealed class cannot be a base class since it cannot be inherited and hence it cannot have any derived classes. All methods in a sealed class are sealed implicitly

sealed class SealedClass { public int x; public int y; } class MyClass { SealedClass sc = new SealedClass(); sc.x = 10; sc.y = 20; } class InClass : SealedClass // error: cannot inherit { }

All source code for a type is normally in a single file Sometimes a type becomes large enough that this is an impractical constraint. Partial types allow classes and interfaces to be broken into multiple pieces stored in different source files for easier development and maintenance. Additionally, partial types allow separation of machine-generated and user-written parts of types so that it is easier to augment code generated by a tool

public class Student { private int studentID; private int name; public Student () {... } public ChangeName() {... } public ChangeID () { … } public partial class Student { private int studentID; public Student () {... } } public partial class Student { private int name; public ChangeName() {... } public ChangeID () { public ChangeID () {... }}

An interface definition begins with a keyword interface and contains a set of public methods and properties that have no default implementation to inherit. An interface must be declared as public. public interface IStudent { int StudentID {get; set;} void AddSubject(string subjectName); } Interfaces can be implemented by classes. An interface defines a contract. This means that a class that implements an interface must provide implementations for every method and property specified in the interface definition.

public class Student : IStudent { private int studentID = 0; private ArrayList subjects = null; public Student() {} public int StudentID { get {return studentID; } set {studentID = value; } } public void AddSubject(string subjectName) { subjects.Add(subjectName); }

public class List { private object[] elements; private int count; public void Add(object element) { if (count == elements.Length) Resize(count * 2); elements[count++] = element; } public object this[int index] { get { return elements[index]; } set { elements[index] = value; } } public int Count { get { return count; } } public class List { private T[] elements; private int count; public void Add(T element) { if (count == elements.Length) Resize(count * 2); elements[count++] = element; } public T this[int index] { get { return elements[index]; } set { elements[index] = value; } } public int Count { get { return count; } } List intList = new List(); intList.Add(1);intList.Add(2);intList.Add("Three"); int i = (int)intList[0]; List intList = new List(); intList.Add(1);// Argument is boxed intList.Add(2);// Argument is boxed intList.Add("Three");// Should be an error int i = (int)intList[0];// Cast required List intList = new List (); intList.Add(1);// No boxing intList.Add(2);// No boxing intList.Add("Three");// Compile-time error int i = intList[0];// No cast required

Why generics? ▫Type checking, no boxing, no downcasts ▫Increased sharing (typed collections) How are C# generics implemented? ▫Instantiated at run-time, not compile-time ▫Checked at declaration, not instantiation ▫Work for both reference and value types ▫Exact run-time type information

Collection classes Collection interfaces Collection base classes Utility classes Reflection List Dictionary SortedDictionary Stack Queue IList IDictionary ICollection IEnumerable IEnumerator IComparable IComparer Collection KeyedCollection ReadOnlyCollection Nullable EventHandler Comparer

public class NonGeneric { object item; public object Item { get { return item; } set { item = value; } public class Generic { T item; public T Item { get { return item; } set { item = value; } public class MyClass() { NonGeneric ng = new NonGeneric (); Generic g = new Generic (); }

static void AddMultiple (List list, params T[] values) { foreach (T value in values) list.Add(value); } void MyMethod() { List list = new List (); AddMultiple (list, 2, 4, 6); }

Methods that incrementally compute and return a sequence of values class Program { static IEnumerable Range(int start, int count) { for (int i = 0; i < count; i++) yield return start + i; } static IEnumerable Squares(IEnumerable source) { foreach (int x in source) yield return x * x; } static void Main() { foreach (int i in Squares(Range(0, 10))) Console.WriteLine(i); }