C# Tutorials and offshore development in India
    Tutorials   Resources   Forum   Communities   Interview   Jobs   Projects   Offshore Development    
Silverlight Tutorials | Mentor | Code Converter | Articles | Code Factory | Computer Jokes | Members | Peer Appraisal | IT Companies | Bookmarks | Revenue Sharing |


Prizes & Awards
My Profile



Active Members
TodayLast 7 Days more...

New Feature: Community Sites: Create your own .NET community website and start earning from Google AdSense ! It's Free !






OOPS Concepts in .Net to Shake your fundamentals Part 4 - Interface and Abstract Class


Posted Date: 17 Jun 2008    Resource Type: Articles    Category: .NET Framework
Author: shakti singh tanwarMember Level: Diamond    
Rating: Points: 12



This article is part of the "OOPS Concepts in .Net to Shake your fundamentals(Interface and Abstract Class)" series. If you have missed out the previous parts please check out -




Scenerio 9-
Abstract class without abstract members.
Function present in both base class and child class.This concept is called Method hiding.


public class MyClass
{
public void foo()
{
Console.WriteLine("Base version of foo");
}
}

public class MyChild : MyClass
{
public new void foo()
{
Console.WriteLine("Child version of foo");
}
}


Without new keyword compiler will give a warning that you are hiding the base class implementation.
We will create objects of base and child normally and through casting in all following code samples:-


MyClass obj = new MyClass(); //Not possible
MyChild obj2 = new MyChild();
MyClass objCastedFromChild = (MyClass)obj2;
obj2.foo();
objCastedFromChild.foo();


output:-
Child version of foo
Base version of foo
So in normal scenerio’s follow the simple rule. Thy object thy function.

So now you must have a good idea about what an abstract class is.
Interview definition:-
Abstract class is a class which can’t be instantiated. By instantiation we mean that we can’t create objects through constructor calling on base but we can create objects through casting.
Usage:-
Handy for building big architectures where all functionality is not known in initial stages so architect compiles whatever his knowledge is about the subject into class definition and declares what he thinks should be there but for which he has no concrete ideas as abstract.
.Net doesn’t support multiple inheritance.This is because the gains that were there for multiple class inheritance were very less compare to the cost of resolving ambiguity or name collisions in cases where two base classes have data or functions with same name so .Net removed multilpe inheritance through classes.Now you can have data from just one base class but you can implement multiple interfaces.

Interfaces:-
In layman’s terms we can say that interface is a pure abstract class with all methods abstract and no data members.
Interview definition:-
Interface is a contract between two parties.The contract is that all the parties implementing interfaces must implement all the methods declared in interfaces failure in doing which the class will be an abstract class.
Abstract class V/s Interfaces:-
1.) Abstract class can contain data members.Interfaces can’t.
2.) Abstract class can contain non abstract methods too.Interfaces can’t.
3.) Default access specifier is abstract class is private but we can have other access specifiers also.Interfaces methods are by default public you can’t specify any access specifier not even public.
4.) In interfaces members are by default abstract you can’e even specify asbtract keyword.
5.) Abstract class can implement interfaces.Interfaces can’t inherit classes but can implement other interfaces also.
6.) You can inherit from a single base class but you can implement multiple interfaces.
7.) Abstract classes are created using abstract keyword.Interfaces are created using interface keyword.

Here is an example of interface:-


public interface myinterfacse
{
void foo();

}
public interface myinterfacse1 : myinterfacse
{
void foo2();

}


Struct in .Net v/s struct in other programming languages:-

Struct are value types.Struct in .Net are different from struct in other languages in a way that struct in .Net is a sealed class. Sealed class is a class which can’e be inherited and this is the reason that members of a struct are by default private and not public.Struct can have Main method also just like a class.Struct can even have a constructor but struct can only have parametrized constructors.In early days struct had default constructors but thery were removed due to some reasons known only to creators ?



public struct MyStruct
{
int a;
public int b;
public MyStruct(int i, int j)
{
this.a = i;
this.b = j;
}
public void foo()
{
Console.WriteLine("Hi");
}
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.Run(new Form1());
}
}


Class v/s Struct
1.) Class can have default constructor.Struct can only have parametrized constructors.
2.) Class is a reference type whereas struct is a value type.
3.) Struct can’t inherit or implement where as class can both imlpement interfaces and inherit interfaces.

We will start by simple interface implementation. Let’s create an interface with one method.


public interface myinterfacse
{
int foo();

}


Now let’s first have a look at a simple class


public class MyClass
{
int a;
}


This class compiles fine but now lets implement the interface defined above


public class MyClass: myinterfacse
{
int a;
}


