OOPS Practical

Oops

Object

 Instance of class is called object, through object we can access class members & methods.

Class

 Class is user defined data type. It consists of data members and methods.
 
Polymorphism

 Same name in different form. 
 
 Types: Compile time polymorphism, Run time Polymorphism.


Overloading (Compile Time Polymorphism)

 Same method name with different type of parameter. 
 Return type may be same or not.
 It occurs with in a class.

 Ex: Static Binding

using System.IO;

public class test
{
  public string add(string str)
  {

  System.Console.WriteLine(str);

  return str;

  }

  public int add(int a, int b)
  {
  int c = a;
  int d = b;
  int e = c + d;
  System.Console.WriteLine(e);

  return e;
  }

 

  public char add(char a)
  {

  System.Console.WriteLine(a);

  return a;
  }



  public static void Main(string[] args)
  {
  test c1 = new test();
  c1.add("Jey");
  c1.add(1, 2);
  c1.add('M');

  }
}

Output: 
 Jey
 3
 M



Overriding (Run Time Polymorphism)

 Same method name with same signature.
 It occurs with in sub class.
 Return type must be same.
 
Ex: Dynamic Binding

using System.IO;

public class test2
{

  public virtual int add(int a, int b)
  {
  int c = a;
  int d = b;
  int e = c + d;
  System.Console.WriteLine(e);
  return e;
  }

}
 

class newtest : test2
{
  public override int add(int a, int b)
  {
  int c = a;
  int d = b;
  int e = c + d;
  System.Console.WriteLine(e);
  return e;
  }




  public static void Main(string[] args)
  {
  newtest nt = new newtest();
  nt.add(1, 2);

  }
}


Inheritance

 One base class derived from one or more classes.

 Type: Single Level, Multi Level, Multiple, Hybrid, Hierarchy.

Interface (Multiple Inheritance)

 We can’t use Multiple Inheritance in C#.
 Instead of that we can use interface in C#.
 Interface is 100% abstract.
 In interface, we can only defined properties.
 One class implements more than one interface.
 
Ex: 

using System.IO;

interface a
{

  void add();


}
 

interface b
{
  void sub();

}

public class exinterface : a, b
{
  int a = 10;
  int b = 20;
  public void add()
  {

  int c = a + b;
  System.Console.WriteLine(c);
  }
  public void sub()
  {
  int d = a - b;
  System.Console.WriteLine(d);
  }


  public static void Main(string[] args)
  {
  exinterface ex = new exinterface();
  ex.add();
  ex.sub();
  }



Abstract Class

 We can’t create object.
 We can’t implement any functionality in abstract method.
 Abstract method should be in Abstract class.

Ex:

using System.IO;

public abstract class ab
{

  public abstract void add();


}

 

public class exabstract : ab
{
  int a = 10;
  int b = 20;
  public override void add()
  {

  int c = a + b;
  System.Console.WriteLine(c);
  }


  public static void Main(string[] args)
  {
  exabstract ex = new exabstract();
  ex.add();

  }
}