Skip to main content

Session 4:- Constructor in c#




Date
Wednesday, December 04, 2013
Time
Description
10.00 am to 12.00 pm
Constructor, Destructor, Inheritance, Constructor in inheritance
Learning:
It is same as that of function but difference is that it does not have any return type.
           Its name is same as that of the class, it cannot be inherited.
Constructor of a class is expected when object of the class is created usually we put initialisation code in the constructor.

Eg. Public class myclass
{
   Public myclass()
  {
     //constructor body or initialisation code
  }
}

C # supports constructor overloading, i.e we can have constructor with different set of parameter for same class

Eg. 


1:  Public class myclass  
2:  {  
3:    Public myclass()  
4:    {  
5:       //default constructor  
6:    }  
7:   Public myclass(int a, int b)  
8:   {  
9:    //parametrised condtructor  
10:   }  
11:  Class program  
12:  {  
13:    Static void main(string[] args)  
14:    {  
15:        myclass m=new myclass();// calling default constructor  
16:        myclass m1=new myclass(10,20);//calling parametrised constructor  
17:    }  
18:  }  
Static constructor:-
It gets called before the first object is created of the class, it could be at the time of loading the assembly.

Eg. Public class myclass
{
  Static myclass()
  {
          //static constructor body
   }
}

Partial class:-

It allows a single class to be divided into two separate physical files. During compile time these files are compiled into a single class.

Eg.
 Public partial class customer
{
   Public void add()
  {
         
  }
}//customer1.cs

Public partial class customer
{
  Public void delete()
   {

   }
}//customer3.cs

Inheritance:-
Inheritance is deriving sub class from the base class i.e child inherits all or some properties from super class.
  Hierarchy is used for defining specialised classes based on pre-existing generalised classes.

       1) Single inheritance :- one class is derived from another class.
Eg. Class a                                  
{

}

Class b:a
            {                        
                                              
//:  means inheritance.
}  

       2) Multilevel  inheritance:- One class is derived from another class and this derived class again derived by the third class.
Eg. Class a                                
        {

        }
       Class b:a                                               
         {
                                                                      
        }
      Class c:b
        {

       }

 

Note:- Multiple inheritance is not used in c#, Interfaces are used.

Program:-
Class any
{
  Public int a;
  Public void getany()
  {
    Console.writeline(“method getany in class any”);
  }
}
Class other:any
{
  Public void getother()
   {
     Console.writeline(“method getother in class other”);
  } 

}
Public class anyother:other
{
  Public void getanyother()
  {
    Console.writeline(“method getany other of class anyother”);
  }
}

Class program
{
   Static void main(string[] args)
  {
      Other b= new other();
       b.getany();
       anyother a=new anyother();
        a.getother();
        a.getanyother();
   }
}

Sealed keyword:-
   If we want that our class should not be inherited then we can use sealed keyword.

Eg:- sealed class password
{
  //body of the class
}


Polymorphism:-

Different objects acts differently on the same message. Polymorphism is a greek letter where “poly” means many and “morphism” means forms.

Eg:- An inbuilt addition operation which can add integer alues as well as floating values.

Types of polymorphism:-
1)      Runtime polymorphism
2)      Compile time polymorphism

Runtime polymorphism:-   if we have to call the function of derived class with the object of derived class then we have to use runtime polymorphism.
   It can be achieved by using keyword virtual. If a function is made virtual in base class then there is no compulsion that function should be overided in derived class.

Program:- runtime polymorphism(illustrate the use of virtual keyword)

Class baseclass
{
   Public virtual void show()
  {
    Console.writeline(“I am in base class”);
  }
}
Class derived:baseclass
{
   Public override void show()
   {
      Console.writeline(“I am in derived class”);
   }
}

Class derived2:derived
 {
     Public override void show()
     {
          Console.writeline(“I am in derived2 class”);
     }
  }

Class program
{
  Static void main(string[] args)
  {
     baseclass cbase=new baseclass();
     cbase=new derived1();
     base.show();
     base=new derived();
    base.show1();

  }
}




Comments

Popular posts from this blog

Session 3 :- Control Structures in c#

Date Friday, November 29, 2013 Time Description 10.00 am to 12.00 pm Control structure in c# Learning :   For Loops Why is this important to learn about? Control structures are one of the most fundamental concepts in any programming language you will come across. If you want to know C#, and if you want to learn to program with any language, you must have a firm grip on the control structures of the language. Definitions of terms used . Iteration – This is the act of repeating something (that ‘something’ being code statements in the programming context). Conditional – A conditional action is said to be one that is only performed if a certain condition is true. For Loops For loops allow us to specify the number of times to repeat a block of code. It is best demonstrated with an example: for (int i = 1; i <= 5; i++) {    Console...
Oop's Concept .Net is purely object oriented Language because it strongly support four major pillars  object oriented language.   four Major Pillars are : -      1) Abstraction.      2) Encapsulation      3) Inheritance      4) Polymorphism .Net  also support three minor pillars : -     1) Strongly Type Cast     2) Persistent     3) Concurrency *** ABSTRACTION ***        In simple words  “Abstraction captures only those details about an object that are relevant to the current perspective.”   In object-oriented programming theory, abstraction involves the facility to define objects that represent abstract “actors” that can perform work, report on and change their state, and “communicate” with other objects in the system . Abstraction is a concept which hide complexity. *** ENCAPSULATION ***      Encaps...

Inheritance in c#

Inheritance is one of the pillars of Object Oriented Programming. It allows the child class to reuse, extend and modify the behavior of parent class. The class whose members are inherited is called as base class and class who inherits is called a derived class. We can inherit only one class at a time. Multiple inheritance is not supported in c#. A struct cannot inherit any base class doing so will generate a compiler error. 1.       Private members are only accessible to the derived class which is nested in the inherited base class. 2.       Protected members are accessible only in derived class. 3.       Internal members are accessible in the derived class that is visible in the same assembly of the base class. 4.       Public members are accessible in the derived class. They can be called just as if they were declared in the derived class. All Types are implicitly inherited ...