SlideShare a Scribd company logo
Inheritance
using System;
public class Parent
{
string parentString;
public Parent()
{
Console.WriteLine("Parent Constructor.");
}
public Parent(string myString)
{
parentString = myString;
Console.WriteLine(parentString);
}
public void print()
{
Console.WriteLine("I'm a Parent Class.");
}
}
.
public class Child : Parent
{
public Child() : base("From Derived")
{
Console.WriteLine("Child Constructor.");
}
public new void print()
{
base.print();
Console.WriteLine("I'm a Child Class.");
}
public static void Main()
{
Child child = new Child();
child.print();

}
}
Output:
From Derived
Child Constructor.
I'm a Parent Class.
I'm a Child Class.
Class and its members in C#.NET

•
    To create a class, use the keyword class and has the following syntax.
•   [Access Modifier] class ClassName
•   {
•   -
•   -
•   -
•   }
•   A class can be created with only two access modifiers, public and internal.
    Default is public. When a class is declared as public, it can be accessed
    within the same assembly in which it was declared as well as from out side
    the assembly. But when the class is created as internal then it can be
    accessed only within the same assembly in which it was declared.
Class and its members in C#.NET

Members of a Class
A class can have any of the following members.
• Fields
• Properties
• Methods
• Events
• Constructors
• Destructor
• Operators
• Indexers
• Delegates

•   Fields : A field is the variable created within the class and it is used to store data of
    the class. In general fields will be private to the class for providing security for the
    data
•   Syntax : [Access Modifier] DataType Fieldname;
Class and its members in C#.NET

• Properties : A property is a method of the class that appears to the
  user as a field of the class. Properties are used to provide access to
  the private fields of the class. In general properties will be public.
• Syntax : [Access Modifier] DataType PropName
• {
• Get
• {
• }
• Set
• {
• }
• }
• A property contains two accessors, get and set.
Class and its members in C#.NET
•   When user assigns a value to the property, the set accessor of the property will be
    automatically invoked AND VALUE ASSIGNED to the property will be passed to set
    accessor with the help of an implicit object called value. Hence set accessor is used
    to set the value to private field. Within the set accessor you can perform validation
    on value before assigning it to the private field.
    When user reads a property, then the get accessor of the property is automatically
    invoked. Hence get accessor is used to write the code to return the value stored in
    private field.


•   Readonly Property : There may be a situation where you want to allow the user to
    read the property and not to assign a value to the property. In this case property
    has to be created as readonly property and for this create the property only with
    get accessor without set accessor.
•   Syntax : [Access Modifier] DataType PropName
•   {
•   Get
•   {
•   }
•   }
Class and its members in C#.NET
• Writeonly Property : There may be a situation where you want to
  allow the user to assign a value to the property and not to read the
  property. In this case property has to be created as writeonly
  property and for this create the property only with set accessor
  without get accessor.
• Syntax : [Access Modifier] DataType PropName
• {
• Set
• {
• }
• }
• Methods
• Methods are nothing but functions created within the class.
  Functions are used to specify various operations that can be
  performed on data represented by the class.
Class and its members in C#.NET
•   class MyClass
    {
    private int x;
    public void SetX(int i)
    {
    x = i;
    }
    public int GetX()
    {
    return x;
    }
    }
    class MyClient
    {
    public static void Main()
    {
    MyClass mc = new MyClass();
    mc.SetX(10);
    int xVal = mc.GetX();
    Console.WriteLine(xVal);//Displays 10
    }
    }
Example program for property and
          method of a class
• using System;
  namespace example
  {
  class programCall
  {
  int n; //instance variable

  //property nn to access private field n
  public int nn
  {
  set { n = value; }
  get { return n; }
  }
Example program for property and
           method of a class
•   //declare a method to print field n value
    //by default members of class will be private , so to access explictly declare public
    public void print()
    {
    Console.WriteLine(n);
    }

    }
    class MainClass
    {
    static void Main(string[] args)
    {
    //creating object to class programcall
    programCall objpc = new programCall();
Example program for property and
           method of a class
• //instance members can only be accessed with an instance
    objpc.nn = 99999;
    objpc.print();
    Console.Read();

    }
    }
    }

•
    Output
    99999
Constructor in C#.NET
•   A special method of the class that will be automatically invoked when an instance
    of the class is created is called as constructor.
    Constructors are mainly used to initialize private fields of the class while creating
    an instance for the class.
    When you are not creating a constructor in the class, then compiler will
    automatically create a default constructor in the class that initializes all numeric
    fields in the class to zero and all string and object fields to null.
    To create a constructor, create a method in the class with same name as class and
    has the following syntax.
    [Access Modifier] ClassName([Parameters])
•   {
•   }

•
Constructor in C#.NET
• Example program using Constructors
  using System;
  class ProgramCall
  {
  int i, j;

  //default contructor
  public ProgramCall()
  {
  i = 45;
  j = 76;
  }
Constructor in C#.NET
•   public static void Main()
    {
    //When an object is created , contructor is called
    ProgramCall obj = new ProgramCall();
    Console.WriteLine(obj.i);
    Console.WriteLine(obj.j);
    Console.Read();

    }
    }
•
    OUTPUT
    45
    76
Constructor types with example
            programs in C#.NET
• A special method of the class that will be
  automatically invoked when an instance of the
  class is created is called as constructor.
    Constructors can be classified

•   Default Constructor
•   Parameterized Constructor
•   Copy Constructor
•   Private Constructor
Constructor types
•   Default Constructor : A constructor without any parameters is called as default constructor.
    Drawback of default constructor is every instance of the class will be initialized to same values and
    it is not possible to initialize each instance of the class to different values.
    Example for Default Constructor
    Parameterized Constructor : A constructor with at least one parameter is called as parameterized
    constructor. Advantage of parameterized constructor is you can initialize each instance of the class
    to different values.

    Example for Parameterized Constructor
•   using System;
    namespace ProgramCall
    {
    class Test1
    {
    //Private fields of class
    int A, B;
Constructor types
•   //default Constructor
    public Test1()
    {
    A = 10;
    B = 20;
    }
    //Paremetrized Constructor
    public Test1(int X, int Y)
    {
    A = X;
    B = Y;
    }


    //Method to print
    public void Print()
    {
    Console.WriteLine("A = {0}tB = {1}", A, B);
    }

    }
Constructor types
•
    class MainClass
    {
    static void Main()
    {
    Test1 T1 = new Test1(); //Default Constructor is called
    Test1 T2 = new Test1(80, 40); //Parameterized Constructor is called
    T1.Print();
    T2.Print();
    Console.Read();
    }
    }
    }
•
    Output
    A = 10 B = 20
    A = 80 B = 40
