Showing posts with label Abstraction. Show all posts
Showing posts with label Abstraction. Show all posts

Monday, May 9, 2011

Differecne Between Interfaces and Abstract ?

Differences between Interface and Abstract Class?
Interfaces :
--An interface is not a class.
--It is an entity that is defined by the word Interface.
--An interface has no implementation;
--it only has the signature or in other words, just the definition of the methods without the body. As one of the similarities to Abstract class
 ____________________________________
Public Interface ImyInterface

{
String Method1();
int Method2();

}
Public Class myClass :ImyInterface
{
string Method1()
{
//Code
}
int Method2()
{
//Code
}
}
____________________________________________________________________________
---Requires more time to find the actual method in the corresponding classes. (Slow )


The main difference between them is that a class can implement more than one interface but can only inherit from one abstract class. Since C# doesn't support multiple inheritance, interfaces are used to implement multiple inheritance

  Abstract Class:
   These Classes Cannot be instantiated, 
 ---These are used if additional functionality is needed in derived classes,
 ---These Implemented partially or No Implementation
--Fast In Operation
 ____________________________________________________________
abstract class MyAbstractClass
{
public MyAbstractClass()
{
// Code to initialize the class goes here.
}

abstract public void Method1();
abstract public void Method2(int A);
abstract public long Method3(int B);
}
class MyClass : MyAbstractClass
{
public MyClass ()
{
// Initialization code goes here.
}

override public void Method1()
{
// code goes here.
}

override public void Method2(int A)
{
// code goes here.
}

override public long Method3(int B)
{
//code goes here.
}
}


What is OOPS Concepts ?

OOPS Concepts 
   
Ans:  Object Oriented Programming (OOP) All Programming Language will support this OOP there are Three main concepts in OOP
   a)Encapsulation
   b)Abstraction
   c)Inheritance
   d)Polymorphism
Encapsulation  :
Encapsulation is the process of Keeping the data and method together into Objects
OR
Simple Putting Together into one simple class Like.
OR
Means that a group of related properties, methods, and other members are treated as a single unit or object.
_____________________________________________________________
Class My Class
{
public MyClass()
{

}
int a;
int b;
Public void GetAdd()
{
 int C=a+b;
}

}
____________________________________________________________

Abstraction : Hiding the Data
Inheritance:
Inheritance Enables  you to Create Class that Reuses
OR
Acquiring the Properties of Base Class(Parent Class)
OR
Means Provide Further Implementation ,Reusing of  Base Class
__________________________________________________________
Public Class MyClass
{
Public MyClass()
{
}
int a;
int b;
Public void GetMyMethod()
{
int c=a+b;
}
}
Public Class MyChildClass : MyClass
{
 Public MyChildClass()
{}
}
C# Doesn't Support Multiple Inheritance By Using Interfaces We Can 


Polymorphism
Polymorphism means same operation may Behave Differently in Different Classes

Example of Compile Time Polymorphism: Method Overloading
Example of Run Time Polymorphism: Method Overriding

Method Overloading- Method with same name but with different arguments is called method overloading.
- Method Overloading forms compile-time polymorphism.
- Example of Method Overloading:
class MyClass
{
Public string hello()
{
return "Hello";
}
public string hello(string s)
{
return "Hello"+S.Tostring();
}
}

Method Overriding- Method overriding occurs when child class declares a method that has the same type arguments as a method declared by one of its superclass.
- Method overriding forms Run-time polymorphism.
-Virtual Key word is required for Overriding
- Example of Method Overriding:
Public Class MyClass
{
virtual void hello()
{
Console.WriteLine(“Hello from MyClass”);
}
}
Public Class MyChildchild : MyClass
{
override void hello()
{
Console.WriteLine(“Hello from Child”); }
}
}________________________________________________