You are on page 1of 12

1Raja Danish

Virtual Function:
In object-oriented programming, a virtual function or virtual method is a
function or method whose behaviour can be overridden within an inheriting class by
a function with the same signature. This concept is a very important part of the
polymorphism portion of object-oriented programming (OOP).

Friend function:
A friend function is used in object-oriented programming to allow access to private or protected
data in a class from the outside. Normally, a function that is not a member of a class cannot
access such information; neither can an external class. Occasionally, such access will be
advantageous for the programmer. Under these circumstances, the function or external class can
be declared as a friend of the class using the friend keyword. The function or external class will
then have access to all information public, private, or protected within the class.
This procedure should be used with caution. If too many functions or external classes are
declared as friends of a class with protected or private data, it lessens the value of encapsulation
of separate classes in object-oriented programming.

Understanding and Using .NET Partial Classes

Learn how to use partial classes for your .NET


applications to improve code readability
and maintainability.
ne of the language enhancements in .NET 2.0available in both VB.NET 2005 and C# 2.0
is support for partial classes. In a nutshell, partial classes mean that your class definition can be
split into multiple physical files. Logically, partial classes do not make any difference to the
compiler. During compile time, it simply groups all the various partial classes and treats them as
a single entity.
One of the greatest benefits of partial classes is that it allows a clean separation of business logic
and the user interface (in particular the code that is generated by the visual designer). Using
partial classes, the UI code can be hidden from the developer, who usually has no need to access
it anyway. Partial classes will also make debugging easier, as the code is partitioned into separate
files.
In this article, I will examine the use of partial classes in more detail and discuss how Visual
Studio 2005 makes use of partial classes.
Using Partial Classes

2Raja Danish

Listing 1 contains two class definitions written in VB.NET, with the second class definition
starting with the partial keyword. Both class definitions may reside in two different physical
files. Functionally, Listing 1 is equivalent to Listing 2.
Listing 1
'---File1.vb--Public Class Class1
Public Sub method1()
End Sub
End Class
File2.vb
Partial Public Class Class1
Public Sub method2()
End Sub
End Class

Listing 2

'---File1.vb--Public Class Class1


Public Sub method1()
End Sub
Public Sub method2()
End Sub
End Class

So, what are the uses for partial classes?


Here are some good reasons to use partial classes:
1. They allow programmers on your team to work on different parts of a class without
needing to share the same physical file. While this is useful for projects that involve big
class files, be wary: If you find your class file getting too large, it may well signal a
design fault and refactoring may be required.
2. The most compelling reason for using partial class is to separate your application
business logic from the designer-generated code. For example, the code generated by
Visual Studio 2005 for a Windows Form is kept separate from your business logic (we
will discuss this in a later section). This will prevent developers from messing with the
code that is used for the UI. At the same time, it will prevent you from losing your
changes to the designer-generated code when you change the UI.
Author's Note: The "partial" keyword in VB.NET used to be called "expands" in prebeta versions of Visual Studio 2005.

Examining Partial Classes


The following code sample shows the class definition of MyClass1. I declared all my properties
in this file. To avoid confusion, I named my class file MyClass1.Properties.vb, in order to make
it obvious that this file contains a properties definition.
'---MyClass1.Properties.vb

3Raja Danish

'---one of the classes need not have the Partial keyword


Public Class MyClass1
Private pX As Integer
Private py As Integer
Property x() As Integer
Get
Return pX
End Get
Set(ByVal value As Integer)
pX = value
End Set
End Property
Property y() As Integer
Get
Return py
End Get
Set(ByVal value As Integer)
py = value
End Set
End Property
End Class

In another file, named MyClass1.Methods.vb, I provide the methods implementation of


MyClass1. I used the Partial keyword to indicate that this definition should be combined with the
original MyClass1 definition.
'---MyClass1.Methods.vb
'---must have the Partial keyword
Partial Public Class MyClass1
Private py As Integer
Public Sub method1()
' implementation here
End Sub
Public Sub method3(ByVal x As Integer, ByVal y As Integer)
' implementation here
End Sub
Public Sub method2()
' implementation here
End Sub
End Class