Constructor types
•   Copy Constructor : A parameterized constructor that contains a parameter of same class type is
    called as copy constructor. Main purpose of copy constructor is to initialize new instance to the
    values of an existing instance.
    Example for Copy Constructor
    using System;
    namespace ProgramCall
    {
    class Test2
    {
    int A, B;
    public Test2(int X, int Y)
    {
    A = X;
    B = Y;
    }
Constructor types
•   //Copy Constructor
    public Test2(Test2 T)
    {
    A = T.A;
    B = T.B;
    }


    public void Print()
    {
    Console.WriteLine("A = {0}tB = {1}", A, B);
    }

    }

    class CopyConstructor
    {
    static void Main()
    {
Constructor types
•   Test2 T2 = new Test2(80, 90);
    //Invoking copy constructor
    Test2 T3 = new Test2(T2);
    T2.Print();
    T3.Print();
    Console.Read();
    }
    }
    }
•
    Output
    A = 80 B = 90
    A = 80 B = 90
Constructor types
Private Constructor : You can also create a constructor as
   private. When a class contains at least one private
   constructor, then it is not possible to create an instance for
   the class. Private constructor is used to restrict the class
   from being instantiated when it contains every member as
   static.
    Some unique points related to constructors are as follows
•   A class can have any number of constructors.
•   A constructor doesn’t have any return type even void.
•   A static constructor can not be a parameterized
    constructor.
•   Within a class you can create only one static constructor.
Destructor in C#.NET

•
    A destructor is a special method of the class that is automatically invoked while an instance of the
    class is destroyed. Destructor is used to write the code that needs to be executed while an instance
    is destroyed. To create a destructor, create a method in the class with same name as class preceded
    with ~ symbol.
    Syntax :
    ~ClassName()
    {
•   }

    Example : The following example creates a class with one constructor and one destructor. An
    instance is created for the class within a function and that function is called from main. As the
    instance is created within the function, it will be local to the function and its life time will be
    expired immediately after execution of the function was completed.
Destructor in C#.NET

•   using System;
    namespace ProgramCall
    {
    class myclass
    {
    public myclass()
    {
    Console.WriteLine("An Instance Created");
    }
    //destructor
    ~myclass()
    {
    Console.WriteLine("An Instance Destroyed");
    }
    }
Destructor in C#.NET

•
    class Destructor
    {
    public static void Create()
    {
    myclass T = new myclass();
    }

    static void Main()
    {
    Create();
    GC.Collect();
    Console.Read();
    }
    }
    }
•   Output
    An Instance Created
    An Instance Destroyed
Polymorphism in C#
• When a message can be processed in different ways is called
  polymorphism. Polymorphism means many forms.
• Polymorphism is one of the fundamental concepts of OOP.
• Polymorphism provides following features:
• It allows you to invoke methods of derived class through base class
  reference during runtime.
• It has the ability for classes to provide different implementations of
  methods that are called through the same name.
• Polymorphism is of two types:
• Compile time polymorphism/Overloading
• Runtime polymorphism/Overriding
• Compile Time Polymorphism
• Compile time polymorphism is
 method and
 operators overloading.
• In method overloading method performs the different task at the different
  input parameters.
Polymorphism in C#
• Runtime Time Polymorphism
• Runtime time polymorphism is done using inheritance and virtual
  functions. Method overriding is called runtime polymorphism. It is also
  called late binding.
• When overriding a method, you change the behavior of the method for
  the derived class. Overloading a method simply involves having another
  method with the same prototype.
• Caution: Don't confused method overloading with method
  overriding, they are different, unrelated concepts. But they sound similar.
• Method overloading has nothing to do with inheritance or virtual
  methods.
• Following are examples of methods having different overloads:
• void area(int side);
• void area(int l, int b);
• void area(float radius);
Polymorphism in C#
•   Practical example of Method Overloading (Compile Time Polymorphism)
•   using System;
•   namespace method_overloading
•   {
•   class Program
•   {
•   public class Print
•   {
•   public void display(string name)
•   {
•   Console.WriteLine("Your name is : " + name);
•   }
•   public void display(int age, float marks)
•   {
•   Console.WriteLine("Your age is : " + age);
•   Console.WriteLine("Your marks are :" + marks);
•   }
Polymorphism in C#
•   }
•   static void Main(string[] args)
•   {
•   Print obj = new Print();
•   obj.display("George");
•   obj.display(34, 76.50f);
•   Console.ReadLine();
•   }
•   }
•   }
•   Note: In the code if you observe display method is called two times.
    Display method will work according to the number of parameters
    and type of parameters.
Polymorphism in C#
•   When and why to use method overloading
•   Use method overloading in situation where you want a class to be able to do something, but there
    is more than one possibility for what information is supplied to the method that carries out the
    task.
•   You should consider overloading a method when you for some reason need a couple of methods
    that take different parameters, but conceptually do the same thing.
•   Method Overloading showing many forms.
•   using System;
•   namespace method_overloading_polymorphism
•   {
•   class Program
•   {
•   public class Shape
•   {
•   public void Area(float r)
•   {
•   float a = (float)3.14 * r;
•   // here we have used funtion overload with 1 parameter.
•   Console.WriteLine("Area of a circle: {0}",a);
•   }
Polymorphism in C#
•   public void Area(float l, float b)
•   {
•   float x = (float)l* b;
•   // here we have used funtion overload with 2 parameters.
•   Console.WriteLine("Area of a rectangle: {0}",x);
•   }
•   public void Area(float a, float b, float c)
•   {
•   float s = (float)(a*b*c)/2;
•   // here we have used funtion overload with 3 parameters.
•   Console.WriteLine("Area of a circle: {0}", s);
•   }
•   }
Polymorphism in C#
•   static void Main(string[] args)
•   {
•   Shape ob = new Shape();
•   ob.Area(2.0f);
•   ob.Area(20.0f,30.0f);
•   ob.Area(2.0f,3.0f,4.0f);
•   Console.ReadLine();
•   }
•   }
•   }
•   Things to keep in mind while method overloading
•   If you use overload for method, there are couple of restrictions that the compiler imposes.
•   The rule is that overloads must be different in their signature, which means the name and the
    number and type of parameters.
•   There is no limit to how many overload of a method you can have. You simply declare them in a
    class, just as if they were different methods that happened to have the same name.
Method overriding
•   class BaseClass
    {
    public virtual string YourCity()
    {
    return "New York";
    }
    }
    class DerivedClass : BaseClass
    {
    public override string YourCity()
    {
    return "London";
    }
    }
    class Program
    {
    static void Main(string[] args)
    {
    DerivedClass obj = new DerivedClass();
    string city = obj.YourCity();
    Console.WriteLine(city);
    Console.Read();
    }
    } }
•
• Output

 London

More Related Content

What's hot

C notes.pdf
C notes.pdfC notes.pdf
C notes.pdf
Durga Padma
 
C# lecture 2: Literals , Variables and Data Types in C#
C# lecture 2: Literals , Variables and Data Types in C#C# lecture 2: Literals , Variables and Data Types in C#
C# lecture 2: Literals , Variables and Data Types in C#
Dr.Neeraj Kumar Pandey
 
