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

Inheritence and Polymorphism

Well, now I would like to make some more clear ideas about what coding is when you are working in VB.NET.

As I have already told you, the first line just after the class name, you will be getting the inherits part. This inherits part making our task of getting a new form much easier. It would be worthy enough to get a well designed form just with a click of mouse. Isnt it. Its what we all know about the famous feature of any OOPS based language. In case of JAVA we have to derive from Frame class whereas here it is Form class. Both are the same thing.
OOPS concept:

1. Inheritence:
Inheritence is the process of getting all the properties of the base class within the new class that you are deriving from. Well, its just a shortcut of getting everything pre-defined or created. So it just ensures reusability. Nothing else. Just once a class is defined, it can be inherited instantly to another class and can be used with their function whenever you require to use them. Only the exception if you define a class as NOTINHERITABLE, that class cannot be inherited. So hey hey.. until you are hard enough to give your code to other programmers, you can surely make your code Inheritable... Now lets look at the code below. public class A
public void show()
msgbox ("Hello") ' Messagebox function defined already within your scope...
end sub
end class

public class B
inherits A
end class

Now if you define an object of B, it can definitely call the show function defined within A.

2. Polymorphism: The term signifies, that more than one form. How in case of OOPS, polymorphism can exist. Well, the support of this feature is because of OVERLOADING and OVERRIDING. The term overloading means, existence of more than one function in the same scope with same name. Well, just think, suppose we dont support overloading... just functions.. in that case, you have to define functions individually differently. So there may exist only one function say sum with some number of arguments. Now suppose you have defined a function sum as
public function sum(byval a as Integer, ByVal b as Integer) as Integer
sum=a+b
end Function
in this case, the function sum takes two arguments and returning the sum of both. Well, the condition is nice enough to sum up two numbers. Well, after defining this function suppose some requirement comes, where the function can sum up 3 numbers. Just now the problem begins. Now you need to name the function which you are to create differently. That means separate indentifier to separate functions.. You may think what will be the problem with this. Now suppose you have named a function which is working almost similarly but having different names and you need to remember all of their names. Here comes the benefit of Overloading where similar functions can be named identical but they should be having one common difference. The difference lies on their Argument List. Eg.
public overloads function sum(byval a as Integer,ByVal b as Integer)as Integer
Return a+b
end Function

public overloads Function sum(Byval a as Single,ByVal b as Single) as Single
Return a+b
End Function

Here the two functions having the same name but difference in arguments can stay on the same scope, i.e in a scope of a class. Mind that, overloading is a property of a class, so it would not be working if you write it in a standard module.

Another feature of Polymorphism is Overriding... In case of overriding, two functions are having exactly same signature ... but defined differently.. well, this make your all confusion, isnt it.. Well, how could be possible for a class to call the same two function with exactly the same signature...
Well the case is bit different. Well, the two function should stay on different classes, one in the base class, and another in the derived class... The base class will be overriding the derived class functions.. this is overriding....
Now look at the example
Class A
public overridable function abc()
msgbox ("Hii")
end Function
end class
Class B
Inherits A
Public overrides Function abc()
msgbox ("Hello")
end Function
End Class
So here when we call abc with the object of B, it will call the msgbox with hello, and when we create the object of a and call abc, it will call hi...
So we are modifying the definition of already defined function. isn't it beautiful...

I will continue my posts... later on...
Thanks for reading

No comments: