Introduction
OOPS or Object Oriented Programming Structures. Some of the concepts have been always used in one or the other programming languages. For example you must have used structs in C which is a good example of encapsulation. There are four major pillar of OOPS. Let’s try to understand each one of them by taking some examples also:-
1.) Encapsulation
2.) Abstraction
3.) Polymorphism
4.) Inheritance
Encapsulation:-
Interview Definition: - Binding data and member functions together inside a single unit.
How to Encapsulate: - By creating types e.g. Classes and Struct
By using encapsulation we can create our own custom types by reusing the existing or primitive types.
Abstraction:-
Abstraction defines way to abstract or hide your data and members from outside world. Simply speaking Abstraction is hiding the complexities of your class or Struct or in a generic term Type from outer world. This is achieved by means of access specifiers.
Interview Definition: - Hiding the complexities of your type from outside world
How to abstract: - By using Access Specifiers
VB.Net has five access specifiers:-
Public — Accessible outside the class through object reference.
Private — Accessible inside the class only through member functions.
Protected — Just like private data but accessible in derived classes also through member functions.
Friend – Friend is Visible inside the assembly and accessible through objects.
Protected Friend – Protected Friend is Visible inside the assembly through objects and in derived classes outside the assembly through member functions.
Let’s try to understand by a practical example:-
Interview Tip:-
The default access specifier for a class is Friend while that of member of class is private
Public Class Class1 Private i As Integer 'No Access specifier means private Public j As Integer ' Public Protected k As Integer 'Protected data Friend m As Integer ' Internal means visible inside assembly Protected Friend n As Integer 'inside assembly as well as to derived classes outside assembly Shared x As Integer ' This is also private Public Shared y As Integer 'Static means shared across objects <DllImport("MyDll.dll")> _ Public Shared Function MyFoo() As Integer End Function 'extern means declared in this assembly defined in some other assembly Public Sub myFoo2() 'Within a class if you create an object of same class then you can access all data members through object reference even private data too Dim obj As New Class1() obj.i = 10 'Should be Error since we can’t access private data through object. But here it is accessible.:) obj.j = 10 obj.k = 10 obj.m = 10 obj.n = 10 ' obj.s =10; //Error Shared data can be accessed by class names only Class1.x = 10 ' obj.y = 10; //Error Shared data can be accessed by class names only Class1.y = 10 End Sub End Class
Now let’s try to copy the same code inside Main method and try to compile
<STAThread> _ Private Shared Sub Main() 'Access specifiers comes into picture only when you create object of class outside the class Dim obj As New Class1() ' obj.i =10; //Error can’t access private data through object. obj.j = 10 ' obj.k=10; //Error can’t access protected data through object. obj.m = 10 obj.n = 10 ' obj.s =10; //Errror Static data can be accessed by class names only Class1.x = 10 'Error can’t access private data outside class ' obj.y = 10; //Errror Static data can be accessed by class names only Class1.y = 10 End Sub
What if Main is inside another assembly?
<STAThread> _ Private Shared Sub Main() 'Access specifiers comes into picture only when you create object of class outside the class Dim obj As New Class1() ' obj.i =10; //Error can’t access private data through object. obj.j = 10 ' obj.k=10; //Error can’t access protected data through object. ' obj.m=10; // Error can’t access internal data outside assembly ' obj.n=10; // Error can’t access internal data outside assembly ' obj.s =10; //Errror Static data can be accessed by class names only Class1.x = 10 'Error can’t access private data outside class ' obj.y = 10; //Errror Static data can be accessed by class names only Class1.y = 10 End Sub