Monday, 25 April 2016

EVEN OR ADD --Sample Programs -- C#.NET

EVEN OR ADD


Write a Program to check a given is even or odd in C# language ?

ANS :



1. read a number from keyboard 
2. in if , divide that number with 2 if  remainder is zero it is even otherwise odd


------------------------------------------------------------------------------------------------------------------

Wednesday, 2 March 2016

C#.NET -- INTERFACE

                                    INTERFACE 

Interface is a mechanism to achieve full abstraction that is it contains only abstract methods without body.

Interface is a contract between itself and it's  derived classes . and it acts like a blueprint for creating a new objects.

Ex :  oops ( rules and regulations) are blueprint for java and c# etc., like that interface is a blueprint for its derived class to provide a guide to do one task.

Interface cannot implement any thing so we can used that format  for further development of same format of derived classes.

features of interface :


  • The interface contains only method declarations and can‘t contain method definitions (similar to the abstract methods).
  • The interface can‘t contain any data members but can contain ―automatic properties (Already you know that the automatic property doesn‘t contain definitions for ―get and ―set accessors).
  • Interface methods are by default ―public. We can‘t use another access modifier like private and protected etc. there no need external declaration  of public also.
  • The interface can‘t be instantiated. That means you can‘t create an object for the interface.
  • The interface can‘t contain constructors.
  • The class that inherits the interface is called as Implementation Class.
  •  The implementation class should implement the definitions for all the interface methods. If not, it would generate compile time errors.
  • One interface can be inherited by any no. of classes (Hierarchical inheritance).
  • One class can inherit any no. of interfaces (Multiple inheritance).
  •  The interface methods can‘t be declared as ―virtual or ―static in the interface definition.


Syntax :

interface definition:

interface interfacename 
{
  // empty methods declarations here
 }

implementation class definition  :

class classname : interfacename 
   public returntype methodname(arguments) 
  { 
    //some code 
  } 
 }



Sample Programs :

Write a Program on interface usage .



Output is :

the method is taken from parent interface


-----------------------------------------------------------------------------------------------------

Write a program to develop multiple inheritance using interface ?



Output is :
take this money childs!!
take this snakes childs !!
hello chidls 

So even there are same methods in two interface the program will execute .










Monday, 29 February 2016

C#.NET --- ABSTRACT CLASS

ABSTRACT CLASS 

In general a method which is not implemented directly and its is implemented in its derived class is known as virtual method.

in general virtual means ' not real ' or ' not actual '  or which is not existed physically is known as virtual 

why virtual methods ?

for suppose if a class is inherited by several classes, but each derived class have different implementation for a single methods, in that case base is gives a chance to implement in their classes rather writing code in that method(s).

for example if a base class CAR is there and it is inherited by two sub classes they are one is BMW and another one is FORMULA _ONE 

the BMW has right hand side steering and FORMULA_ONE have left hand side steering 
there is a function in CAR class Steering()  so rather to write that method either left hand side or right hand side . it is better to set empty or virtual and implement is derived classes
for right hand side in BMW and left hand side FORMULA_ONE respectively. 

Abstract methods and Abstract classes :

Abstract method is a virtual function created with abstract keyword  i.e., it can implemented in appropriate derived class is known as abstract method 

Abstract class is a class which contain at-least  one abstract method is known as abstract class. A abstract class contains both virtual and non-virtual methods 

rules to write a abstract method or abstract class :


  • An abstract method must be created with abstract key word as functional modifier 
  • An abstract method must be placed in a abstract class only , we cannot write it in non-abstract classes
  • An abstract class contains both abstract methods and non-abstract methods 
  • we cannot create a instance for abstract classes So it must be derived by another class 
  • An abstract method must be override in derived class otherwise it will raises compilation error.
  • An  abstract method must be public type access modifier. 
  • While implementing in derived class it must use "override" keyword.
Program :


Output is :::::::




Interview Questions :

1.Can we need to write virtual keyword for abstract methods ?
ans : No need to virtual keyword ,but Although an abstract method is implicitly also a virtual method, it         cannot have the modifier virtual.

2.Where we implement  abstract methods ?
ans: all abstract methods are not provide their implementation of that method. Instead, non-abstract derived classes are required to provide their own implementation by overriding that method.

