.net · asp.net & asp.net mvc

OOPS concept in VB.NET and C#.NET

The following paragraphs explain OOPS concepts in VB.NET and C#.NET.

1) Class:
It is template
It is designed at design time
Ex: paper work (drawing of building plan on paper by civil engineers)

2) Object:
Object can be defined as real entity,
Which occupy some memory and with have life
Ex: constructed building on the basis of paper plan by civil engineers

Class:
Template
No memory
No life
Physical entity

Object:
It has life
It has memory
It design at run time

3) Access keyword in oops (.net)
Access keywords

Access keywords define the access to class members from the same class and from other classes. The most common access keywords are:

·Public: Allows access to the class member from any other class.
·Private: Allows access to the class member only in the same class.
·Protected: Allows access to the class member only within the same class and from inherited classes.
·Internal: Allows access to the class member only in the same assembly.
·Protected internal: Allows access to the class member only within the same class, from inherited classes, and other classes in the same assembly.
·Static: Indicates that the member can be called without first instantiating the class.

Public

Imports system

Public class class1
	Public i as integer
	Public function raghu () as integer
	Console.writeline (i)
	End function
End class

Public class class2
Shared sub main ()
Dim a as sub class1
a.i=10
a.raghu ()
End class

Output=10
Reason: Allows access to the class member from any other class.

Private:

Imports system
Public class class1
	Private i as integer
	Public function raghu () as integer
	Console.writeline (i)
	End function
End class

Public class class2
Shared sub main ()
Dim a as sub class1
a.i=10
a.raghu()
End class

Out put: error
Reason: Allows access to the class member only in the same class.

Conclusion

Public class class1
	Private i as integer
	Public function raghu () as integer
	Console.writeline (i)
	End function
Shared sub main()
Dim a as new class1
a.i=10
a.raghu ()
End sub
End class

Output:10

Protected:

Imports system

Public class class1
	Protected i as integer
	Public function raghu () as integer
	Console.writeline (i)
	End function
End class
Public class class2
Shared sub main ()
Dim a as sub class1
a.i=10
a.raghu ()
End class

Output=error
Reason: Allows access to the class member only within the same class and from inherited classes.

Conclusion:

Imports system
Public class class1
	Protected i as integer=10
	Public function raghu () as integer
	Console.writeline (i)
	End function
End class
Public class class2
Shared sub main ()
Dim a as sub class1
a.raghu ()
End class


Ouput: 10

Friend:

Imports system
Public class class1
         friend i as integer
         public sub hello()
         Console.writeline (i)
         end sub
End class
Public class class2
	Shared sub main ()
	Dim a as new class1 ()
                      a.i=10
                      a.hello()
	End sub
End class


Ouput: 10
Reason: Allows access to the class member only in the same assembly.

Protected friend:

Imports system
Public class class1
     protected friend i as integer
         public sub hello()
         Console.writeline (i)
         end sub
End class

Public class class2
	Shared sub main ()
	Dim a as new class1 ()
                     a.i=10
                      a.hello ()
	End sub
End class

output:10
Reason: Allows access to the class member only within the same class, from inherited classes, and other classes in the same assembly

Static: Indicates that the member can be called without first instantiating the class.

1) Load into memory once (we can save memory)
2) Instance not required

Constructor:

It is a method
It name same as a class (in c#, c++)
In vb.net it is declared as sub new ()
In wont return any value

see for program

Imports system
Public class class1
Private i as integer
         Public sub new ()
          Console.writeline (i)
         End sub

End class
Public class class2
	Shared sub main ()
         Dim a as new class1
         End sub
        End class


Output :0
Reason: default: it will take 0

please see this example

Imports system
Public class class1
Private i as integer=10
         Public sub new ()
          Console.writeline (i)
         End sub

End class
Public class class2
	Shared sub main ()
         Dim a as new class1
         End sub
        End class

output=10

However our .net support two types of constructor
1) Shared
2) Instant

Shared:
1) It won’t accept any parameter
2) So it can’t be overloaded

Instant:
1) It accept parameter
2) So it can be overloaded

Note:
One constructor can be called from other constructor that can be done in vb.net

myclass.new ()
Example:
Imports system
Public class class1

   Public sub new ()
         Console.writeline ("hai")
   End sub
    Public sub new (i as integer)
          Console.writeline (i)
    End sub
End class
Public class class2
	Shared sub main ()
         Dim a as new class1 (10)
         End sub
        End class


Output=10
Reason: because of when passing arguments from object only parameter constructor Will be called

Note : if u want to display two constructor values

Let us see:

  Imports system
Public class class1
   Public sub new ()
         Console.writeline ("hai")
   End sub
    Public sub new (i as integer)