01 c++ Intro.ppt
01 c++ Intro.ppt01 c++ Intro.ppt
01 c++ Intro.ppt
Tareq Hasan
 
CSharp Presentation
CSharp PresentationCSharp Presentation
CSharp Presentation
Vishwa Mohan
 
OOP Poster Presentation
OOP Poster PresentationOOP Poster Presentation
OOP Poster Presentation
Md Mofijul Haque
 
C by balaguruswami - e.balagurusamy
C   by balaguruswami - e.balagurusamyC   by balaguruswami - e.balagurusamy
C by balaguruswami - e.balagurusamy
Srichandan Sobhanayak
 
Let's us c language (sabeel Bugti)
Let's us c language (sabeel Bugti)Let's us c language (sabeel Bugti)
Let's us c language (sabeel Bugti)
Unviersity of balochistan quetta
 
C programming
C programmingC programming
C programming
saniabhalla
 
Python Interview questions 2020
Python Interview questions 2020Python Interview questions 2020
Python Interview questions 2020
VigneshVijay21
 
C# in depth
C# in depthC# in depth
C# in depth
Arnon Axelrod
 
C language
C languageC language
C language
SMS2007
 
Learning c - An extensive guide to learn the C Language
Learning c - An extensive guide to learn the C LanguageLearning c - An extensive guide to learn the C Language
Learning c - An extensive guide to learn the C Language
Abhishek Dwivedi
 
Assignment4
Assignment4Assignment4
Assignment4
Sunita Milind Dol
 
Top C Language Interview Questions and Answer
Top C Language Interview Questions and AnswerTop C Language Interview Questions and Answer
Top C Language Interview Questions and Answer
Vineet Kumar Saini
 
C++ Basics
C++ BasicsC++ Basics
C++ Basics
Himanshu Sharma
 
CProgrammingTutorial
CProgrammingTutorialCProgrammingTutorial
CProgrammingTutorial
Muthuselvam RS
 
C language (Collected By Dushmanta)
C language  (Collected By Dushmanta)C language  (Collected By Dushmanta)
C language (Collected By Dushmanta)
Dushmanta Nath
 
C-PROGRAM
C-PROGRAMC-PROGRAM
C-PROGRAM
shahzadebaujiti
 
C++ Programming Course
C++ Programming CourseC++ Programming Course
C++ Programming Course
Dennis Chang
 
Ppt of c++ vs c#
Ppt of c++ vs c#Ppt of c++ vs c#
Ppt of c++ vs c#
shubhra chauhan
 

What's hot (20)

C notes.pdf
C notes.pdfC notes.pdf
C notes.pdf
 
C# lecture 2: Literals , Variables and Data Types in C#
C# lecture 2: Literals , Variables and Data Types in C#C# lecture 2: Literals , Variables and Data Types in C#
C# lecture 2: Literals , Variables and Data Types in C#
 
01 c++ Intro.ppt
01 c++ Intro.ppt01 c++ Intro.ppt
01 c++ Intro.ppt
 
CSharp Presentation
CSharp PresentationCSharp Presentation
CSharp Presentation
 
OOP Poster Presentation
OOP Poster PresentationOOP Poster Presentation
OOP Poster Presentation
 
C by balaguruswami - e.balagurusamy
C   by balaguruswami - e.balagurusamyC   by balaguruswami - e.balagurusamy
C by balaguruswami - e.balagurusamy
 
Let's us c language (sabeel Bugti)
Let's us c language (sabeel Bugti)Let's us c language (sabeel Bugti)
Let's us c language (sabeel Bugti)
 
C programming
C programmingC programming
C programming
 
Python Interview questions 2020
Python Interview questions 2020Python Interview questions 2020
Python Interview questions 2020
 
C# in depth
C# in depthC# in depth
C# in depth
 
C language
C languageC language
C language
 
Learning c - An extensive guide to learn the C Language
Learning c - An extensive guide to learn the C LanguageLearning c - An extensive guide to learn the C Language
Learning c - An extensive guide to learn the C Language
 
Assignment4
Assignment4Assignment4
Assignment4
 
Top C Language Interview Questions and Answer
Top C Language Interview Questions and AnswerTop C Language Interview Questions and Answer
Top C Language Interview Questions and Answer
 
C++ Basics
C++ BasicsC++ Basics
C++ Basics
 
CProgrammingTutorial
CProgrammingTutorialCProgrammingTutorial
CProgrammingTutorial
 
C language (Collected By Dushmanta)
C language  (Collected By Dushmanta)C language  (Collected By Dushmanta)
C language (Collected By Dushmanta)
 
C-PROGRAM
C-PROGRAMC-PROGRAM
C-PROGRAM
 
C++ Programming Course
C++ Programming CourseC++ Programming Course
C++ Programming Course
 
Ppt of c++ vs c#
Ppt of c++ vs c#Ppt of c++ vs c#
Ppt of c++ vs c#
 

Viewers also liked

06 iec t1_s1_oo_ps_session_08
06 iec t1_s1_oo_ps_session_0806 iec t1_s1_oo_ps_session_08
06 iec t1_s1_oo_ps_session_08
Niit Care
 
Advantages Over Conventional Error Handling in OOP
Advantages Over Conventional Error Handling in OOPAdvantages Over Conventional Error Handling in OOP
Advantages Over Conventional Error Handling in OOP
Raju Dawadi
 
Generics in .NET, C++ and Java
Generics in .NET, C++ and JavaGenerics in .NET, C++ and Java
Generics in .NET, C++ and Java
Sasha Goldshtein
 
9 subprograms
9 subprograms9 subprograms
9 subprograms
jigeno
 
Auteursrecht in academische omgeving: DPO Professionaliseringsbijeenkomst, 23...
Auteursrecht in academische omgeving: DPO Professionaliseringsbijeenkomst, 23...Auteursrecht in academische omgeving: DPO Professionaliseringsbijeenkomst, 23...
Auteursrecht in academische omgeving: DPO Professionaliseringsbijeenkomst, 23...
Leon Osinski
 
Collections in-csharp
Collections in-csharpCollections in-csharp
Collections in-csharp
Lakshmi Mareddy
 
Raspuns MS Subprogram FIV 2016
Raspuns MS Subprogram FIV 2016Raspuns MS Subprogram FIV 2016
Raspuns MS Subprogram FIV 2016
Asociatia SOS Infertilitatea - www.vremcopii.ro
 
A basic course on Research data management: part 1 - part 4
A basic course on Research data management: part 1 - part 4A basic course on Research data management: part 1 - part 4
A basic course on Research data management: part 1 - part 4
Leon Osinski
 
3963066 pl-sql-notes-only
3963066 pl-sql-notes-only3963066 pl-sql-notes-only
3963066 pl-sql-notes-only
Ashwin Kumar
 
