SlideShare une entreprise Scribd logo
1  sur  35
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

Contenu connexe

Tendances

Tendances (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#
 

En vedette

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_08Niit 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 OOPRaju Dawadi
 
Generics in .NET, C++ and Java
Generics in .NET, C++ and JavaGenerics in .NET, C++ and Java
Generics in .NET, C++ and JavaSasha Goldshtein
 
9 subprograms
9 subprograms9 subprograms
9 subprogramsjigeno
 
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
 
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 4Leon Osinski
 
3963066 pl-sql-notes-only
3963066 pl-sql-notes-only3963066 pl-sql-notes-only
3963066 pl-sql-notes-onlyAshwin Kumar
 
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 guideOtto Paiz
 
16 logical programming
16 logical programming16 logical programming
16 logical programmingjigeno
 
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-solutionsAshwin Kumar
 
Software engineering lecture notes
Software engineering lecture notesSoftware engineering lecture notes
Software engineering lecture notesSiva 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
 

En vedette (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
 

Similaire à 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
 
Constructor
ConstructorConstructor
Constructor
 
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
 

Dernier

Oppenheimer Film Discussion for Philosophy and Film
Oppenheimer Film Discussion for Philosophy and FilmOppenheimer Film Discussion for Philosophy and Film
Oppenheimer Film Discussion for Philosophy and FilmStan Meyer
 
EMBODO Lesson Plan Grade 9 Law of Sines.docx
EMBODO Lesson Plan Grade 9 Law of Sines.docxEMBODO Lesson Plan Grade 9 Law of Sines.docx
EMBODO Lesson Plan Grade 9 Law of Sines.docxElton John Embodo
 
Activity 2-unit 2-update 2024. English translation
Activity 2-unit 2-update 2024. English translationActivity 2-unit 2-update 2024. English translation
Activity 2-unit 2-update 2024. English translationRosabel UA
 
Student Profile Sample - We help schools to connect the data they have, with ...
Student Profile Sample - We help schools to connect the data they have, with ...Student Profile Sample - We help schools to connect the data they have, with ...
Student Profile Sample - We help schools to connect the data they have, with ...Seán Kennedy
 
Integumentary System SMP B. Pharm Sem I.ppt
Integumentary System SMP B. Pharm Sem I.pptIntegumentary System SMP B. Pharm Sem I.ppt
Integumentary System SMP B. Pharm Sem I.pptshraddhaparab530
 
Presentation Activity 2. Unit 3 transv.pptx
Presentation Activity 2. Unit 3 transv.pptxPresentation Activity 2. Unit 3 transv.pptx
Presentation Activity 2. Unit 3 transv.pptxRosabel UA
 
How to Add Barcode on PDF Report in Odoo 17
How to Add Barcode on PDF Report in Odoo 17How to Add Barcode on PDF Report in Odoo 17
How to Add Barcode on PDF Report in Odoo 17Celine George
 
AUDIENCE THEORY -CULTIVATION THEORY - GERBNER.pptx
AUDIENCE THEORY -CULTIVATION THEORY -  GERBNER.pptxAUDIENCE THEORY -CULTIVATION THEORY -  GERBNER.pptx
AUDIENCE THEORY -CULTIVATION THEORY - GERBNER.pptxiammrhaywood
 
Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)
Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)
Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)lakshayb543
 
Inclusivity Essentials_ Creating Accessible Websites for Nonprofits .pdf
Inclusivity Essentials_ Creating Accessible Websites for Nonprofits .pdfInclusivity Essentials_ Creating Accessible Websites for Nonprofits .pdf
Inclusivity Essentials_ Creating Accessible Websites for Nonprofits .pdfTechSoup
 
Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17
Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17
Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17Celine George
 
Q4-PPT-Music9_Lesson-1-Romantic-Opera.pptx
Q4-PPT-Music9_Lesson-1-Romantic-Opera.pptxQ4-PPT-Music9_Lesson-1-Romantic-Opera.pptx
Q4-PPT-Music9_Lesson-1-Romantic-Opera.pptxlancelewisportillo
 
Expanded definition: technical and operational
Expanded definition: technical and operationalExpanded definition: technical and operational
Expanded definition: technical and operationalssuser3e220a
 
Concurrency Control in Database Management system
Concurrency Control in Database Management systemConcurrency Control in Database Management system
Concurrency Control in Database Management systemChristalin Nelson
 
USPS® Forced Meter Migration - How to Know if Your Postage Meter Will Soon be...
USPS® Forced Meter Migration - How to Know if Your Postage Meter Will Soon be...USPS® Forced Meter Migration - How to Know if Your Postage Meter Will Soon be...
USPS® Forced Meter Migration - How to Know if Your Postage Meter Will Soon be...Postal Advocate Inc.
 
Textual Evidence in Reading and Writing of SHS
Textual Evidence in Reading and Writing of SHSTextual Evidence in Reading and Writing of SHS
Textual Evidence in Reading and Writing of SHSMae Pangan
 
Transaction Management in Database Management System
Transaction Management in Database Management SystemTransaction Management in Database Management System
Transaction Management in Database Management SystemChristalin Nelson
 
