Sealed, Abstract classes and Interface in C#

Working with sealed classes: 

1)sealed is a keyword 

2)sealed classes are not inheritable sealed class A {} 

3)when ever a class is providing complete functionality as per the requirements, Then it is recommended to declare that class as sealed

 

Working with abstract classes, abstract methods and interface 

1)abstract &interface are keywords 

2)abstract classes & interfaces are 99% same 

3)abstract keyword can be used with the method & classes, A method without body is called an abstract method. 

4)syntax: public abstract void find Area(); 

5)A method with body is called as non-abstract method

 

Syntax is: 

     public void print() 

     { 

          //Logic; 

     }

 

6)abstract methods are also called as RULES 

7)whenever a class contains atleast one abstract method, then recommended to declare that class as Abstract 

8)interface contains only abstract methods 

9)whenever a class is not proving complete functionality, then declare that class as abstract 

10)all the abstract methods must be overrided 

11)abstract classes & interfaces are not instantitable but a reference can be created

 

            Test t=new Test();  //object 

             Shape s;   //reference

 

Object contains its own memory, Reference does not contains its own memory

 

12)reference works with the help of dependent memory (or) child class memory

 

           Shape s=new circle();

 

Example on abstract classes & abstract methods:

Open windows forms application project: 

Start->programs->Microsoft visual studio 2010->Microsoft Visual studio 2010->file menu->new-> 

project->select visual c# from installed templates->select windows forms application project     

 

open toolbox [view->toolbox] 

place a button (double click on button)

 

using System; 

using System.Collections.Generic; 

using System.ComponentModel; 

using System.Data; 

using System.Drawing; 

using System.Linq; 

using System.Text; 

using System.Windows.Forms;

 

namespace WindowsFormsApplication5 

{ 

    public partial class Form1 : Form 

    { 

        public Form1() 

        { 

            InitializeComponent(); 

        }

 

        abstract class shape 

        { 

            private int x=10,y=20; 

            public void print() //non-abstract method 

            { 

                MessageBox.Show(x+" "+y); 

            }

 

            public abstract void findraea(); 

        }

 

        //shape 

        class circle:shape 

        { 

            private int r=123; 

            public override void findarea() 

            { 

                   double a=3.14*r*r; 

                   MessageBox.Show("area is :"+a); 

            } 

       

           public void display() 

           { 

               MessageBox.Show("thanks"); 

           } 

    }  

 

   //circle

 

      private void button1_Click(object sender, EventArgs e) 

      {

            circle c = new circle(); 

            c.print(); 

            c.findarea(); 

            c.display();

 

            shape s = new circle(); 

            s.print(); 

            s.findarea();

        } 

    } 

}

 

Go to F5

 

Working with partial classes

 

1)partial is a keyword 

2)partial keyword is introduced from .NET 2.0 

3)when ever a class need to be implemented at more than one locations with same name. Then those classes need to be declared as partial

 

Syntax: 

 

partial class Emp 

{ 

  public void p1() {} 

}

 

partial class Emp 

{ 

   public void p2() {} 

}

 

4)at runtime, These partial classes will be merged together and works like a single class 

    { 

       Emp e1=new Emp(); 

    }

 

Working with GUI based applications (windows programming) 

1)working with form and controls is called as windows programming 

2)in windows programming, The most essential control(container) is FORM 

3)form is also called as winform 

 

Q)what is FORM? 

1)FORM works like a container, Where any number of controls can be placed 

2)WINFORM must be a class & which must be inherited from a predefined class called as FORM

 

Q)can I create my own form at runtime? 

A)yes

 

Code in GeneralDescription

 

class Test:Form 

{ 

     …. 

}

 

code for button1-click

 

  Test t=new Test(); 

  t.Show();

 

Q)when I place a control on the Form, Then internally what happens? 

a)1.All the controls are predefined classes 

2)when ever a control is placed on the form, Then an object will be created for that controls class 

3)This created object will be stored in Form1.Designer.cs file 

4)for every winform,3 link files will be created.