16 exception handling - i
16 exception handling - i16 exception handling - i
16 exception handling - i
Ravindra Rathore
 
Oracle database 12c sql worshop 1 activity guide
Oracle database 12c sql worshop 1 activity guideOracle database 12c sql worshop 1 activity guide
Oracle database 12c sql worshop 1 activity guide
Otto Paiz
 
16 logical programming
16 logical programming16 logical programming
16 logical programming
jigeno
 
C# Generics
C# GenericsC# Generics
C# Generics
Rohit Vipin Mathews
 
Delegates and events
Delegates and events   Delegates and events
Delegates and events
Gayathri Ganesh
 
48742447 11g-sql-fundamentals-ii-additional-practices-and-solutions
48742447 11g-sql-fundamentals-ii-additional-practices-and-solutions48742447 11g-sql-fundamentals-ii-additional-practices-and-solutions
48742447 11g-sql-fundamentals-ii-additional-practices-and-solutions
Ashwin Kumar
 
Csci360 08-subprograms
Csci360 08-subprogramsCsci360 08-subprograms
Csci360 08-subprograms
Boniface Mwangi
 
Software engineering lecture notes
Software engineering lecture notesSoftware engineering lecture notes
Software engineering lecture notes
Siva Ayyakutti
 
Free PMP notes,Free PMP Study Material,Free PMP Chapter wise notes,PMP Exam N...
Free PMP notes,Free PMP Study Material,Free PMP Chapter wise notes,PMP Exam N...Free PMP notes,Free PMP Study Material,Free PMP Chapter wise notes,PMP Exam N...
Free PMP notes,Free PMP Study Material,Free PMP Chapter wise notes,PMP Exam N...
TURKI , PMP
 
Constructor ppt
Constructor pptConstructor ppt
Constructor ppt
Vinod Kumar
 
E-Commerce PPT
E-Commerce PPTE-Commerce PPT
E-Commerce PPT
Anshuman Mahapatra
 

Viewers also liked (20)

06 iec t1_s1_oo_ps_session_08
06 iec t1_s1_oo_ps_session_0806 iec t1_s1_oo_ps_session_08
06 iec t1_s1_oo_ps_session_08
 
Advantages Over Conventional Error Handling in OOP
Advantages Over Conventional Error Handling in OOPAdvantages Over Conventional Error Handling in OOP
Advantages Over Conventional Error Handling in OOP
 
Generics in .NET, C++ and Java
Generics in .NET, C++ and JavaGenerics in .NET, C++ and Java
Generics in .NET, C++ and Java
 
9 subprograms
9 subprograms9 subprograms
9 subprograms
 
Auteursrecht in academische omgeving: DPO Professionaliseringsbijeenkomst, 23...
Auteursrecht in academische omgeving: DPO Professionaliseringsbijeenkomst, 23...Auteursrecht in academische omgeving: DPO Professionaliseringsbijeenkomst, 23...
Auteursrecht in academische omgeving: DPO Professionaliseringsbijeenkomst, 23...
 
Collections in-csharp
Collections in-csharpCollections in-csharp
Collections in-csharp
 
Raspuns MS Subprogram FIV 2016
Raspuns MS Subprogram FIV 2016Raspuns MS Subprogram FIV 2016
Raspuns MS Subprogram FIV 2016
 
A basic course on Research data management: part 1 - part 4
A basic course on Research data management: part 1 - part 4A basic course on Research data management: part 1 - part 4
A basic course on Research data management: part 1 - part 4
 
3963066 pl-sql-notes-only
3963066 pl-sql-notes-only3963066 pl-sql-notes-only
3963066 pl-sql-notes-only
 
16 exception handling - i
16 exception handling - i16 exception handling - i
16 exception handling - i
 
Oracle database 12c sql worshop 1 activity guide
Oracle database 12c sql worshop 1 activity guideOracle database 12c sql worshop 1 activity guide
Oracle database 12c sql worshop 1 activity guide
 
16 logical programming
16 logical programming16 logical programming
16 logical programming
 
C# Generics
C# GenericsC# Generics
C# Generics
 
Delegates and events
Delegates and events   Delegates and events
Delegates and events
 
48742447 11g-sql-fundamentals-ii-additional-practices-and-solutions
48742447 11g-sql-fundamentals-ii-additional-practices-and-solutions48742447 11g-sql-fundamentals-ii-additional-practices-and-solutions
48742447 11g-sql-fundamentals-ii-additional-practices-and-solutions
 
Csci360 08-subprograms
Csci360 08-subprogramsCsci360 08-subprograms
Csci360 08-subprograms
 
Software engineering lecture notes
Software engineering lecture notesSoftware engineering lecture notes
Software engineering lecture notes
 
Free PMP notes,Free PMP Study Material,Free PMP Chapter wise notes,PMP Exam N...
Free PMP notes,Free PMP Study Material,Free PMP Chapter wise notes,PMP Exam N...Free PMP notes,Free PMP Study Material,Free PMP Chapter wise notes,PMP Exam N...
Free PMP notes,Free PMP Study Material,Free PMP Chapter wise notes,PMP Exam N...
 
Constructor ppt
Constructor pptConstructor ppt
Constructor ppt
 
E-Commerce PPT
E-Commerce PPTE-Commerce PPT
E-Commerce PPT
 

Similar to Oops

25csharp
25csharp25csharp
25csharp
Sireesh K
 
25c
25c25c
Constructor
ConstructorConstructor
Constructor
abhay singh
 
Constructor and destructor in C++
Constructor and destructor in C++Constructor and destructor in C++
Constructor and destructor in C++
Lovely Professional University
 
Constructors and destructors in C++ part 2
Constructors and destructors in C++ part 2Constructors and destructors in C++ part 2
Constructors and destructors in C++ part 2
Lovely Professional University
 
constructor in object oriented program.pptx
constructor in object oriented program.pptxconstructor in object oriented program.pptx
constructor in object oriented program.pptx
urvashipundir04
 
[OOP - Lec 13,14,15] Constructors / Destructor and its Types
[OOP - Lec 13,14,15] Constructors / Destructor and its Types[OOP - Lec 13,14,15] Constructors / Destructor and its Types
[OOP - Lec 13,14,15] Constructors / Destructor and its Types
Muhammad Hammad Waseem
 
Constructor and Destructor.pdf
Constructor and Destructor.pdfConstructor and Destructor.pdf
Constructor and Destructor.pdf
MadnessKnight
 
Constructors in C++.pptx
Constructors in C++.pptxConstructors in C++.pptx
Constructors in C++.pptx
Rassjb
 
Constructors and Destructor in C++
Constructors and Destructor in C++Constructors and Destructor in C++
Constructor and Destructor
Constructor and DestructorConstructor and Destructor
Constructor and Destructor
Sunipa Bera
 