Dust Of Snow By Robert Frost Class-X English CBSE
Dust Of Snow By Robert Frost Class-X English CBSEDust Of Snow By Robert Frost Class-X English CBSE
Dust Of Snow By Robert Frost Class-X English CBSEaurabinda banchhor
 

Dernier (20)

Oppenheimer Film Discussion for Philosophy and Film
Oppenheimer Film Discussion for Philosophy and FilmOppenheimer Film Discussion for Philosophy and Film
Oppenheimer Film Discussion for Philosophy and Film
 
EMBODO Lesson Plan Grade 9 Law of Sines.docx
EMBODO Lesson Plan Grade 9 Law of Sines.docxEMBODO Lesson Plan Grade 9 Law of Sines.docx
EMBODO Lesson Plan Grade 9 Law of Sines.docx
 
Activity 2-unit 2-update 2024. English translation
Activity 2-unit 2-update 2024. English translationActivity 2-unit 2-update 2024. English translation
Activity 2-unit 2-update 2024. English translation
 
Student Profile Sample - We help schools to connect the data they have, with ...
Student Profile Sample - We help schools to connect the data they have, with ...Student Profile Sample - We help schools to connect the data they have, with ...
Student Profile Sample - We help schools to connect the data they have, with ...
 
Integumentary System SMP B. Pharm Sem I.ppt
Integumentary System SMP B. Pharm Sem I.pptIntegumentary System SMP B. Pharm Sem I.ppt
Integumentary System SMP B. Pharm Sem I.ppt
 
Presentation Activity 2. Unit 3 transv.pptx
Presentation Activity 2. Unit 3 transv.pptxPresentation Activity 2. Unit 3 transv.pptx
Presentation Activity 2. Unit 3 transv.pptx
 
How to Add Barcode on PDF Report in Odoo 17
How to Add Barcode on PDF Report in Odoo 17How to Add Barcode on PDF Report in Odoo 17
How to Add Barcode on PDF Report in Odoo 17
 
AUDIENCE THEORY -CULTIVATION THEORY - GERBNER.pptx
AUDIENCE THEORY -CULTIVATION THEORY -  GERBNER.pptxAUDIENCE THEORY -CULTIVATION THEORY -  GERBNER.pptx
AUDIENCE THEORY -CULTIVATION THEORY - GERBNER.pptx
 
YOUVE GOT EMAIL_FINALS_EL_DORADO_2024.pptx
YOUVE GOT EMAIL_FINALS_EL_DORADO_2024.pptxYOUVE GOT EMAIL_FINALS_EL_DORADO_2024.pptx
YOUVE GOT EMAIL_FINALS_EL_DORADO_2024.pptx
 
Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)
Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)
Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)
 
Inclusivity Essentials_ Creating Accessible Websites for Nonprofits .pdf
Inclusivity Essentials_ Creating Accessible Websites for Nonprofits .pdfInclusivity Essentials_ Creating Accessible Websites for Nonprofits .pdf
Inclusivity Essentials_ Creating Accessible Websites for Nonprofits .pdf
 
Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17
Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17
Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17
 
Q4-PPT-Music9_Lesson-1-Romantic-Opera.pptx
Q4-PPT-Music9_Lesson-1-Romantic-Opera.pptxQ4-PPT-Music9_Lesson-1-Romantic-Opera.pptx
Q4-PPT-Music9_Lesson-1-Romantic-Opera.pptx
 
Expanded definition: technical and operational
Expanded definition: technical and operationalExpanded definition: technical and operational
Expanded definition: technical and operational
 
Concurrency Control in Database Management system
Concurrency Control in Database Management systemConcurrency Control in Database Management system
Concurrency Control in Database Management system
 
INCLUSIVE EDUCATION PRACTICES FOR TEACHERS AND TRAINERS.pptx
INCLUSIVE EDUCATION PRACTICES FOR TEACHERS AND TRAINERS.pptxINCLUSIVE EDUCATION PRACTICES FOR TEACHERS AND TRAINERS.pptx
INCLUSIVE EDUCATION PRACTICES FOR TEACHERS AND TRAINERS.pptx
 
USPS® Forced Meter Migration - How to Know if Your Postage Meter Will Soon be...
USPS® Forced Meter Migration - How to Know if Your Postage Meter Will Soon be...USPS® Forced Meter Migration - How to Know if Your Postage Meter Will Soon be...
USPS® Forced Meter Migration - How to Know if Your Postage Meter Will Soon be...
 
Textual Evidence in Reading and Writing of SHS
Textual Evidence in Reading and Writing of SHSTextual Evidence in Reading and Writing of SHS
Textual Evidence in Reading and Writing of SHS
 
Transaction Management in Database Management System
Transaction Management in Database Management SystemTransaction Management in Database Management System
Transaction Management in Database Management System
 
Dust Of Snow By Robert Frost Class-X English CBSE
Dust Of Snow By Robert Frost Class-X English CBSEDust Of Snow By Robert Frost Class-X English CBSE
Dust Of Snow By Robert Frost Class-X English CBSE
 

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(); } } } •