Download presentation
Presentation is loading. Please wait.
1
UNIT 2
2
Object oriented aspects of C#
3
Classes and objects . Classes
Class is a template that defines the form of object. It specifies both the data and code that will operate the data. Principle of oops: Encapsulation Polymorphism Inheritance 3
4
What is an Object? An object is a self-contained piece of functionality that can be easily used, and re-used as the building blocks for a software application. Objects consist of data variables and functions (called methods) that can be accessed and called on the object to perform tasks. These are collectively referred to as members. 4
5
What is a Class? . Class is a blueprint or architect's drawing defines what an item or a building will look like once it has been constructed, A class defines what an object will look like when it is created. It defines, for example, what the methods will do and what the member variables will be. . 5
6
Declaring a C# Class A C# class is declared using the public class keywords followed by the name of the class class name begin with a capital letter: public class BankAccount { } 6
7
Creating C# Class Members
Class members or properties are essentially variables and methods embedded into the class. Members can be public, private or protected. public members can be accessed from outside the object and are also visible in classes derived from the current class. private members can only be accessed by methods contained in the class and are not accessible to derived classes. protected classes are only available to derived classes. public class BankAccount { public string accountName; public int accountFee; private int accountBalance; private int accountNumber; } 7
8
Static, Read-only and Const Data Members
C# static member types (also referred to as class properties) are used to store data values which are common to all object instances of class. For example, all bank customers would likely earn the same rate of interest on a savings account. An interestRate member would, therefore, be declared as static since it is common across all object instances of the class. Static members are declared using the static keyword. For example: public class BankAccount { public static int interestRate; } 8
9
Instantiating an Object from a C# Class
The process of creating an object from the class blueprint is called instantiation. The first step is to create an object variable of the required object type. An instance of the object is then created using the new keyword and assigned to the object variable: BankAccount custAccount; custAccount = new BankAccount(); It is also possible to declare the object variable and assign the object in a single statement: BankAccount custAccount = new BankAccount(); 9
10
public class BankAccount public string accountName;
using System; class Hello { public class BankAccount public string accountName; public int accountFee; private int accountBalance; private int accountNumber; } static void Main() BankAccount custAccount = new BankAccount(); custAccount.accountName = "John Smith"; custAccount.accountFee = 5; Console.WriteLine ("Customer Name is " + custAccount.accountName); Console.WriteLine ("Account Fee = $" + custAccount.accountFee); The above code assigns values to the accountName and accountFee members Our object. It then references the properties in order to display text which reads: 10
11
Adding Methods to a C# Class
Class rectangle { int length; int width; Public voidGetdata(int x,int y) length=x; Width=y; } 11
12
Class methods are called using dot notation
Class methods are called using dot notation. For example, to set the value of the accountNumber: BankAccount custAccount = new bankAccount(); custAccount.setAccountNumber( ); Console.WriteLine ("Account Number = " + custAccount.getAccountNumber() ); The above code sets the account number using the setter method and then displays the account number using the getter method.
13
C# Constructors and Finalizers
Initializes an object when it is created It has the same name as its class and is syntatically similar to a method Const have no explict return type Access class –name() public class BankAccount { public string accountName; public int accountFee; private int accountBalance; private int accountNumber; // Constructor public BankAccount(string acctName, int acctNumber) accountName = acctName; accountNumber = acctNumber; } // .... BankAccount custAccount = new BankAccount("Fred Wilson", ); 13
14
public ~BankAccount(string acctName, int acctNumber) {
Finalizers are used to clean up any resources used by a class object when the object is destroyed. Unlike constructors which can be triggered from code using the new keyword there is no way to explicitly call a finalizer (for example there is no delete equivalent to the new keyword). Instead, the finalizer will be called when the garbage collector decides that the object instance is no longer needed. All the programmer can be sure of is that the finalizer will be called at some time between the when the object is no longer needed by the code and the point that the application terminates. Finalizers are defined in the same way as constructors with the exception that the name is preceded by a tilde (~): // Finalizer public ~BankAccount(string acctName, int acctNumber) { // Code to perform clean up } CLR via C#, Jeffrey Richter 14
15
What is Inheritance? The concept of inheritance brings something of a real-world view to programming. It allows a class to be defined which has a number of characteristics and then other classes to be created which are derived from that class. The derived class inherits all of the features of the parent class and typically then adds some features of its own. By deriving classes we create what is often referred to as a class hierarchy. The class at the top of the hierarchy is known as the base class and the derived classes as subclasses. Any number of classes may be derived from a class. It is only possible for a derived class to inherit from one class. As such, C# is known as a single inheritance programming language. Classes need not only be derived from a base class. For example, a subclass can also inherit from another subclass. 15
16
An Example of Inheritance
This class does a good job of defining characteristics common to any type of bank account, such as account holder name, account number and current balance. Imagine, however, that our banking program needs to support a number of specific types of account. For example, the bank might offer its customers an interest bearing savings account. A savings account will have all the characteristics of our BankAccount class but would also need a way to store the prevailing interest rate. One option would be to create a brand new class from the ground up called SavingsAccount which duplicates everything we have in our BankAccount class, plus extra members needed for a savings account. Another, more efficient method is to derive a SavingsAccount class from the BankAccount class and then add in the extra functionality into this subclass. public class BankAccount { public string accountName; public int accountFee; private int accountBalance; private int accountNumber; public int getAccountNumber() return accountNumber; } public void setAccountNumber(int newNumber) accountNumber = newNumber; 16
17
Creating a Subclass in C#
Now that we have ascertained that we need to create a sub class of our BankAccount class we can take a look at the code necessary to achieve this. Subclasses are declared in the same way as any other class with the exception that the class name is followed by a colon (:) followed by the name of the class from which it is to inherit. With this in mind we can begin by creating our SavingsAccount class: public class BankAccount { public string accountName; public int accountBalance; public int accountNumber; public BankAccount (string name, int number) { accountName = name; accountNumber = number; } public int getAccountNumber() return accountNumber; public void setAccountNumber(int newNumber) accountNumber = newNumber; } public class SavingsAccount : BankAccount { } 17
18
Passing Arguments to the Base Class Constructor
Of particular significance is the constructor. In the BankAccount base class we have a constructor which takes the account name and account number as arguments. In the SavingsAccount subclass we need to accept two additional arguments - the balance and the interest rate. The : base code instructs C# to handle the name and number arguments using the constructor from the base class. The remaining two arguments are then passed to the SavingsAccount constructor. With our subclass complete we can now make use of it: static void Main() { SavingsAccount saveAccount = new SavingsAccount("Fred Wilson", , 432, 0.02F); Console.WriteLine ("Interest this Month = " + saveAccount.monthlyInterest() ); } 18
19
POLYMORPHISM Polymorphism Polymorphism is the ability for classes to provide different implementations of methods that are called by the same name. Polymorphism allows a method of a class to be called without regard to what specific implementation it provides. 19
20
Method overriding Sample code Virtual and Override keywords allows you to implement Methods Overriding in Base and Derived Classes. Different implementations of a method with the same name and signature in the base and sub-classes is called as Polymorphism. For complete details, please refer the following URL: 20
21
The New Keyword C# introduces a keyword new to mark a method as a non-overriding method and as the one which we don't want to use polymorphically. For complete details, please refer the following URL: 21
22
“is” and “as keyword To check the run-time type of an object, you can use either “is” or “as” keyword. “is” compares the type of the object with the given type and returns true if it is cast-able otherwise, it returns false. For complete details, please refer the following URL: 22 22
23
23 For complete details, please refer the following URL:
23 23
24
24 For complete details, please refer the following URL:
24 24
25
Operator overloading All unary and binary operators have pre-defined implementations, that are automatically available in any expressions. In addition to this pre-defined implementations, user defined implementations can also be introduced in C#. The mechanism of giving a special meaning to a standard C# operator with respect to a user defined data type such as classes or structures is known as operator overloading. Remember that it is not possible to overload all operators in C#. The following table shows the operators and their overloadability in C#.
26
Operator overloading Operators Overloadability
+, -, *, /, %, &, |, <<, >> All C# binary operators can be overloaded. +, -, !, ~, ++, --, true, false All C# unary operators can be overloaded. ==, !=, <, >, <= , >= All relational operators can be overloaded, but only as pairs. &&, || They can't be overloaded () (Conversion operator) They can't be overloaded +=, -=, *=, /=, %= These compound assignment operators can be overloaded. But in C#, these operators are automatically overloaded when the respective binary operator is overloaded. =, . , ?:, ->, new, is, as, sizeof These operators can't be overloaded
27
Delegates and Events A delegate in C# is similar to a function pointer in C or C++. Using a delegate allows the programmer to encapsulate a reference to a method inside a delegate object. The delegate object can then be passed to code which can call the referenced method, without having to know at compile time which method will be invoked. The Event model in C# finds its roots in the event programming model that is popular in asynchronous programming. The basic foundation behind this programming model is the idea of "publisher and subscribers." In this model, you have publishers who will do some logic and publish an "event." Publishers will then send out their event only to subscribers who have subscribed to receive the specific event. In C#, any object can publish a set of events to which other applications can subscribe. When the publishing class raises an event, all the subscribed applications are notified. The following figure shows this mechanism. 27
28
Errors and Exceptions int SafeDivision(int x, int y) { try
The C# language's exception handling features provide a way to deal with any unexpected or exceptional situations that arise while a program is running. Exception handling uses the try, catch, and finally keywords to attempt actions that may not succeed, to handle failures, and to clean up resources afterwards. Exceptions can be generated by the common language runtime (CLR), by third-party libraries, or by the application code using the throw keyword. In this example, a method tests for a division by zero, and catches the error. Without the exception handling, this program would terminate with a DivideByZeroException was unhandled error. int SafeDivision(int x, int y) { try return (x / y); } catch ( (System.DivideByZeroException dbz) System.Console.WriteLine("Division by zero attempted!"); return 0; } 28 28
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.