OOPS:
It stands for Object Oriented Programming. Object-Oriented Programming (OOP)
It stands for Object Oriented Programming. Object-Oriented Programming (OOP)
Object:
An object can be considered a "thing" that can perform a set of related activities. The set of activities that the object performs defines the object's behavior
An object can be considered a "thing" that can perform a set of related activities. The set of activities that the object performs defines the object's behavior
Class:
A class is simply a representation of a type of object.
Class is composed of three things: a name, attributes, and operations.
A class is simply a representation of a type of object.
Class is composed of three things: a name, attributes, and operations.
Encapsulation:
The encapsulation is mainly achieved by
creating classes; the classes expose public methods and properties. The class
is kind of a container, which encapsulate the set of methods, attribute and
properties to provide its indented functionalities to other classes. In that
sense, encapsulation also allows a class to change its internal implementation
without hurting the overall functioning of the system.
In order to modularize/ define the
functionality of a one class, that class can uses functions/ properties exposed by another class in many different ways. According to Object Oriented
Programming there are several techniques, classes can use to link with each
other and they are named association, aggregation, and composition.
There
are several other ways that an encapsulation can be used, as an example we can
take the usage of an interface. The interface can be used to hide the
information of an implemented class.
Example:
IStudent myStudent = new LocalStudent ();
IStudent myStudent = new ForeignStudent ();
According to the sample above (let’s assume that LocalStudent and ForeignStudent are implemented
by the IStudent interface) we can see how LocalStudent and ForeignStudent are hiding their,
localize implementing information through the IStudent interface.
Association:
Association is a (*a*) relationship
between two classes. It allows one object instance to cause another to perform
an action on its behalf. Association is the more general term that define the
relationship between two classes, where as the aggregation and composition are
relatively special.
Example:
public class StudentRegistrar
{
public StudentRegistrar ();
{
new RecordManager().Initialize();
}
}
In this case we can say that there
is an association between StudentRegistrar and RecordManager or
there is a directional association from StudentRegistrar to RecordManager
or StudentRegistrar use a (*Use*) RecordManager. Since a
direction is explicitly specified, in this case the controller class is the StudentRegistrar.
To some beginners, association is a confusing concept. The troubles created not only by the
association alone, but with two other OOP concepts, that is association, aggregation and
composition.
Everyone understands association, before aggregation and composition are described.
The aggregation or composition cannot be separately understood. If you understand the
aggregation alone it will crack the definition given for association, and if you try to understand the
composition alone it will always threaten the definition given for aggregation, all three concepts
are closely related, hence must study together, by comparing one definition to another.
Let’s explore all three and see whether we can understand the differences between these
useful concepts.
Aggregation:
Aggregation describes a special type of an
association. Aggregation is the (*the*)
relationship between two classes. When object of one class has an (*has*) object of
another, if second is a part of first (containment
relationship) then we called
that there is an aggregation between two classes. aggregation
always insists a
direction.
Example:
public class University
{
private Chancellor universityChancellor = new Chancellor();
}
In this case I can say that University aggregate Chancellor or University has an (*has-a*) Chancellor. But even without a Chancellor a University
can exists. But the Faculties cannot
exist without the University,
the life time of a Faculty (or
Faculties) attached with the life time of the University . If University
is disposed the Faculties will
not exist. In that case we called that University
is composed of Faculties.
So that composition can be recognized as a special type of an aggregation.
Abstraction:
Abstraction reduces complexity by hiding
irrelevant detail, generalization reduces complexity by replacing multiple
entities which perform similar functions with a single construct.
Generalization:
Generalization is the broadening of
application to encompass a larger domain of objects of the same or different
type.
Polymorphism
:-
The
ability to request that same operations be performed by a wide range of
different types of things. The
polymorphisms is achieved by using many
different techniques named
- Method overloading
- Method overriding
- Operator overloading
Method
Overloading:
ü It can be defined as,
ability to define a several methods all with the same name. A
Function can be overloading in two situations
When data type arguments are changed.
When number of arguments are changed.
Example:
Public class MyLogger
{ Public void LogError (Exception e) { // Implementation goes here }
Public bool LogError (Exception e, string message)
{
// Implementation goes here
}
}
Operator Overloading:
The
operator overloading (less commonly known as ad-hoc polymorphisms) is a specific case of polymorphisms in which
some or all of operators like +, - or == are treated as polymorphic functions
and as such have different behaviors depending on the types of its arguments.
Example
public class Complex
{
Private int real;
Public int Real
{
Get
{return real; }
}
Private int imaginary;
Public int Imaginary
{
Get
{return imaginary; }
}
Public Complex (int real, int imaginary)
{
this.real = real;
this.imaginary = imaginary;
}
Public static Complex operator +(Complex c1, Complex c2)
{
Return new Complex (c1.Real + c2.Real,
c1.Imaginary + c2.Imaginary);
}
}
I
above example I have overloaded the plus operator for adding two complex
numbers. There the two properties named Real and Imaginary has been declared
exposing only the required “get” method, while the object’s constructor
is demanding for mandatory real and imaginary values with the user defined
constructor of the class.
Method
Overriding:
Method overriding is a language feature that
allows a subclass to override a specific implementation of a method that is
already provided by one of its super-classes.
A subclass can give its own definition of
methods but need to have the same signature as the method in its super-class. This
means that when overriding a method the subclass's method has to have the same
name and parameter list as the super-class's overridden method.
Example:
Class BC
{
public void Disp()
{
System.Console.WriteLine("BC:: Display");
}
}
Class DC : BC
{
new public void Display()
{
System.Console.WriteLine("DC:: Display");
}
}
Class Demo
{
Public static void Main()
{
BC b;
b = new BC();
b.Disp();
}
}
Difference
between Overloading and Overriding:
Overloading:
- Providing new implementation with same name and different signatures.
- Overloading can be done in same class.
- No separate keyword is used to implement Overloading.
- Used to implement Static Polymorphism.
Overriding:
- Providing new implementation with same name and same signature.
- Overriding can be done in base class and derived class
- Use Virtual keyword for base class function an override keyword in derived class function to implement overriding.
- Used to implement Dynamic Polymorphism.
ü Static or Compile time Polymorphism:
- The method is to be called is decided at compile-time only. Method overloading is an example of this.
- Method overloading is a concept where we use the same method name many times in the same class,but different parameters.
- Depending on the parameters we pass, it is decided at compile-time only.
- The same method name with the same parameters is an error and it is a case of duplication of methods which C# does not permit.
- In Static Polymorphism the decision is made at compile time.
Example:
public Class
StaticDemo
{
public void display(int x)
{
Console.WriteLine("Area of a Square:"+x*x);
}
public void display(int x, int y)
{
Console.WriteLine("Area of a Square:"+x*y);
}
public static void main(String args[])
{
StaticDemo spd=new StaticDemo();
Spd.display(5);
Spd.display(10,3);
}
}
{
public void display(int x)
{
Console.WriteLine("Area of a Square:"+x*x);
}
public void display(int x, int y)
{
Console.WriteLine("Area of a Square:"+x*y);
}
public static void main(String args[])
{
StaticDemo spd=new StaticDemo();
Spd.display(5);
Spd.display(10,3);
}
}
Dynamic or Runtime
Polymorphism.
- Run time polymorphism is also known as method overriding.
- In this mechanism by which a call to an overridden function is resolved at a Run-Time (not at Compile-time)base Class contains a method that is overridden.
- Method overriding means having two or more methods with the same name, same signature but with different implementation.
In this process, an overridden method is called through the
reference variable of a super class the determination of the method to be
called is based on the object being referred to by reference variable.
Example
Class BaseClass
{
Public void show ()
{
Console.WriteLine("From base class show method");
}
}
Public Class DynamicDemo : BaseClass
{
Public void show()
{
Console.WriteLine("From Derived Class show method");
}
Public static void main(String args[])
{
DynamicDemo dpd=new DynamicDemo ();
Dpd.show();
}
}
Virtual Function:
They implement the concept of polymorphism; are the same as in C#, except that you use the override keyword with the virtual function implementation in the child class. The parent class uses the same virtual keyword. Every class that overrides the virtual method will use the override keyword.
They implement the concept of polymorphism; are the same as in C#, except that you use the override keyword with the virtual function implementation in the child class. The parent class uses the same virtual keyword. Every class that overrides the virtual method will use the override keyword.
Why to use them:
- It is not compulsory to mark the derived/child class function with override keyword while base/parent class contains a virtual method
- Virtual methods allow subclasses to provide their own implementation of that method using the override keyword
- Virtual methods can't be declared as private.
- You are not required to declare a method as virtual. But, if you don't, and you derive from the class, and your derived class has a method by the same name and signature, you'll get a warning that you are hiding a parent's method
- A virtual property or method has an implementation in the base class, and can be overriden in the derived classes.
- We will get a warning if we won't use Virtual/New keyword.
- Instead of Virtual we can use New Keyword
Example:
class A
{
public void M()
{
Console.WriteLine("A.M() being called");
}
public virtual void N()
{
Console.WriteLine("A.N() being called");
}
}
class B : A
{
public new void M()
{
Console.WriteLine("B.M() being called");
}
public override void N()
{
Console.WriteLine("B.N() being called");
}
}
say
A a = new B();
a.M();
a.N();
The results would be
A.M() being called
B.N() being called
Inheritance :
Deriving a new class based on existing class.C# does not support multiple class inheritance because of the
diamond problem that is associated, with multiple class inheritance.
No comments :
Post a Comment