Friday, 18 May 2012

Object Oriented Programming in VB.Net (Cont.)


Polymorphism

Polymorphism basically stands for " one name , multiple behaviour" in general concept. It is a important feature of OOPS , In programming it is also called overloading, overloading means the use of same thing for different purposes.
By using of this polymorphism feature we can create many function with the same name, and that all function will show different behavior in different scenarios, but with different argument list.
and the function will perform different operations based on argument list  in function call.

The following code demonstrates the implementation of Polymorphism.



Module Module1


    Sub Main()
        Dim two As New One()
        Console.WriteLine(two.addition(5))
        Console.WriteLine(two.addition(5, 10))
        Console.WriteLine(two.addition(5, 10, 20))
        Console.Read()
    End Sub


End Module


Public Class One
    Public x, y, z As Integer


    Public Function addition(ByVal x As Integer) As Integer
        'function with one argument
        Return x
    End Function


    Public Function addition(ByVal x As Integer, ByVal y As Integer) As Integer
        'function with two arguments
        Return x + y
    End Function


    Public Function addition(ByVal x As Integer, ByVal y As Integer, ByVal z As Integer) As Integer
        'function with three arguments
        Return x + y + z
    End Function


End Class



Output of the above code is shown in the image below.

Thursday, 17 May 2012

Object Oriented Programming in VB.Net..

Hello! this is my first post in my blog, As this blog is for VB.Net I am going to start with OOP in  VB.Net.

What is OOP :

  • Basically OOP stands for object oriented programming , it is a programming paradigm focussing on     "Objects". An "Object" is a DataStucture- that consist of  Data Fields and Methods together with their interactions – to design applications and computer programs.
  • The Objects referred to are created from something called a Class. 
OOP is basically stands for Object Oriented Programming, the main features of Object Oriented Programming is , - Encapsulation , Abstraction , Polymorphism and Interface , - The feature Interface differ the Object Oriented with Object base.
Lets attention a little on Object Based Language too , The object based language is called object based because of its supports only three feature -  Data Encapsulation , Data Abstraction and Polymorphism.but not Interface..

Concept of Object Oriented Programming:

  • Classes
  • Objects
  • Data Abstraction and Encapsulation
  • Inheritence
  • Plymorphism

Objects 

Objects are the basic run-time entities in an object-oriented system. Programming problem is analyzed in terms of objects and nature of communication between them. When a program is executed, objects interact with each other by sending messages. Different objects can also interact with each other without knowing the details of their data or code.

Classes 

class is a collection of objects of similar type. Once a class is defined, any number of objects can be created which belong to that class.

Data Abstraction and Encapsulation 

Abstraction refers to the act of representing essential features without including the background details or explanations. Classes use the concept of abstraction and are defined as a list of abstract attributes.

Storing data and functions in a single unit (class) is encapsulation. Data cannot be accessible to the outside world and only those functions which are stored in the class can access it.

Inheritance 

Inheritance is the process by which objects can acquire the properties of objects of other class. In OOP, inheritance provides reusability, like, adding additional features to an existing class without modifying it. This is achieved by deriving a new class from the existing one. The new class will have combined features of both the classes.

Polymorphism 

Polymorphism means the ability to take more than one form. An operation may exhibit different behaviors in different instances. The behavior depends on the data types used in the operation. Polymorphism is extensively used in implementing Inheritence.

Abstract Classes : 

An abstract classes is the class that is not used to create objects. It is designed to act as a base class that is to be inherited by another class. We can say abstract class is design concept in program development and also provide a base upon which other classes are built. Abstract classes are similar to Interfaces, the abstract classes cannot be instantiated its own.after declaring a abstract class it cannot be instantiated its own. it must be inherited, Like as interfaces abstract classes can specify members that must be implemented in inheriting classes . Abstract Classes can only specify members that should be implemented by all inheriting classes.

Creating Abstract Classes :

In vb.net an abstract class is created by using "MustInherit" keyword. An abstract class like all other classes can implement any number of members. Members of an abstract class can either be Overridable (all the inheriting classes can create their own implementation of the members) or they can have a fixed implementation that will be common to all inheriting members. Abstract classes can also specify abstract members. Like abstract classes, abstract members also provide no details regarding their implementation. Only the member type, access level, required parameters and return type are specified. To declare an abstract member we use the MustOverride keyword. Abstract members should be declared in abstract classes.


Implementing Abstract Class :

When a class is inherited by Abstract Class, It must inherit every abstract member defined in abstract class, Implementation is possible by overriding the member specified in the abstract class.

Following is the code to demonstrate the abstract class,

Module moduloe1
       Public MustInherit Class AbstractClass
             'declaring an abstract class with MustInherit keyword
             Public MustOverride Function Add() As Integer
             Public MustOverride Function Mul() As IntegerEnd Class
       End Class
Public Class A1 inherits A
       Dim i as integer  = 30
       Dim j as integer  = 50
       Public Override Function Add() as Integer
           Return i+j
       End Function
       Public Override Function mul() as integer
           return i*j
       End Function
End Class

Sub Main()
        Dim abs as New A()
        Console.WriteLine("sum is" & "" & abs.add().ToString())
        Console.WriteLine("Multiplication is" & " " & abs.mul().ToString())
       Console. Read()
End Sub
End Module

The Result will be