Myclass.new ()
          Console.writeline (i)
    End sub
End class
Public class class2
	Shared sub main ()
         Dim a as new class1 (10)
         End sub
        End class

Output:
Hai
10
Let us consider example

Imports system
Public class class1
   Public sub new ()
         Console.writeline ("hai")
   End sub
    Public sub new (i as integer)
Myclass.new ()
          Console.writeline (i)
    End sub
Shared sub new ()
Console.writeline (“this is raghu”)
End sub

End class
Public class class2
	Shared sub main ()
         Dim a as new class1 (10)
         End sub
        End class


Output:
This is raghu
Hai
10
Reason:
Because of shared constructor will be called first when object is created

Overloading of constructor ()

Imports system
Public class class1
   Public sub new (j as string)
         Console.writeline ("this is raghu”)
   End sub
    Public sub new (i as integer)
Myclass.new (“raghu”)
          Console.writeline (i)
    End sub 

End class
Public class class2
	Shared sub main ()
         Dim a as new class1 (10)
         End sub
End class


Output:
This is raghu
10

Calling of methods while creating one object with help of constructor:

Imports system

Public class class1
Public sub new ()
Console.writeline ("this is raghu")
End sub
End class

Public class class2: inherits class1
Public sub new ()
Console.writeline ("this is raghu1")
End sub
End class
Public class class3: inherits class2

Public sub new ()
Console.writeline ("this is raghu2")
End sub
End class
Public class class4
Shared sub main ()
Dim a as new class3 ()
End sub
End class


Output:
This is raghu
This is raghu1
This is raghu2

Conclusion: when ever object is created constructor will be called first

class &structures

Class: it will take help of heap memory
It is reference type
It can inheritable
It can be over loadable
Structures:
It will take help of stack memory
It is value type
It can’t be inheritable
It can’t be over loadable

Inheritance:

Inheritance in a beautiful concept supported by oops (.net)
Mainly usage of:
1) Reusability
2) Data redundancy
Example:

Imports system
Public class class1
Public sub raghu ()
Console.writeline (“this is raghu”)
End sub
End class

Public class class2: inherits class1
Public sub raghu1
Console.writeline (“this is placid man”)
End sub
End class

Public class class2
Shared sub main ()
Dim a as new class2 ()
a.raghu1 ()
a.raghu ()
End sub
End class


Output:

This is raghu
This is placid man

When ever taking about inheritance so many topics
Like:
1) Overriding
2) Overload able
3) Dynamic dispatcher

Let us consider below example
When a make super class reference is pointing to sub class object then super
class methods will be called
But not sub class
Let us see with example:
Imports system
Public class class1
Public sub raghu ()
Console.writeline (“this is raghu”)
End sub
End class
Public class class2: inherits class1
Public sub raghu1 ()
Console.writeline (“this is placid man”)
End sub
End class
Public class class3
Shared sub main ()
Dim a as class1=new class2 ()
a.raghu1 ()
a.raghu()
End sub
End class
Output: error

Above error can be resolved like as fallow:
Imports system
Public class class1
Public sub raghu ()
Console.writeline (“this is raghu”)
End sub
End class
Public class class2: inherits class1
Public sub raghu1 ()
Console.writeline (“this is placid man”)
End sub
End class
Public class class3
Shared sub main ()
Dim a as class1=new class2 ()

a.raghu ()
End sub
End class
Output:
This is raghu:

When a super class reference is pointing to sub class object and if a method is designed in both classes
In this scenario to get the sub class implementation the sub class can override super class method
To override super class method super class method should be given as a virtual method
To declare virtual methods in the vb.net use key word override able, while overriding in the subclass
Use keyword in the sub class override
Note: 1) to stop overriding use shadows
2) To stop inheritance use keyword not inheritable (final in java)

Let us see example:

Imports system
Public class class1
Public sub raghu ()
Console.writeline (“this is raghu”)
End sub
End class
Public class class2: inherits class1
Public sub raghu ()
Console.writeline (“this is placid man”)
End sub
End class
Public class class3
Shared sub main ()
Dim a as class1=new class2 ()
a.raghu ()
End sub
End class
Output: there is a conflict of same method names of “raghu”
Use keyword like “shadows”

With help of shadows:
Imports system
Public class class1
Public sub raghu ()
Console.writeline (“this is raghu”)
End sub
End class
Public class class2: inherits class1
Public sub raghu ()
Console.writeline (“this is placid man”)
End sub
End class
Public class class3
Shared sub main ()
Dim a as class1=new class2 ()
a.raghu ()
End sub
End class
Output
This is raghu

