Created By-

PRANAB BHATTACHARYA
MCA-TY
Roll no. : 14
 Overview
 Classes and objects

Class members

Methods

Constructors

Destructors

Nested Classes

Access Modifier...
o .NET

Framework languages provide full support for
Object-oriented programming
o Encapsulation, Inheritance, Polymorphis...
o Classes describe the type of objects…
o …while objects are usable instances of classes.
o Using the blueprint analogy, a...
o A method is an action that an object can perform.
To define a method of a class:
class SampleClass
{
public int sampleMe...
o A class can have several implementations, or
overloads, of the same method that differ in the
number of parameters or pa...
o Constructors are class methods that are executed
automatically when an object of a given type is created. (Run
before an...
o Destructors are used to destruct instances of classes.
o In the .NET Framework, the garbage collector
automatically mana...
o A class defined within another class is called nested.
(Default - private)
class Container
{
class Nested
{
// Add code ...
o All classes and class members can specify what access level
they provide to other classes by using access modifiers.
The...
o A static member of the class is a
property, procedure, or field that is shared by all
instances of a class.
o To define ...
o To access the static member, use the name of the
class without creating an object of this class:
Console.WriteLine(Sampl...
o Enable you to create objects without writing a class
definition for the data type. (compiler generates a
class)
oThe cla...
o Creates a new class that reuses, extends, and modifies
the behavior that is defined in another class.
Base Class
– The c...
o By default all classes can be inherited.
o We can specify whether a class must not be used as a
base class, or create a ...
o By default, a derived class inherits all members
from its base class.
oyou want to change the behavior of the inherited
...
Overriding modifiers..
C# Modifier
Definition
virtual (C# Reference) - Allows a class member to be
overridden in a derived...
o Interfaces, like classes, define a set of
properties, methods, and events.
o But unlike classes, interfaces do not provi...
o To define an interface:
interface ISampleInterface
{
void doSomething();
}

o To implement an interface in a class:
clas...
19
Upcoming SlideShare
Loading in …5
×

Object Oriented Programming with C#

1,595 views

Published on

An Overview of OOP concepts using C#.
.NET is a beautiful framework, OOP gives it the Oomph !

Published in: Technology
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
1,595
On SlideShare
0
From Embeds
0
Number of Embeds
8
Actions
Shares
0
Downloads
66
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Object Oriented Programming with C#

  1. 1. Created By- PRANAB BHATTACHARYA MCA-TY Roll no. : 14
  2. 2.  Overview  Classes and objects  Class members  Methods  Constructors  Destructors  Nested Classes  Access Modifiers and Access levels  Static (Shared) Classes and Members  Anonymous types  Inheritance  Overriding members • Sealed and Abstract classes  Interfaces 1
  3. 3. o .NET Framework languages provide full support for Object-oriented programming o Encapsulation, Inheritance, Polymorphism o Encapsulation means that a group of related properties, methods, and other members are treated as a single unit or object. o Inheritance describes the ability to create new classes based on an existing class. o Polymorphism means that you can have multiple classes that can be used interchangeably, even though each class implements the same properties or methods in different ways. 2
  4. 4. o Classes describe the type of objects… o …while objects are usable instances of classes. o Using the blueprint analogy, a class is a blueprint, and an object is a building made from that blueprint. To define a class and object: class SampleClass { } SampleClass SC1 = new SampleClass(); 3
  5. 5. o A method is an action that an object can perform. To define a method of a class: class SampleClass { public int sampleMethod(string sampleParam) { // Insert code here } } 4
  6. 6. o A class can have several implementations, or overloads, of the same method that differ in the number of parameters or parameter types. oTo overload a method: public int sampleMethod(string sampleParam) {}; public int sampleMethod(int sampleParam) {} Public int sampleMethod(int param1,int param2){}; o In most cases you declare a method within a class definition. However, both Visual Basic and C# also support extension methods that allow you to add methods to an existing class outside the actual 5
  7. 7. o Constructors are class methods that are executed automatically when an object of a given type is created. (Run before any other code) o Constructors usually Initialize the data members. oSupport Overloading. To define a constructor for a class: public class SampleClass { int I; public SampleClass() { I = 1; } 6
  8. 8. o Destructors are used to destruct instances of classes. o In the .NET Framework, the garbage collector automatically manages the allocation and release of memory for the managed objects in your application. o Destructors are still needed to clean up any unmanaged resources that your application creates. o There can be only one destructor for a class. 7
  9. 9. o A class defined within another class is called nested. (Default - private) class Container { class Nested { // Add code here. } } o To create an instance of the nested class, use the name of the container class followed by the dot and then followed by the name of the nested class: Container.Nested nestedInstance = new 8
  10. 10. o All classes and class members can specify what access level they provide to other classes by using access modifiers. The following access modifiers are available: C# Modifier Definition ___________________________________________________ Public The type or member can be accessed by any other code in the same assembly or another assembly that references it. Private The type or member can only be accessed by code in the same class. Protected The type or member can only be accessed by code in the same class or in a derived class. Internal The type or member can be accessed by any code in the 9
  11. 11. o A static member of the class is a property, procedure, or field that is shared by all instances of a class. o To define a static (shared) member: static class SampleClass { public static string SampleString = "Sample String"; } 10
  12. 12. o To access the static member, use the name of the class without creating an object of this class: Console.WriteLine(SampleClass.SampleStrin g); o Static (shared) classes in C# have static (shared) members only and cannot be instantiated. o Static (shared) members also cannot access nonstatic (non-shared) properties, fields or methods 11
  13. 13. o Enable you to create objects without writing a class definition for the data type. (compiler generates a class) oThe class has no usable name and contains the properties you specify in declaring the object. o To create an instance of an anonymous type: // sampleObject is an instance of a simple anonymous type. var sampleObject = new { FirstProperty = "A", SecondProperty = "B" }; 12
  14. 14. o Creates a new class that reuses, extends, and modifies the behavior that is defined in another class. Base Class – The class whose members are inherited Derived Class – The class that inherits those members. o In C#, all classes implicitely inherit from the Object class. * .NET Framework doesn’t support MULTIPLE INHERITANCE i.e. One derived class can not have more than One base class Syntax : 13
  15. 15. o By default all classes can be inherited. o We can specify whether a class must not be used as a base class, or create a class that can be used as a base class only. To specify that a class cannot be used as a base class: public sealed class A { }  To specify that a class can be used as a base class only and cannot be instantiated : public abstract class B { } 14
  16. 16. o By default, a derived class inherits all members from its base class. oyou want to change the behavior of the inherited member, you need to override it oWhat is Overriding ?? -> Overriding is defining a new implementation of a method, property or event in the derived class. 15
  17. 17. Overriding modifiers.. C# Modifier Definition virtual (C# Reference) - Allows a class member to be overridden in a derived class. override (C# Reference) - Overrides a virtual (overridable) member defined in the base class. abstract (C# Reference) - Requires that a class member to be overridden in the derived class. 16
  18. 18. o Interfaces, like classes, define a set of properties, methods, and events. o But unlike classes, interfaces do not provide implementation. oThey are implemented by classes, and defined as separate entities from classes. oAn interface represents a contract, in that a class that implements an interface must implement every aspect of that interface exactly 17
  19. 19. o To define an interface: interface ISampleInterface { void doSomething(); } o To implement an interface in a class: class SampleClass : ISampleInterface { void ISampleInterface.doSomething() { // Method implementation. } } 18
  20. 20. 19

×