Hi there! Seems you are visiting my site for the first time. Hmm, Why dont you subscribe to RSS or use email subscription service to get all posts at your inbox (No spam). You can also follow me as google friend.
Please carry on reading my blogs, if you like, please give your feedback too. Have a wonderful day! (Dismiss)

Sunday, October 12, 2008

Bit related about Interfaces and Abstract classes.

So friends, until now, I think you have got the basic idea about the Object Oriented Programming paradigm. With changing atmosphere, it gets rather difficult to make a project worthwhile. So It needs a structured way to maintain the whole project. Every company before its very creation creates their own framework to make data access for their own products much more easier. The architecture what microsoft provides us is also structured. In talking about structure, one thing that comes first in our mind is Interface.

Lets discuss Interfaces a bit further...
Interface: An interface is a class that have only abstract methods and final variables. An abstract method is just a declaration of the signature without actual implementation of its body. Thus when creating an abstract function we dont need to create the body of the function. Its just the declaration of how a function looks line..

abstract Function sum(Byval x as Integer,ByVal y as Integer) as Integer

In the previous declaration, the function body is nothing, i.e, Its not defined yet. Just it would be defined or overridden to its derived classes.
If a class is having any function within it as abstract, the class must also be declared as abstract. Means, whenever we write any of the member function of a class as abstract, we are losing the propoerty of instantiating the object of that class. So the class will also be called as abstract.
abstract Class A
Function a()
End Class
Now Coming to Inheritence, as I mentioned in my earlier post, Inheritence is the process of making a class just derived from another class. Well, this property ensures re usability of code. Now In .NET environment, one class can have only one base, at most... So Multiple Inheritence is not supported in .NET environment. But this can be supported with the help of Interface.
Interfaces are pure abstract classes.. Means in an Interface there may not be any defination of any of its methods so that rather than inheriting from the class we use implementing from it. Just we need to override all of the functions defined within the interface in our class. This is the case of Multiple Inheritence where if you are implementing from interfaces, a single class can have multiple base classes.
Class A
Inherits BaseA 'Inherits can be only one
Implements Base IA,BaseIB 'Implements can be more than one
Public Function Afun() as string Implements BaseIA.Afun
end Function
Public Function Bfun() as string Implements BaseIB.Bfun
end Function
End class
So here a class A is having three base classes, One is which it is derived from and the others are which it is implementing.
Interface can point to the object of the class which it implementing it. means
Dim ba as BaseIA
ba=new A()

This is possible as if the base class can point to derived class objects.
So you will get everything like the base classes in case of implementing and can enjoy the flavour of Multiple Inheritence in VB.NET.....
Isn't it Cool....

1 comment:

abhishek2434 said...

Good post ... I like it.