Imports system
Public class class1
Public sub raghu ()
Console.writeline (“this is raghu”)
End sub
End class
Public class class2: inherits class1
Shadows Public sub raghu ()
Console.writeline (“this is placid man”)
End sub
End class
Public class class3
Shared sub main ()
Dim a as new class2 ()
a.raghu()
End sub
End class

Output:
This is placid man

With help of overridable overrides
Imports system
Public class class1
Public overridable sub raghu ()
Console.writeline (“this is raghu”)
End sub
End class
Public class class2: inherits class1
Public overrides sub raghu ()
Console.writeline (“this is placid man”)
End sub
End class
Public class class3
Shared sub main ()
Dim a as new class2 ()
a.raghu ()
End sub
End class

Output:
This is placid man
Suppose we want to display super class member with help of (mybase.methodname)

Imports system
Public class class1
Public overridable sub raghu ()
Console.writeline (“this is raghu”)
End sub
End class
Public class class2: inherits class1
Public overrides sub raghu ()
Mybase.raghu ()
Console.writeline (“this is placid man”)
End sub
End class
Public class class3
Shared sub main ()
Dim a as new class2 ()
a.raghu ()
End sub
End class

Output:
This is raghu
This is placid man

Abstraction

Abstraction can be defined the way of representing of a class or method with out any background,
Or without any explanation
To create an abstract class in VB.NET, the class declaration should be done as:
Must inherit class class1

Abstract and interfaces
Abstract:
1)It allow two methods
a) Concrete methods
b) Non concrete methods

2) Not possible to create object for abstract classes
3) It can inherited

Interfaces:
1) By default interfaces are public
2) Same as class but except in methods should be abstract
3)And variable should be final
4)It can inherited

See for example:

Imports system

Public interface in1
Function setname () as string
Sub raghu (person name as string)
End interface

Public class class1
Implements in1
Dim name as string
Sub raghu (person name as string) implements in1.raghu
Name=person name
End sub

Function setname () as string implements in1.setname
Console.writeline (name)
End function
End class

Public class class2
Shared sub main ()
Dim a as new class1 ()
a.raghu (“raghu Nethikunta”)
Username ()
End sub
End class

Conclusion:
1) In my above program I have 2absrtact methods setname (), raghu ()
2)Interface cannot be private
3)By default it is public
4)In interface body don’t use access keyword for methods, functions

Late Binding:

Everything is resolved at runtime is called as late binding
But vb support only early binding

Let us see for program better to understand:

Option strict off
Imports system

Public class class1
Public sub raghu ()
Console.writeline (“this is raghu”)
End sub
End class

Public class class2
Public sub raghu ()
Console.writeline (“this is placid man”)
End sub
End class
Public class class3
Public shared sub Display (o as object)
o.raghu()
End sub
Shared sub main ()
Dim a as new class1 ()
Dim b as new class2 ()
Display (a)
Display (b)
End sub
End class

Output:

This is raghu
This is placid man

Conclusion:

If use option strict is on our vb.net complier throw a beautiful error
So take care of option strict while programming for late binding

Pass by (Val, Ref, Out)

Pass by Val

using System;
Public class class1
{
public void hello (int i)
{
i=i+1;
Console.WriteLine (i);
}
static void Main ()
{
Int j=10;
class1 a =new class1();
a. hello (j);
Console.WriteLine (j);
}
}

Output: 11
10

Reason: arguments, parameters both are pointing to different location

Pass by ref

using System;
Public class class1
{
public void hello (ref int i)
{
i=i+1;
Console.WriteLine (i);
}
static void Main ()
{
Int j=10;
class1 a =new class1 ();
a. hello (ref j);
Console.WriteLine (j);
}
}

Output:
11
11

Reason: arguments, parameters both are pointing to same location

Pass byout:

using System;
Public class class1
{
public void hello (out int i)
{
i=i+1;
Console.WriteLine (i);
}
static void Main ()
{
Int j=10;
class1 a =new class1 ();
a. hello (ref j);
Console.WriteLine (j);
}
}
output: error:

Reason: 1)it never accepts any input parameters
2)So it will act as pass by ref (but it not accepts any input parameters)

Let us check:

using System;
Public class class1
{
public void hello (out int i)
{
i=100
i=i+1;
Console.WriteLine (i);
}
static void Main ()
{
Int j=10;
class1 a =new class1 ();
a. hello (ref j);
Console.WriteLine (j);
}
}

output:
101
101

Below table shows equivalent keywords in c#, vb.net

Vb.Net C#. Net
Me This
Shared Static
Shadows New
Overridable Virtual
Overrides Override
Mybase Base
Must Inherit Abstract
Finalize Destructor
Not Inheritable Sealed
Dim Private

Just u observe how c#, vb.net play, really it is magic

I hope that u did lot of enjoy with this stuff
Please mail me is there any mistake in my article

Leave a comment