Thursday 28 February 2013

Need of Abstract classes and Abstract Methods

The concept of abstract classes is an extension to the concept of inheritance. In inheritance the parent classes can be defined with attributes that are commonly required under multiple child classes providing reusability.

FIGURE
  • Rectangle
  • Circle
  • Triangle
  • Cone
In the above case when we want to define different entities representing various figures like Rectangle, Traingle, Circle, Cone etc in such cases first we identify the attributes that are commonly required and put them under a parent class.
The class Figure as it is the parent provides the attributes commonly required to all figures given reusability. Along with this we can also define any abstract in the parent class figure so that the child classes will implement the abstract members which is a restriction for the child classes and cannot escape from it.
   

Created .aspx pages and Connected to the Database

Sudeep gave us a task to create two .aspx pages, and to link the first page with the second page and then asked to connect this pages with the Database. So that if once connected to the database we can Add, Update, Delete the data in the table that is stored in the database.
I have created one table GetJob in my database 'DB1' with the following columns and inserted some values to it:
  • JobCode
  • Vacancy
  • Post
  • Company
  • City
After creating a table I have added two web forms by selecting Asp.Net Empty Web Application in Visual Studio. The forms are named as Entry.aspx and JobDetails.aspx.
In Entry.aspx I have taken a Gridview Control(GridView is use to accept, display and edit data on a web page) and one Button(ADD).
Then I have provided a link to the ADD button that will transfer directly to second page i.e JobDetails.aspx wherein I have created one table with the same number of columns as in the GetJob table and then  within the table i have taken 5  labels and 5 text boxes. Along with that one button(SAVE) to save the data to the database. When we click to SAVE button it saves the data in the database. This I Have done through connectivity with SQL Server.
 Sudeep also told us to make the entire process more clear and asked us to do the following:
  •  Some validations the the text boxes.
  • Assign a Primary key to the first column of a table i.e'JobCode' and also it should generate automatic values once we switch to the form wherein Details need t o add.
  • With the Add link there should be two more links Update and Delete.
  • Instead of taking textbox towards the label 'City' there should be a DropDown List to give a chance to select one from many cities. 
  • After saving a JobDetails that page should redirect to the Entry.aspx page to add another Vacancies if required.

Interfaces

  • This is also a user defined type same as a class but can contain only abstract members in it.
  • The abstract members of an Interface will be implemented by a child class of  the Interface so here also the Interface is imposing restrictions on the child class.
  • A child can be inherited either from another class or an Interface also. When a class is inheriting from another class it can inherit only from one class (Single Inheritance). When class is inheriting from an Interface it can be inherited from any number of interfaces (Multiple Inheritance).
  •  Inheritance is divided into two categories
  1.   Implementation Inheritance
  2.   Interface Inheritance
  • If a class is inheriting from another class we call it as implementation Inheritance and this provides reusibility because child classes can consume their parent class members.
  • If a class is inheriting from an Interface we call it as Interface Inheritance but Interface Inheritance does not provide any reusibility because here the child c lass is only implementing the methods of its parent Interface.
Syntax:
[<modifiers>]  Interface <Name>    
{
 Abstract class declaration
}   

Rules while working with Interfaces
  • We can not declare variables under an interface.
  • Default scope for a member of an Interface is public where as it is private in case of class.
  • Every member of an Interface is by default abstract so we don't require to declare explicitly.
  • An Interface can inherit from another Interface if required.
Add an Interface item template under the project by choosing Interface from the add new item window and name it interface1.cs

interface Interface1
    {
        void add(int x, int y);
        void sub(int x, int y);
    }

Add another interface interface2.cs and write the following code

interface Interface2
    {
        void mul(int x, int y);
        void div(int x, int y);
    }

Now to implement the members of both these interfaces add a class interclass.cs and write the following code
       
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApps
{
    class interclass: Interface1,Interface2
    {
        public void add(int x, int y)
        {
            Console.WriteLine(x + y);
        }
        public void sub(int x, int y)
        {
            Console.WriteLine(x - y);
        }
        public void mul(int x, int y)
        {
            Console.WriteLine(x * y);
        }
        public void div(int x, int y)
        {
            Console.WriteLine(x / y);
        }
        static void Main()
        {
            interclass c = new interclass();
            c.add(100,45);
            c.sub(89,26);
            c.mul(12,34);
            c.div(456,23);
            Console.ReadLine();
        }
    }
}