3. Can we do inheritance by abstract class 
ans: Yes, we can inheritance by abstract classes from normal classes and also abstract classes 
   
example 1 :
   abstract class A
    {
      public abstract void method1();
      public void method2()
      {
       Console.WriteLine("implemented in class A");
      }
    }
   abstract class B : A
  {
    public abstract void method3();

  }
  public class C : B
{
     public void method1()
      {
       Console.WriteLine("implemented in class C");
      }
       public void method3()
      {
       Console.WriteLine("implemented in class C");
      }
}

Here two abstract classes are available which are inheriting one by another, but it is class B responsibility to implement class A abstract method in its sub class C.

4. Can we write 










Saturday, 27 February 2016

C#.NET --- POLYMORPHISM

POLYMORPHISM 

Polymorphism is a concept of use a single method  we can provides different operations is known as polymorphism,

In general poly means many and morphism means form i.e., many forms for a single method .

Advantage :  

We can assigns some additional work to the function apart from the existing work.
We can provide different implementation to the same function or method

Suppose if a function add() do addition two number but we want addition three number with the name of add() function we can do by again writing it.

Polymorphism can be classified into two types :

1. Static Polymorphism
2. Dynamic Polymorphism


Static Polymorphism :

static polymorphism  is the polymorphism which occurs at compile time. the deciding choosing a method is done at compile time only. It is also called early  binding.

By using Function Overloading we can achieve it.

Dynamic Polymorphism :

dynamic polymorphism is the type of polymorphism which occurs at runtime only, that is choosing of which is method is decided at runtime  or execution time. It is also called late binding.

By using function overriding we can achieve dynamic polymorphism.


Function Overloading 

The mechanism of  providing different implementation to same function with different signature is called function overloading. It is a static polymorphism

we can write two or more functions with different signature. Here signature means

1. number of arguments
2. type of arguments
3. return type of function

If we write any two or more functions with same name and by changing number of arguments , type of arguments or return type of  arguments

Ex:






































Here in a same class we have wrote 3 add functions of different of signature. The compiler detects which type of function is suitable based on given parameter at compiler time.

Outpur is :





-----------------------------------------------------------------------------------------------------------


Function Overriding :

The mechanism of  providing a same function name  which is base class can write  with different implementation with same signature in derived class is called as function overriding

While doing inheritance if we want to provide different implementation for base class method we can override method by writing same function with same signature. the method choosing done at runtime 






C#.NET ---- MULTI LEVEL INHERITANCE

MULTI LEVEL INHERITANCE

Multilevel inheritance is one type of inheritance which deriving a child class from already derived parent class that is acquiring the properties of  a class which acquires some properties from another class.

Suppose class B acquired one methods form Class A, and class C acquired from class B then all properties of class A and class B of both can access by class C.

for example, a person B acquires some properties from his father A, and person is C is son of person B who can acquires his father B properties and his fathers father A properties both.



Ex :

class father    
{
  public void fProperty()
{
  Console.WriteLine("I have one big house ");
 }
}
class Person : father    // this is first level of inheritance
{
  public void pProperty()
 {
  Console.WriteLine("I have10kg gold ");
 }
}
class son : Person     // this is second level of inheritance
{
  public void sProperty()
  {
    Console.WriteLine("I have a car ");


}

}
class Program
{
 static void Main()
{
  son s = new son()
   s.fProperty();  // his father property
   s.pProperty();  // person property
   s.sProperty();   // his son property
   Console.ReadKey();
}
}

Output :
I have one house
I have 10kg gold
I have a car


---------------------------------------------------------------------------------------------------------------------

Friday, 26 February 2016

C#.NET ---- HIERARCHICAL INHERITANCE

                                                    HIERARCHICAL INHERITANCE 

Hierarchical inheritance is  one type of inheritance which is deriving two or more class from a single base class is know as hierarchical inheritance.

The same base class B1 properties are can share by different class d1, d2, d3...dn., are done here, and for each and every derived class a single copy is generated if we change the properties of a base class it cannot effect the other derived classes.


fig 1 :

fig 2: student is base class and art student , science student and commerce are derived class




A Sample example program on hierarchical inheritance ;

