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.
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
Post a Comment