In reality, you can mix and match properties and method definitions in any of the files, but for
clarity it is a good idea to group properties definitions in one file and methods definitions in
another. The usual rules of OO apply: If there is a method1 in both files, then both method1s
must have unique signatures.
The syntax of partial classes in VB.NET and C# differs slightly. The following shows the
implementation of partial classes in C#:
// In C#, the partial keyword must
// appear in all class definitions
public partial class MyClass1
{
public MyClass1()
{

4Raja Danish

//implementation here

}
public partial class MyClass1
{
//implementation here
}

Besides the order in which the "partial" keyword is placed, the most significant difference is the
strict enforcement of the use of the "partial" keyword in all partial classes in C#. It is mandatory,
whereas in VB.NET, not all of the partial classes have to have the "partial" keyword. This has
caused a significant amount of newsgroup discussion about the rationale for the difference. My
advice is that you should always prefix partial classes with the "partial" keyword. At least this
will give you a visual clue that part of the implementation of the class lies somewhere else, and it
is definitely useful when it comes to debugging.
While partial classes allow you to split the definition of a class into multiple files, you cannot
mix languages. That is, all partial classes must be written in the same language. Besides using the
"partial" keyword for classes, you can also use it for structures and interfaces.
If your class implements multiple interfaces, it is a good idea to use partial classes to contain the
implementation for each interface.

Four Pillars of OOP?


1) Inheritence
2) Polymorphism
3) Abstraction
4) Encapsulation

3) Abstraction In OOP?
class program
{
abstract class animal
{
public abstract void eat();
public void sound()
{
Console.WriteLine("dog can sound");
}
}

5Raja Danish

class dog : animal


{
public override void eat()
{
Console.WriteLine("dog can eat");
}
}
static void Main(string[] args)
{
dog mydog = new dog();
mydog.eat();
mydog.sound();
}
}
OUTPUT:

dog can eat


:

dog can sound

Take an abstract class or an interface for example, it gives the user some method
descriptions, but not the internal implementaion.
Abstraction just gives the functional description but not the implementaion part.

Here you can see we have 2 methods in the Abstract Base Class, the method eat() has no
implementation that is why it is being declared as 'abstract' while the method sound() has its own
body so it is not declared as 'abstract'.
In the derived class we have the same name method but this method has got its body.

Why we use Abstraction?


Abstraction is another good feature of OOPS. Abstraction means to show only the
necessary details to the client of the object. Do you know the inner details of the
Monitor of your PC? What happen when you switch ON Monitor? Does this matter to
you what is happening inside the Monitor?
As a programmer you only need to call the methods, you need not to understand
the functionality of that method. So for this reason in abstraction, we only defines
functions in the abstract class, there is no need to put body here. Because when
user wants to call a method, he just calls it, but its body is not given here. We will
provide the functionality of this method in the other child classes.
So when there is need to change the functionality, then it not impacts on that
programmer who only call this method, because by calling you only call the method,
But the functionality is updated in the child class method.

6Raja Danish

Abstraction VS Encapsulation:
1) Abstraction:
Take an abstract class or an interface for example, it gives the user some method
descriptions, but not the internal implementation.
Abstraction just gives the functional description but not the implementation part.

2) Encapsulation:
Encapsulation is also called information hiding and is used to hide those parts
of your class you do not wish others to see (and hence change). In C++/C# you
hide these parts of your class by using the 'private' keyword. Anything declared
as private will be hidden from developers using your class.
Its mean when you will make an object of this class from out-side this class, then
u cannot access its private methods.
For example in C# we can use properties inside the class to manipulate or access the
private data declared in the same class, but cannot do so from outside functions.

------------------------------- Properties Concept in C# --------------------------

public class myClass