Now when you compile this class you will get a compile time error stating that myinterfcae.foo() is not implemented in MyClass.If you don’t want to implement foo in MyClass then you must declare MyClass as abstract class.Let’s now look at 2 more scenerios of interface implementation


public class MyClass: myinterfacse
{
int a;
public void foo()
{
return a;
}
}


This time the class compiles fine.Now let’s create objects of myinterface and MyClass.


In next samples we will be discussing about interface polymorphism.
Scenario 10:-
First we will discuss implicit implementation of interface.


public interface myinterfacse
{
int foo();
}
public class MyClass : myinterfacse
{
int a=10;
public int foo()
{
return a;
}
}
MyClass obj = new MyClass();
myinterfacse obj2 = (myinterfacse)obj;
obj.foo(); //Returns 10
obj2.foo(); //Returns 10


Here foo is acting both as a class member as well as interface member.
Scenario 11:-
Remember OOPS is a bit tricky and if you write MyClass definition as below then


public class MyClass : myinterfacse
{
int a=10;
int foo()
{
return a;
}
}


It will give a compile time error.
Error 1 'WindowsFormsApplication3.Program.MyClass' does not implement interface member 'WindowsFormsApplication3.Program.myinterfacse.foo()'. 'WindowsFormsApplication3.Program.MyClass.foo()' cannot implement an interface member because it is not public.

Scenario 12:-
Lets now discuss explicit implementation of interface be taking below example. In explicit implementation functions are qualifies with parent interface names as below:-


public class MyClass : myinterfacse
{
int a=10;

#region myinterfacse Members

int myinterfacse.foo()
{
throw new NotImplementedException();
}

#endregion
}


Now there will be some errors in code of scenario 10 as below


MyClass obj = new MyClass();
myinterfacse obj2 = (myinterfacse)obj;
obj.foo(); //Compile time error.No class member foo
obj2.foo(); //Returns 10


Scenario 13:-
Interface polymorphism with multiple interfaces having same method.


public interface myinterfacse
{
int foo();
}

public interface myinterfacse2
{
int foo();
}
public class MyClass : myinterfacse,myinterfacse2
{
int a=10;

#region myinterfacse Members

public int foo()
{
return a;
}

#endregion
}

MyClass obj = new MyClass();
myinterfacse obj2 = (myinterfacse)obj;
myinterfacse obj3 = (myinterfacse)obj;
obj.foo(); //Returns 10
obj2.foo(); //Returns 10
obj3.foo(); //Returns 10


Scenario 14:-
Different implementations for both interfaces and class.


public class MyClass : myinterfacse,myinterfacse2
{
int a=10;
#region myinterfacse Members

public int foo()
{
return a;
}

#endregion

#region myinterfacse Members

int myinterfacse.foo()
{
return 11;
}

#endregion

#region myinterfacse2 Members

int myinterfacse2.foo()
{
return 12;
}

#endregion
}


In this case


MyClass obj = new MyClass();
myinterfacse obj2 = (myinterfacse)obj;
myinterfacse obj3 = (myinterfacse)obj;
obj.foo(); //Returns 10
obj2.foo(); //Returns 11
obj3.foo(); //Returns 12


This concludes our discussion on interfaces.In next article of series we will discuss Compile time polymorphism i.e. Operator and Function overloading.




Responses

Author: Ankit Bhadana    18 Jun 2008Member Level: Bronze   Points : 0
thank u sir!
Regards
-Ankit Bhadana


Author: Nanak Deep    18 Jun 2008Member Level: Bronze   Points : 0
very useful,
thanx sir


Author: kalpana    19 Jun 2008Member Level: Bronze   Points : 0
Its really very helping.

Kalpana


Author: Ashish verma    19 Jun 2008Member Level: Bronze   Points : 2
sir,
It was really a nice piece of coding.
This article really helps in building
concept about interface and abstract class.
Keep on posting more articles.
Thanks.


Author: kamal kant    23 Jun 2008Member Level: Gold   Points : 0
This Article is Very Knowledgeful


Feedbacks      
Popular Tags   What are tags ?   Search Tags  
Object oriented programming funamentals  .  Object oriented programming concets  .  Interface concepts  .  Abstract classes in c#  .  Abstract class in c#  .  

Post Feedback


This is a strictly moderated forum. Only approved messages will appear in the site. Please use 'Spell Check' in Google toolbar before you submit.
You must Sign In to post a response.
Next Resource: How to send a Mail using vb.net
Previous Resource: Handling Application Level Exceptions in Windows and Web Applications
Return to Discussion Resource Index
Post New Resource
Category: .NET Framework


Post resources and earn money!
 
Related Resources



dotNet Slackers   BizTalk Adaptors    Web Design


Contact Us    Privacy Policy    Terms Of Use