visula c# programming lecture 8

18
Get set property class Thought { private int a; public int xy { get { return a; } set { a = value; } } } class Program { static void Main( string[] args ){ Thought t = new Thought(); t.xy = 23; Console.WriteLine("value=“+t. xy); Console.ReadLine(); } }

Upload: abu-bakr-ashraf

Post on 10-May-2015

337 views

Category:

Education


1 download

DESCRIPTION

Lecture 8

TRANSCRIPT

Page 1: Visula C# Programming Lecture 8

Get set property

class Thought { private int a; public int xy { get { return a; } set { a = value; } } }

class Program { static void Main( string[] args ){

Thought t = new Thought(); t.xy = 23; Console.WriteLine("value=“+t.xy); Console.ReadLine(); } }

Page 2: Visula C# Programming Lecture 8

2

Abstract Class

Motivation: if you think no object of a class should be created, declare the class abstract

In general, an abstract class is a placeholder in a class hierarchy that represents a generic concept The use of abstract classes is a design decision; it helps

us establish common elements in a class that is too general to instantiate

Vehicle

Car Boat Plane

Page 3: Visula C# Programming Lecture 8

3

Abstract Classes: Syntax Use the modifier abstract on a class header to declare

an abstract class, e.g.,

abstract class Vehicle{ // … public abstract void Move();}class Car : Vehicle{ //… public void Move() { Console.WriteLine( “Car is moving!” ); }}abstract class StaffMember{ // … public abstract double Pay();}

Page 4: Visula C# Programming Lecture 8

4

Abstract Class: Some Properties

An abstract class cannot be instantiated but you can define a reference of an abstract

class

Only abstract classes can contain abstract methods

The child of an abstract class must override the abstract methods of the parent, or it too must be declared abstract

Page 5: Visula C# Programming Lecture 8

5

C# Contains Many Other Features to Further Refine the Definition of a Class

Sealed class or method if you declare a class or method sealed,

then the class or method cannot be extended

Operator overloading you can define operators for a class so that

operations look more like a normal arithmetic operation

Page 6: Visula C# Programming Lecture 8

6

Using Interface for Multiple Inheritance

Java/C# decision: single inheritance, meaning that a derived class can have only one parent class

To take the advantages of multiple inheritance, Java/C# defines interfaces, which give us the best aspects of multiple inheritance without the complexity

Page 7: Visula C# Programming Lecture 8

7

C# Interface

A C# interface is a collection of methods, and properties it can actually include other types of definitions

These methods and properties have no implementation

An interface is used to formally define a set of methods that a class will implement

An interface captures one aspect of a class If class D is derived from class B, then you should feel

comfortable in saying an object of D is also a B

If class D implements interface I, then you should feel comfortable in saying an object of D has I perspective

Page 8: Visula C# Programming Lecture 8

8

Interfaces: Syntaxinterface is a reserved wordinterface is a reserved word

public interface IComplexity { int Level { get; set; }}

public interface IDisplayAble{ void Paint(); } // inherits Question, implements two interfaces class MultiChoice: Question, IComplexity, IDisplayAble { public void Paint() {...} public int Level {...} public string GetQuestionPart() {} public string GetAnswerPart() {}}

Page 9: Visula C# Programming Lecture 8

9

Interfaces Methods in an interface have public visibility by

default An interface cannot be instantiated

A class implements an interface by stating so in the class header after :

A class can implement multiple interfaces: the interfaces are separated by commas

If a class asserts that it implements an interface, it must define all methods in the interface or the compiler will produce errors

A class that implements an interface can implement other methods as well

Page 10: Visula C# Programming Lecture 8

10

Polymorphism via Interfaces

An interface name can be used as the type of an object reference variable

IDoable obj;

The obj reference can be used to point to any object of any class that implements the IDoable interface

The version of doThis that the following line invokes depends on the type of object that obj is referring to:

obj.doThis();

Page 11: Visula C# Programming Lecture 8

11

An Example

public interface ISpeak{ public void Speak();}class Faculty {…}class Professor: Faculty, ISpeak{ // public void Speak()

{…} public void Pontificate() {…}}class Animal {}class Dog: Animal, ISpeak{ // public void Speak()

{ … }}

ISpeak guest;

guest = new Professor();guest.Speak();

guest = Dog();guest.Speak();

ISpeak special;special = new Professor();

special.Pontificate();

ISpeak special;special = new Professor();

((Professor)special).Pontificate();

// compiler error

Page 12: Visula C# Programming Lecture 8

12

Exception Handing

Exception handling is an in built mechanism in .NET framework to detect and handle run time errors.

The .NET framework contains lots of standard exceptions.

The exceptions are anomalies that occur during the execution of a program.

They can be because of user, logic or system errors.

Page 13: Visula C# Programming Lecture 8

13

Exception Handing

If a user (programmer) do not provide a mechanism to handle these anomalies, the .NET run time environment provide a default mechanism, which terminates the program execution.  

Page 14: Visula C# Programming Lecture 8

14

Syntax

C# provides three keywords try, catch and finally to do exception handling.

try{// Statement which can cause an exception.}catch(Type x){// Statements for handling the exception}finally{//Any cleanup code} 

Page 15: Visula C# Programming Lecture 8

15

Uncaught Exception

using System;class MyClient{public static void Main(){int x = 0;int div = 100/x;Console.WriteLine(div);}} 

Page 16: Visula C# Programming Lecture 8

16

Exception handled

class MyClient{public static void Main(){int x = 0;int div = 0;try{div = 100/x;Console.WriteLine("This line in not executed");}catch(DivideByZeroException de){

Console.WriteLine("Exception occured");}

finally{Console.WriteLine("Finally Block");}

Console.WriteLine("Result is {0}",div);}} 

Page 17: Visula C# Programming Lecture 8

17

Catching all Exception

using System;class MyClient{public static void Main(){int x = 0;int div = 0;try{div = 100/x;Console.WriteLine("Not executed line");}catch{Console.WriteLine("oException" );}Console.WriteLine("Result is {0}",div);}}

Page 18: Visula C# Programming Lecture 8

18

Standard Exceptions

System.OutOfMemoryException System.NullReferenceException Syste.InvalidCastException Syste.ArrayTypeMismatchException System.IndexOutOfRangeException

         System.ArithmeticException System.DevideByZeroException System.OverFlowException