Wednesday, 5 March 2014

Abstract Modifier

  • abstract keyword enables you to create classes and class members that are incomplete and must be implemented in a derived class
ABSTRACT CLASS

Purpose
The purpose of an abstract class is to provide a common definition of a base class that multiple derived classes can share.
Characteristics
They are classes that cannot be instantiated.
An abstract class contain either abstract methods or non abstract methods.
A non abstract class derived from abstract class MUST include actual implementation of all inherited members.

ABSTRACT METHOD
Characteristics
Abstract methods have no implementation, so the method definition is followed by a semicolon instead of a normal method block.
Derived classes of the abstract class must implement all abstract methods.
Implementation is provided by overriding method.
Cannot be private.
Cannot be static.
Abstract method cannot be contained in non abstract class.

Example of Abstract Class and Methods

   abstract class Animal
    {
       internal  void Show()
       {
           Console.WriteLine("Mein Janwar Hun");
       }
       abstract internal void Sound();
       abstract internal void FoodHabit();

    }

  class Dog : Animal
   {

      
     internal override void Sound()
       {
           Console.WriteLine("Bow Bow Bow Bow");
       }

      internal override void FoodHabit()
       {
           Console.WriteLine("Noveggieee");
       }
   }
    class Cat:Animal
    {
        internal override void Sound()
        {
            Console.WriteLine("meow Meow meow");
        }

        internal override void FoodHabit()
        {
            Console.WriteLine("veggieee");
        }
      
      
    }

class CallingClass
{
       public static void Main()
       {
             Dog d = new Dog();
           d.Show();
           d.Sound();
           d.FoodHabit();

           Console.ReadKey();

           Cat c=new Cat();
           c.Show();
           c.Sound();
           c.FoodHabit();
           Console.ReadKey();
       } 
 }

Monday, 3 March 2014

What is Specialisation?

  • When some new sub classes are created from an existing super class to do specific job, then this process  is known as specialization.

What is Generalisation?


  • When we create a base/super class from two or more similar type of objects by extracting their all  common characteristics(attributes and behavior) and combine them, then this process is known as Generalization.

What is Inheritance?

  • Inheritance is the process of creating a new classes from an existing class.
  • The Pre-existing class is called base class or super class and
  • the new class is knows as derived class or subclass.
  • The newly created class have all the characteristics of its existing or base class and it can also add its own additional attributes and behavior too.
  • Inheritance allows the extension and reuse of existing code, without having to repeat or rewrite the code from scratch

Example of Inheritance

//generalization  - extracted common characteristic
class Product
    {
         int productcode;
         string productname;
         int price;
        
        protected void getData()
        {
            Console.WriteLine("Input Product Information");
            Console.WriteLine("Enter Product Code");
            productcode = int.Parse(Console.ReadLine());
            Console.WriteLine("Enter product Name");
            productname = Console.ReadLine();
            Console.WriteLine("Enter Price");
            price = int.Parse(Console.ReadLine());
        }
       protected  void showData()
        {

            Console.WriteLine("Show Product Information");
            Console.WriteLine("Product Code={0} \n Product Name={1} \n Price={2} \n", productcode, productname, price);
        }
 }//class Product Ends
   
//specialization  - to specific job + inherited product
    class Book : Product
    {
        int noPages;
        string Author;
        internal void getBook()
        {

            getData();

            Console.WriteLine("Input Book  specific Information");
            Console.WriteLine("Enter No of Pages");
            noPages = int.Parse(Console.ReadLine());
            Console.WriteLine("Enter Name of Author");
            Author = Console.ReadLine();

        }
      internal void showBook()
        {
           showData();

            Console.WriteLine("Show Book specific information");
            Console.WriteLine("Pages={0} \n Author={1}", noPages, Author);

        }
         
          
    }//Book Class Closed

//specialization  - to specific job + inherited product
    class Pen : Product
    {

    
        string b;
        string cink;

        internal void getPen()
        {
            getData();
            cink = Console.ReadLine();
            Console.WriteLine("Enter Brand Name");
            b = Console.ReadLine();

        }
        internal void showPen()
        {   showData();
            Console.WriteLine("Brand={0} \n Ink Color={1}", b,cink);

        }

    }

class CallingClass
{
     public static void Main()
      {
        Console.WriteLine("****BOOK *****"); 
        Book b=new Book();
        b.getBook();
        b.showBook();

        Console.WriteLine("****PEN *****"); 
        Pen p=new Pen();
        p.getPen();
         p.showPen();

        Console.ReadKey();
}