{
private string fName;
private string lName;

public string x
{
set

7Raja Danish

{this.fName = value;}

get
{return fName;}
}

---------------------

myClass objClass = new myClass();


objClass.X = "Danish";
responce.write("First Name:"+ objClass.X);

Generalization VS Sepecialization:
The term "insect" describes a general type of creature with various
characteristics. Because grasshoppers and bumblebees are insects, they have all
the general characteristics of an insect. In addition, they have special
characteristics of their own. For example, the grasshopper has its jumping ability,
and the bumblebee has its stinger. Grasshoppers and bumblebees are specialized
versions of an insect. This is illustrated in the Figure (given below).

8Raja Danish

Difference between Abstraction and


Generalization?
As you know generalization is like inheritance (One (general) base-class and
other (specific)
child-classes).
As in abstraction we only declare methods in Abstract class or interface but its
body is not defined here.
And Generalization is like inheritance so we make child class to provide the
functionality of these methods that are declared in the Parent or Abstract
class.

Association

9Raja Danish

Association is a relationship between two objects. In other words, association defines the
multiplicity between objects. You may be aware of one-to-one, one-to-many, many-to-one,
many-to-many all these words define an association between objects. Aggregation is a special
form of association. Composition is a special form of aggregation.

Example: A Student and a Faculty are having an association.

Aggregation
Aggregation is a special case of association. A directional association
between objects. When an object has-a another object, then you have got
an aggregation between them. Direction between them specified which
object contains the other object. Aggregation is also called a Has-a
relationship.

Composition
Composition is a special case of aggregation. In a more specific manner, a
restricted aggregation is called composition. When an object contains the
other object, if the contained object cannot exist without the existence of
container object, then it is called composition.

Example: A Library contains students and books. Relationship between library and
student is aggregation. Relationship between library and book is composition. A
student can exist without a library and therefore it is aggregation. A book cannot
exist without a library and therefore its a composition.

Abstraction
See above. No image is given for it.

10Raja Danish

Generalization
Generalization uses a is-a relationship from a specialization to the
generalization class. Common structure and behaviour are used from the
specializtion to the generalized class. At a very broader level you can
understand this as inheritance. Why I take the term inheritance is, you can
relate this term very well. Generalization is also called a Is-a relationship.

Example: Consider there exists a class named Person. A student is a person.


A faculty is a person. Therefore here the relationship between student and
person, similarly faculty and person is generalization.

Realization
You can understand this as the relationship between the interface and the
implementing class.

Example: A particular model of a car GTB Fiorano that implements the


blueprint of a car realizes the abstraction.

Dependency
Change in structure or behaviour of a class affects the other related class,
then there is a dependency between those two classes. It need not be the
same vice-versa. When one class contains the other class it this happens.

Example: Relationship between shape and circle is dependency.

11Raja Danish

Structure VS Classes?
Similarities:
1. Both have members, which can include constructors, methods, properties,
fields, constants, enumerations, events, and event handlers.
2. Both members can be declared Public and another Private.
3. Both can implement interfaces.
4. Both can declare and raise events, and both can declare delegates.

Diffrences:
1. Structures are value types; classes are reference types.
2. Structures use stack allocation; classes use heap allocation.
3. All structure elements are Public by default; class variables and constants
are Private by default, while other class members are Public by default.
4. Structure elements cannot be declared as Protected; class members can.
5. Structures are not inheritable; classes are.
6. A structure does not require a constructor; a class does.

12Raja Danish

What is enum?
An enum is a value type with a set of related named constants often referred to as an
enumerator list. The enum keyword is used to declare an enumeration. It is a primitive data
type, which is user defined.
Enums type can be integer (float, int, byte, double etc.). But if you used beside int it has to
be cast.
The default underlying type of the enumeration elements is int. By default, the first
enumerator has the value 0, and the value of each successive enumerator is increased by 1.
// keyword_enum.cs
// enum initialization:
using System;
public class EnumTest
{
enum Days {Sat=1, Sun, Mon, Tue, Wed, Thu, Fri};

static void Main()


{
int x = (int)Days.Sun;
int y = (int)Days.Fri;
Console.WriteLine("Sun = {0}", x);
Console.WriteLine("Fri = {0}", y);
}

Output
Sun = 2
Fri = 7

You might also like