class Student
{
  public void print()
 {
   Console.Write("this   student ");
  }

}
class ArtsStudent : Student
{
  public void print1()
 {
    
   Console.WriteLine("is Arts student ");
  }
}
class ScienceStudent : Student
{
 public void print2()
 {
   Console.Write("is Science student ");
  }
}
class CommerceStudent : Student
{
 public void print3()
 {
   Console.Write("is Science student ");
  }
}

class Program
{
   ArtsStudent as = new ArtsStudent();   // creating object for each class 
   ScienceStudent ss = new ScienceStudent();
   CommerceStudent cs = new CommerceStudent();
   as.print();        // accessing base class method 
   as.print1();
   ss.print();
   ss.print2();
   cs.print();
   cs.print3();
    
     Console.ReadKey();
}

output is 
this student is Art student
this student is Science student 
this student is Commerce student


Program 2: another on hierarchical inheritance is here if you want to download it see below


C#.NET ---- SINGLE INHERITANCE

Single inheritance :

The process of creating one new class by derived from a single class is known as single Inheritance.

A simple example for single inheritance is father and son . a father property is can a chance to us        by son like their relationship, inheritance forms a relationship called "IS A' for suppose
 "Man is a Human ".
       base class = Human
       derived class = Man
After inheritance , A Derived class can access Base class properties , But The Base cannot have a permission to access Derived class properties 

Define a base class :

class  <BaseClassName >
{
 //data members of super class
 //methods of super class
 }

EX :
      class A
     {
        String str = "base class" ;
        public void Print()
        {
            Console.WriteLine("this is "+ str);
         }
      }

Here we write a normal class A with str as member variable and Print() member method 

Define a derived class:

Class < SubClassName> : < SuperClassName >
{
 //data members of sub class
 //methods of sub class

 }
   Ex : 
       class B : A
      {
        string str2 = "child class";
        public void print2()
       {
           print();                  // accessing Class A method with out using object
           Console.WriteLine(str2+" is derived from " + str);
        }
      }

Here By using colon ' : ' operator we can do inheritance from A to B , in Class B we can directly access Class A properties like print() method because they are inherited.


Accessing base class through derived class:

<subclassname> objectname = new  <subclassname>();

     objectname.SuperClassMethod();
                                or
     variable = objectname.SuperClassVarible ;

 Ex :
  
Class Program
{
   B b = new B(); // creating a sub class object
    b.print(); // accessing Class A method through object
    b.print2() ;

   Console.ReadKey();
}

Here we create a sub class object b and using sub class object only we can access directly the base class methods.
output  :
              this is base class
              this is base class
              child class is derived from base class

------------------------------------------------------------------------------------------------

Program 2 : 

Write a program on single inheritance like entering details in Base class and print that details in Derived class ?



/** A program on single inhertance **/
using System;
namespace InheritanceDemo
{
    class ClsParent    // creating parent class or base class
    {
        public  string source, destination; // base class variables 
        public void Reservation() // base class method
        {
            Console.Write("Enter city name where your are in now : ");
            source = Console.ReadLine();
            Console.Write("Enter city name where you need to go : ");
            destination = Console.ReadLine();
        }
    }
    class ClsChid : ClsParent  // creating child class using : colon operator
    {
         double amount;
        public void calAmount() // derived class method
        {
           
            amount=1000;
        }
        public void DisplayReservationDetails() // derived class method
        {
            Console.Write("\nfyour are  travling from "+source); // source is accessible because it is inherited from parent class
            Console.WriteLine(" to "+destination);  // destination is accessible because it is inherited from parent class
            Console.WriteLine("fare is :"+amount);   // amount is child class variable only             
        }
   
    }
    class Program
    {
        static void Main(string[] args)
        {
            ClsChid obj = new ClsChid(); // creating object for derived class
            obj.Reservation();   // using derived class object accessing base class method 
            obj.calAmount();        // as usual accesing member methods 
            obj.DisplayReservationDetails(); // as usual accessig member methods
            Console.ReadKey();
        }
    }
}

Output :
Enter city name where you are in now : hyderabad 
Enter city name where yor need to go : vijayawada

your are  travling from hyderabad to vijayawada
face is : 1000








------------------------------------------------------------------------------------------