constructorsfjy5ediykEASFul;IUWORHusi;gfb.pptx
constructorsfjy5ediykEASFul;IUWORHusi;gfb.pptxconstructorsfjy5ediykEASFul;IUWORHusi;gfb.pptx
constructorsfjy5ediykEASFul;IUWORHusi;gfb.pptx
AshrithaRokkam
 
Constructors.16
Constructors.16Constructors.16
Constructors.16
myrajendra
 
Constructor and destructor
Constructor and destructorConstructor and destructor
Constructor and destructor
rajshreemuthiah
 
C#2
C#2C#2
Class introduction in java
Class introduction in javaClass introduction in java
Class introduction in java
yugandhar vadlamudi
 
C# classes objects
C#  classes objectsC#  classes objects
C# classes objects
Dr.Neeraj Kumar Pandey
 
Learning C++ - Class 4
Learning C++ - Class 4Learning C++ - Class 4
Learning C++ - Class 4
Ali Aminian
 
Constructors in JAva.pptx
Constructors in JAva.pptxConstructors in JAva.pptx
Constructors in JAva.pptx
V.V.Vanniaperumal College for Women
 
CONSTRUCTORS IN C++ +2 COMPUTER SCIENCE
CONSTRUCTORS IN C++ +2 COMPUTER SCIENCECONSTRUCTORS IN C++ +2 COMPUTER SCIENCE
CONSTRUCTORS IN C++ +2 COMPUTER SCIENCE
Venugopalavarma Raja
 

Similar to Oops (20)

25csharp
25csharp25csharp
25csharp
 
25c
25c25c
25c
 
Constructor
ConstructorConstructor
Constructor
 
Constructor and destructor in C++
Constructor and destructor in C++Constructor and destructor in C++
Constructor and destructor in C++
 
Constructors and destructors in C++ part 2
Constructors and destructors in C++ part 2Constructors and destructors in C++ part 2
Constructors and destructors in C++ part 2
 
constructor in object oriented program.pptx
constructor in object oriented program.pptxconstructor in object oriented program.pptx
constructor in object oriented program.pptx
 
[OOP - Lec 13,14,15] Constructors / Destructor and its Types
[OOP - Lec 13,14,15] Constructors / Destructor and its Types[OOP - Lec 13,14,15] Constructors / Destructor and its Types
[OOP - Lec 13,14,15] Constructors / Destructor and its Types
 
Constructor and Destructor.pdf
Constructor and Destructor.pdfConstructor and Destructor.pdf
Constructor and Destructor.pdf
 
Constructors in C++.pptx
Constructors in C++.pptxConstructors in C++.pptx
Constructors in C++.pptx
 
Constructors and Destructor in C++
Constructors and Destructor in C++Constructors and Destructor in C++
Constructors and Destructor in C++
 
Constructor and Destructor
Constructor and DestructorConstructor and Destructor
Constructor and Destructor
 
constructorsfjy5ediykEASFul;IUWORHusi;gfb.pptx
constructorsfjy5ediykEASFul;IUWORHusi;gfb.pptxconstructorsfjy5ediykEASFul;IUWORHusi;gfb.pptx
constructorsfjy5ediykEASFul;IUWORHusi;gfb.pptx
 
Constructors.16
Constructors.16Constructors.16
Constructors.16
 
Constructor and destructor
Constructor and destructorConstructor and destructor
Constructor and destructor
 
C#2
C#2C#2
C#2
 
Class introduction in java
Class introduction in javaClass introduction in java
Class introduction in java
 
C# classes objects
C#  classes objectsC#  classes objects
C# classes objects
 
Learning C++ - Class 4
Learning C++ - Class 4Learning C++ - Class 4
Learning C++ - Class 4
 
Constructors in JAva.pptx
Constructors in JAva.pptxConstructors in JAva.pptx
Constructors in JAva.pptx
 
CONSTRUCTORS IN C++ +2 COMPUTER SCIENCE
CONSTRUCTORS IN C++ +2 COMPUTER SCIENCECONSTRUCTORS IN C++ +2 COMPUTER SCIENCE
CONSTRUCTORS IN C++ +2 COMPUTER SCIENCE
 

Recently uploaded

Solutons Maths Escape Room Spatial .pptx
Solutons Maths Escape Room Spatial .pptxSolutons Maths Escape Room Spatial .pptx
Solutons Maths Escape Room Spatial .pptx
spdendr
 
How to deliver Powerpoint Presentations.pptx
How to deliver Powerpoint  Presentations.pptxHow to deliver Powerpoint  Presentations.pptx
How to deliver Powerpoint Presentations.pptx
HajraNaeem15
 
Walmart Business+ and Spark Good for Nonprofits.pdf
Walmart Business+ and Spark Good for Nonprofits.pdfWalmart Business+ and Spark Good for Nonprofits.pdf
Walmart Business+ and Spark Good for Nonprofits.pdf
TechSoup
 
NEWSPAPERS - QUESTION 1 - REVISION POWERPOINT.pptx
NEWSPAPERS - QUESTION 1 - REVISION POWERPOINT.pptxNEWSPAPERS - QUESTION 1 - REVISION POWERPOINT.pptx
NEWSPAPERS - QUESTION 1 - REVISION POWERPOINT.pptx
iammrhaywood
 
Temple of Asclepius in Thrace. Excavation results
Temple of Asclepius in Thrace. Excavation resultsTemple of Asclepius in Thrace. Excavation results
Temple of Asclepius in Thrace. Excavation results
Krassimira Luka
 
Liberal Approach to the Study of Indian Politics.pdf
Liberal Approach to the Study of Indian Politics.pdfLiberal Approach to the Study of Indian Politics.pdf
Liberal Approach to the Study of Indian Politics.pdf
WaniBasim
 
LAND USE LAND COVER AND NDVI OF MIRZAPUR DISTRICT, UP
LAND USE LAND COVER AND NDVI OF MIRZAPUR DISTRICT, UPLAND USE LAND COVER AND NDVI OF MIRZAPUR DISTRICT, UP
LAND USE LAND COVER AND NDVI OF MIRZAPUR DISTRICT, UP
RAHUL
 
ANATOMY AND BIOMECHANICS OF HIP JOINT.pdf
ANATOMY AND BIOMECHANICS OF HIP JOINT.pdfANATOMY AND BIOMECHANICS OF HIP JOINT.pdf
ANATOMY AND BIOMECHANICS OF HIP JOINT.pdf
Priyankaranawat4
 
spot a liar (Haiqa 146).pptx Technical writhing and presentation skills
spot a liar (Haiqa 146).pptx Technical writhing and presentation skillsspot a liar (Haiqa 146).pptx Technical writhing and presentation skills
spot a liar (Haiqa 146).pptx Technical writhing and presentation skills
haiqairshad
 
Beyond Degrees - Empowering the Workforce in the Context of Skills-First.pptx
Beyond Degrees - Empowering the Workforce in the Context of Skills-First.pptxBeyond Degrees - Empowering the Workforce in the Context of Skills-First.pptx
Beyond Degrees - Empowering the Workforce in the Context of Skills-First.pptx
EduSkills OECD
 
