Thursday, 13 October 2016

C# Tutorial Part 2

Class
Class are template of an object .It contain data , method , function to make the object usable .For example , we create an object “student” , student have some property , like student name ,date of birth ,roll number ect .More over student have an property also called  parent name . Then how we should describe “student” object or class ?


class student
{
    string student_name;
    DateTime date_of_birth;
    int roll_number;
    string parent_name;
}


Now , the student class properly describe the student .But class also contain method, function for specific work. These are called member of the class.
class student
{
    string student_name;
    DateTime date_of_birth;
    int roll_number;
    string parent_name;
    string parent_name;
    void calculate_age()
    {
    }
}

Now, student_name , date_of_birth, roll_number,parent_name, calculate_age all are member of the class .How we will go some other property of the class. One property is nested type. That means one class have been nested under another class . We have already declared how to declare class and we have declare the class “student” . Now we will declare the “student” class under a class called “school”.

class school
{
    string name_of_school;
    string address_of_school;

    class student
    {
        string student_name;
        DateTime date_of_birth;
        int roll_number;
        string parent_name;
        string parent_name;
        void calculate_age()
        {
        }
    }
}


Now , the next comes , how to use class ?.To use an class we need to create instance of the class .Each class has an constructor , we need to initialized the constructor. For Example
student obj = new student();

To understand the concept , we need some modification of the class , just declare public , we will get all the property of the object instance.
public class student
{
    public string student_name;
    public DateTime date_of_birth;
    public int roll_number;
    public string parent_name;
    public string parent_name;
    public void calculate_age()
    {
    }
}

student obj = new student();

But why I had declare public class and member public ? There is a discussion scope and access level. Generally there are three type of access modifier private , public , protected.Other than this , other are friend and protected friend.Now let us see the role of access modifier.
Public : Can be access from any where.
Private : Can be access by the member of the same class only.
Protected : can be acess by the member of the same class only and inherited from base class.
Friend: Can be access from , within the assembly.
Example :

public class student
{
     string student_name;
     DateTime date_of_birth;
     int roll_number;
     string parent_name;
     string parent_name;
     void calculate_age()
    {
    }
}
 private class student
{
     string student_name;
     DateTime date_of_birth;
     int roll_number;
     string parent_name;
     string parent_name;
     void calculate_age()
    {
    }
}


protected class student
{
     string student_name;
     DateTime date_of_birth;
     int roll_number;
     string parent_name;
     string parent_name;
     void calculate_age()
    {
    }
}

Inheritance
Inheritance is the feature of the .Net framework , which allow to create a new class with all the member of previously defined class .For example ,we have defined class A ,, now inherited class B . All member of class A will come to Clasas B automatically.The first class is called Base class an second class is called derived class . In other object oriented programming language allow unlimited level of inheritance  , but .Net frame work allow only 2nd level inheritance . The main advantage of inheritance is just copy/implement common functionality of Base class to derived clasas . Now I am creating an class
 

public class parent //base class
{
    public int public_member;
    public int public_method(int a, int b)
    {
        return (a + b);
    }
}

public class child : parent
{
    //derived class
}

Notice that , child class do not have any method .Now I am creating instance of the clind class.
        child obj = new child();

obj.public_member;
obj.public_method();
We get both member of the parent class in child class . Noticed that , member of the parent class declared public ,so that we can inherit it , only public , protected access modifier will be available during Inheritance .Here is an example ,

public class Parent
{
    private void Method1()
    {
    }
    protected void Method2()
    {
    }

}

public class child : Parent
{
    public void method_call()
    {
        this.method1();//compliler through error-inacccable due to protection level
        this.Method2.method2();//will compile & work
    }
}

Is there any way , that we can define a class that can not be Inherited ?
Yes . there is , the keyword called “sealed” , seal keyword tells compiler that , this class can not be inherted  . For example
public sealed class  Myclass
{

}
Now , I am trying to Inherit the clasas “MyClasas”
public class MyChild : Myclass
{
}
//error MyChild': cannot derive from sealed type 'Myclass'

An error will shown , sealed class can not be inherited , but we can create instance of the seal class .The question is , what is the use of sealed class ?
Sealed class is mainly used for security features, so that class cannot be modified.



No comments:

Post a Comment

বাঙালির বেড়ানো সেরা চারটি ঠিকানা

  বাঙালি মানে ঘোড়া পাগল | দু একদিন ছুটি পেলো মানে বাঙালি চলল ঘুরতে | সে সমুদ্রই হোক , পাহাড়ি হোক বা নদী হোক। বাঙালির ...