คำศัพท์ คำพื้นฐานการอ่าน ภาษาอังกฤษ ระดับชั้น ม.1
คำศัพท์ คำพื้นฐานการอ่าน ภาษาอังกฤษ ระดับชั้น ม.1คำศัพท์ คำพื้นฐานการอ่าน ภาษาอังกฤษ ระดับชั้น ม.1
คำศัพท์ คำพื้นฐานการอ่าน ภาษาอังกฤษ ระดับชั้น ม.1
สมใจ จันสุกสี
 
Main Java[All of the Base Concepts}.docx
Main Java[All of the Base Concepts}.docxMain Java[All of the Base Concepts}.docx
Main Java[All of the Base Concepts}.docx
adhitya5119
 
Advanced Java[Extra Concepts, Not Difficult].docx
Advanced Java[Extra Concepts, Not Difficult].docxAdvanced Java[Extra Concepts, Not Difficult].docx
Advanced Java[Extra Concepts, Not Difficult].docx
adhitya5119
 
The basics of sentences session 6pptx.pptx
The basics of sentences session 6pptx.pptxThe basics of sentences session 6pptx.pptx
The basics of sentences session 6pptx.pptx
heathfieldcps1
 
Your Skill Boost Masterclass: Strategies for Effective Upskilling
Your Skill Boost Masterclass: Strategies for Effective UpskillingYour Skill Boost Masterclass: Strategies for Effective Upskilling
Your Skill Boost Masterclass: Strategies for Effective Upskilling
Excellence Foundation for South Sudan
 
Leveraging Generative AI to Drive Nonprofit Innovation
Leveraging Generative AI to Drive Nonprofit InnovationLeveraging Generative AI to Drive Nonprofit Innovation
Leveraging Generative AI to Drive Nonprofit Innovation
TechSoup
 
ISO/IEC 27001, ISO/IEC 42001, and GDPR: Best Practices for Implementation and...
ISO/IEC 27001, ISO/IEC 42001, and GDPR: Best Practices for Implementation and...ISO/IEC 27001, ISO/IEC 42001, and GDPR: Best Practices for Implementation and...
ISO/IEC 27001, ISO/IEC 42001, and GDPR: Best Practices for Implementation and...
PECB
 
A Independência da América Espanhola LAPBOOK.pdf
A Independência da América Espanhola LAPBOOK.pdfA Independência da América Espanhola LAPBOOK.pdf
A Independência da América Espanhola LAPBOOK.pdf
Jean Carlos Nunes Paixão
 
BBR 2024 Summer Sessions Interview Training
BBR  2024 Summer Sessions Interview TrainingBBR  2024 Summer Sessions Interview Training
BBR 2024 Summer Sessions Interview Training
Katrina Pritchard
 
Présentationvvvvvvvvvvvvvvvvvvvvvvvvvvvv2.pptx
Présentationvvvvvvvvvvvvvvvvvvvvvvvvvvvv2.pptxPrésentationvvvvvvvvvvvvvvvvvvvvvvvvvvvv2.pptx
Présentationvvvvvvvvvvvvvvvvvvvvvvvvvvvv2.pptx
siemaillard
 

Recently uploaded (20)

Solutons Maths Escape Room Spatial .pptx
Solutons Maths Escape Room Spatial .pptxSolutons Maths Escape Room Spatial .pptx
Solutons Maths Escape Room Spatial .pptx
 
How to deliver Powerpoint Presentations.pptx
How to deliver Powerpoint  Presentations.pptxHow to deliver Powerpoint  Presentations.pptx
How to deliver Powerpoint Presentations.pptx
 
Walmart Business+ and Spark Good for Nonprofits.pdf
Walmart Business+ and Spark Good for Nonprofits.pdfWalmart Business+ and Spark Good for Nonprofits.pdf
Walmart Business+ and Spark Good for Nonprofits.pdf
 
NEWSPAPERS - QUESTION 1 - REVISION POWERPOINT.pptx
NEWSPAPERS - QUESTION 1 - REVISION POWERPOINT.pptxNEWSPAPERS - QUESTION 1 - REVISION POWERPOINT.pptx
NEWSPAPERS - QUESTION 1 - REVISION POWERPOINT.pptx
 
Temple of Asclepius in Thrace. Excavation results
Temple of Asclepius in Thrace. Excavation resultsTemple of Asclepius in Thrace. Excavation results
Temple of Asclepius in Thrace. Excavation results
 
Liberal Approach to the Study of Indian Politics.pdf
Liberal Approach to the Study of Indian Politics.pdfLiberal Approach to the Study of Indian Politics.pdf
Liberal Approach to the Study of Indian Politics.pdf
 
LAND USE LAND COVER AND NDVI OF MIRZAPUR DISTRICT, UP
LAND USE LAND COVER AND NDVI OF MIRZAPUR DISTRICT, UPLAND USE LAND COVER AND NDVI OF MIRZAPUR DISTRICT, UP
LAND USE LAND COVER AND NDVI OF MIRZAPUR DISTRICT, UP
 
ANATOMY AND BIOMECHANICS OF HIP JOINT.pdf
ANATOMY AND BIOMECHANICS OF HIP JOINT.pdfANATOMY AND BIOMECHANICS OF HIP JOINT.pdf
ANATOMY AND BIOMECHANICS OF HIP JOINT.pdf
 
spot a liar (Haiqa 146).pptx Technical writhing and presentation skills
spot a liar (Haiqa 146).pptx Technical writhing and presentation skillsspot a liar (Haiqa 146).pptx Technical writhing and presentation skills
spot a liar (Haiqa 146).pptx Technical writhing and presentation skills
 
Beyond Degrees - Empowering the Workforce in the Context of Skills-First.pptx
Beyond Degrees - Empowering the Workforce in the Context of Skills-First.pptxBeyond Degrees - Empowering the Workforce in the Context of Skills-First.pptx
Beyond Degrees - Empowering the Workforce in the Context of Skills-First.pptx
 
คำศัพท์ คำพื้นฐานการอ่าน ภาษาอังกฤษ ระดับชั้น ม.1
คำศัพท์ คำพื้นฐานการอ่าน ภาษาอังกฤษ ระดับชั้น ม.1คำศัพท์ คำพื้นฐานการอ่าน ภาษาอังกฤษ ระดับชั้น ม.1
คำศัพท์ คำพื้นฐานการอ่าน ภาษาอังกฤษ ระดับชั้น ม.1
 
Main Java[All of the Base Concepts}.docx
Main Java[All of the Base Concepts}.docxMain Java[All of the Base Concepts}.docx
Main Java[All of the Base Concepts}.docx
 
Advanced Java[Extra Concepts, Not Difficult].docx
Advanced Java[Extra Concepts, Not Difficult].docxAdvanced Java[Extra Concepts, Not Difficult].docx
Advanced Java[Extra Concepts, Not Difficult].docx
 
The basics of sentences session 6pptx.pptx
The basics of sentences session 6pptx.pptxThe basics of sentences session 6pptx.pptx
The basics of sentences session 6pptx.pptx
 
Your Skill Boost Masterclass: Strategies for Effective Upskilling
Your Skill Boost Masterclass: Strategies for Effective UpskillingYour Skill Boost Masterclass: Strategies for Effective Upskilling
Your Skill Boost Masterclass: Strategies for Effective Upskilling
 
Leveraging Generative AI to Drive Nonprofit Innovation
Leveraging Generative AI to Drive Nonprofit InnovationLeveraging Generative AI to Drive Nonprofit Innovation
Leveraging Generative AI to Drive Nonprofit Innovation
 
ISO/IEC 27001, ISO/IEC 42001, and GDPR: Best Practices for Implementation and...
ISO/IEC 27001, ISO/IEC 42001, and GDPR: Best Practices for Implementation and...ISO/IEC 27001, ISO/IEC 42001, and GDPR: Best Practices for Implementation and...
ISO/IEC 27001, ISO/IEC 42001, and GDPR: Best Practices for Implementation and...
 
A Independência da América Espanhola LAPBOOK.pdf
A Independência da América Espanhola LAPBOOK.pdfA Independência da América Espanhola LAPBOOK.pdf
A Independência da América Espanhola LAPBOOK.pdf
 
BBR 2024 Summer Sessions Interview Training
BBR  2024 Summer Sessions Interview TrainingBBR  2024 Summer Sessions Interview Training
BBR 2024 Summer Sessions Interview Training
 
Présentationvvvvvvvvvvvvvvvvvvvvvvvvvvvv2.pptx
Présentationvvvvvvvvvvvvvvvvvvvvvvvvvvvv2.pptxPrésentationvvvvvvvvvvvvvvvvvvvvvvvvvvvv2.pptx
Présentationvvvvvvvvvvvvvvvvvvvvvvvvvvvv2.pptx
 

Oops

  • 1. Inheritance using System; public class Parent { string parentString; public Parent() { Console.WriteLine("Parent Constructor."); } public Parent(string myString) { parentString = myString; Console.WriteLine(parentString); } public void print() { Console.WriteLine("I'm a Parent Class."); } } .
  • 2. public class Child : Parent { public Child() : base("From Derived") { Console.WriteLine("Child Constructor."); } public new void print() { base.print(); Console.WriteLine("I'm a Child Class."); } public static void Main() { Child child = new Child(); child.print(); } }
  • 3. Output: From Derived Child Constructor. I'm a Parent Class. I'm a Child Class.
  • 4. Class and its members in C#.NET • To create a class, use the keyword class and has the following syntax. • [Access Modifier] class ClassName • { • - • - • - • } • A class can be created with only two access modifiers, public and internal. Default is public. When a class is declared as public, it can be accessed within the same assembly in which it was declared as well as from out side the assembly. But when the class is created as internal then it can be accessed only within the same assembly in which it was declared.
  • 5. Class and its members in C#.NET Members of a Class A class can have any of the following members. • Fields • Properties • Methods • Events • Constructors • Destructor • Operators • Indexers • Delegates • Fields : A field is the variable created within the class and it is used to store data of the class. In general fields will be private to the class for providing security for the data • Syntax : [Access Modifier] DataType Fieldname;
  • 6. Class and its members in C#.NET • Properties : A property is a method of the class that appears to the user as a field of the class. Properties are used to provide access to the private fields of the class. In general properties will be public. • Syntax : [Access Modifier] DataType PropName • { • Get • { • } • Set • { • } • } • A property contains two accessors, get and set.
  • 7. Class and its members in C#.NET • When user assigns a value to the property, the set accessor of the property will be automatically invoked AND VALUE ASSIGNED to the property will be passed to set accessor with the help of an implicit object called value. Hence set accessor is used to set the value to private field. Within the set accessor you can perform validation on value before assigning it to the private field. When user reads a property, then the get accessor of the property is automatically invoked. Hence get accessor is used to write the code to return the value stored in private field. • Readonly Property : There may be a situation where you want to allow the user to read the property and not to assign a value to the property. In this case property has to be created as readonly property and for this create the property only with get accessor without set accessor. • Syntax : [Access Modifier] DataType PropName • { • Get • { • } • }
  • 8. Class and its members in C#.NET • Writeonly Property : There may be a situation where you want to allow the user to assign a value to the property and not to read the property. In this case property has to be created as writeonly property and for this create the property only with set accessor without get accessor. • Syntax : [Access Modifier] DataType PropName • { • Set • { • } • } • Methods • Methods are nothing but functions created within the class. Functions are used to specify various operations that can be performed on data represented by the class.
  • 9. Class and its members in C#.NET • class MyClass { private int x; public void SetX(int i) { x = i; } public int GetX() { return x; } } class MyClient { public static void Main() { MyClass mc = new MyClass(); mc.SetX(10); int xVal = mc.GetX(); Console.WriteLine(xVal);//Displays 10 } }
  • 10. Example program for property and method of a class • using System; namespace example { class programCall { int n; //instance variable //property nn to access private field n public int nn { set { n = value; } get { return n; } }
  • 11. Example program for property and method of a class • //declare a method to print field n value //by default members of class will be private , so to access explictly declare public public void print() { Console.WriteLine(n); } } class MainClass { static void Main(string[] args) { //creating object to class programcall programCall objpc = new programCall();
  • 12. Example program for property and method of a class • //instance members can only be accessed with an instance objpc.nn = 99999; objpc.print(); Console.Read(); } } } • Output 99999
  • 13. Constructor in C#.NET • A special method of the class that will be automatically invoked when an instance of the class is created is called as constructor. Constructors are mainly used to initialize private fields of the class while creating an instance for the class. When you are not creating a constructor in the class, then compiler will automatically create a default constructor in the class that initializes all numeric fields in the class to zero and all string and object fields to null. To create a constructor, create a method in the class with same name as class and has the following syntax. [Access Modifier] ClassName([Parameters]) • { • } •
  • 14. Constructor in C#.NET • Example program using Constructors using System; class ProgramCall { int i, j; //default contructor public ProgramCall() { i = 45; j = 76; }
  • 15. Constructor in C#.NET • public static void Main() { //When an object is created , contructor is called ProgramCall obj = new ProgramCall(); Console.WriteLine(obj.i); Console.WriteLine(obj.j); Console.Read(); } } • OUTPUT 45 76
  • 16. Constructor types with example programs in C#.NET • A special method of the class that will be automatically invoked when an instance of the class is created is called as constructor. Constructors can be classified • Default Constructor • Parameterized Constructor • Copy Constructor • Private Constructor
  • 17. Constructor types • Default Constructor : A constructor without any parameters is called as default constructor. Drawback of default constructor is every instance of the class will be initialized to same values and it is not possible to initialize each instance of the class to different values. Example for Default Constructor Parameterized Constructor : A constructor with at least one parameter is called as parameterized constructor. Advantage of parameterized constructor is you can initialize each instance of the class to different values. Example for Parameterized Constructor • using System; namespace ProgramCall { class Test1 { //Private fields of class int A, B;
  • 18. Constructor types • //default Constructor public Test1() { A = 10; B = 20; } //Paremetrized Constructor public Test1(int X, int Y) { A = X; B = Y; } //Method to print public void Print() { Console.WriteLine("A = {0}tB = {1}", A, B); } }
  • 19. Constructor types • class MainClass { static void Main() { Test1 T1 = new Test1(); //Default Constructor is called Test1 T2 = new Test1(80, 40); //Parameterized Constructor is called T1.Print(); T2.Print(); Console.Read(); } } } • Output A = 10 B = 20 A = 80 B = 40
  • 20. Constructor types • Copy Constructor : A parameterized constructor that contains a parameter of same class type is called as copy constructor. Main purpose of copy constructor is to initialize new instance to the values of an existing instance. Example for Copy Constructor using System; namespace ProgramCall { class Test2 { int A, B; public Test2(int X, int Y) { A = X; B = Y; }
  • 21. Constructor types • //Copy Constructor public Test2(Test2 T) { A = T.A; B = T.B; } public void Print() { Console.WriteLine("A = {0}tB = {1}", A, B); } } class CopyConstructor { static void Main() {
  • 22. Constructor types • Test2 T2 = new Test2(80, 90); //Invoking copy constructor Test2 T3 = new Test2(T2); T2.Print(); T3.Print(); Console.Read(); } } } • Output A = 80 B = 90 A = 80 B = 90
  • 23. Constructor types Private Constructor : You can also create a constructor as private. When a class contains at least one private constructor, then it is not possible to create an instance for the class. Private constructor is used to restrict the class from being instantiated when it contains every member as static. Some unique points related to constructors are as follows • A class can have any number of constructors. • A constructor doesn’t have any return type even void. • A static constructor can not be a parameterized constructor. • Within a class you can create only one static constructor.
  • 24. Destructor in C#.NET • A destructor is a special method of the class that is automatically invoked while an instance of the class is destroyed. Destructor is used to write the code that needs to be executed while an instance is destroyed. To create a destructor, create a method in the class with same name as class preceded with ~ symbol. Syntax : ~ClassName() { • } Example : The following example creates a class with one constructor and one destructor. An instance is created for the class within a function and that function is called from main. As the instance is created within the function, it will be local to the function and its life time will be expired immediately after execution of the function was completed.
  • 25. Destructor in C#.NET • using System; namespace ProgramCall { class myclass { public myclass() { Console.WriteLine("An Instance Created"); } //destructor ~myclass() { Console.WriteLine("An Instance Destroyed"); } }
  • 26. Destructor in C#.NET • class Destructor { public static void Create() { myclass T = new myclass(); } static void Main() { Create(); GC.Collect(); Console.Read(); } } } • Output An Instance Created An Instance Destroyed
  • 27. Polymorphism in C# • When a message can be processed in different ways is called polymorphism. Polymorphism means many forms. • Polymorphism is one of the fundamental concepts of OOP. • Polymorphism provides following features: • It allows you to invoke methods of derived class through base class reference during runtime. • It has the ability for classes to provide different implementations of methods that are called through the same name. • Polymorphism is of two types: • Compile time polymorphism/Overloading • Runtime polymorphism/Overriding • Compile Time Polymorphism • Compile time polymorphism is  method and  operators overloading. • In method overloading method performs the different task at the different input parameters.
  • 28. Polymorphism in C# • Runtime Time Polymorphism • Runtime time polymorphism is done using inheritance and virtual functions. Method overriding is called runtime polymorphism. It is also called late binding. • When overriding a method, you change the behavior of the method for the derived class. Overloading a method simply involves having another method with the same prototype. • Caution: Don't confused method overloading with method overriding, they are different, unrelated concepts. But they sound similar. • Method overloading has nothing to do with inheritance or virtual methods. • Following are examples of methods having different overloads: • void area(int side); • void area(int l, int b); • void area(float radius);
  • 29. Polymorphism in C# • Practical example of Method Overloading (Compile Time Polymorphism) • using System; • namespace method_overloading • { • class Program • { • public class Print • { • public void display(string name) • { • Console.WriteLine("Your name is : " + name); • } • public void display(int age, float marks) • { • Console.WriteLine("Your age is : " + age); • Console.WriteLine("Your marks are :" + marks); • }
  • 30. Polymorphism in C# • } • static void Main(string[] args) • { • Print obj = new Print(); • obj.display("George"); • obj.display(34, 76.50f); • Console.ReadLine(); • } • } • } • Note: In the code if you observe display method is called two times. Display method will work according to the number of parameters and type of parameters.
  • 31. Polymorphism in C# • When and why to use method overloading • Use method overloading in situation where you want a class to be able to do something, but there is more than one possibility for what information is supplied to the method that carries out the task. • You should consider overloading a method when you for some reason need a couple of methods that take different parameters, but conceptually do the same thing. • Method Overloading showing many forms. • using System; • namespace method_overloading_polymorphism • { • class Program • { • public class Shape • { • public void Area(float r) • { • float a = (float)3.14 * r; • // here we have used funtion overload with 1 parameter. • Console.WriteLine("Area of a circle: {0}",a); • }
  • 32. Polymorphism in C# • public void Area(float l, float b) • { • float x = (float)l* b; • // here we have used funtion overload with 2 parameters. • Console.WriteLine("Area of a rectangle: {0}",x); • } • public void Area(float a, float b, float c) • { • float s = (float)(a*b*c)/2; • // here we have used funtion overload with 3 parameters. • Console.WriteLine("Area of a circle: {0}", s); • } • }
  • 33. Polymorphism in C# • static void Main(string[] args) • { • Shape ob = new Shape(); • ob.Area(2.0f); • ob.Area(20.0f,30.0f); • ob.Area(2.0f,3.0f,4.0f); • Console.ReadLine(); • } • } • } • Things to keep in mind while method overloading • If you use overload for method, there are couple of restrictions that the compiler imposes. • The rule is that overloads must be different in their signature, which means the name and the number and type of parameters. • There is no limit to how many overload of a method you can have. You simply declare them in a class, just as if they were different methods that happened to have the same name.
  • 34. Method overriding • class BaseClass { public virtual string YourCity() { return "New York"; } } class DerivedClass : BaseClass { public override string YourCity() { return "London"; } } class Program { static void Main(string[] args) { DerivedClass obj = new DerivedClass(); string city = obj.YourCity(); Console.WriteLine(city); Console.Read(); } } } •