1) Encapsulation: Wrapping up of data and methods in to a
single unit is called as encapsulation.
Protecting our data
2) Abstraction: Hidding our irrelavance data
3) Inheritence: Process of Aquiring properties from one
object to another without changes.
4) Polymorphism: Process of aquiring properies from one
object to anotherwith changes.Different behaviors at diff. instances
There are two types of polymorphisms.
i)Compile-time polymorphism:what object will be assigned to
the present variable.This will be evaluated at compile time.
This is known as compile-time polymorphism.
ii)Run-time polymorphism:what object will be assigned to the
present variable.This will be evaluated at runtime depending
on condition.This is known as Run-time polymorphism.
5) Message Passing:message passing is possible from one
object to another.
6) Robust and Secure: every object is strong one.
every object is secure one with their access specifiers.
Abstract, Sealed, and Static Modifiers in C#.
C# provides many modifiers for use with types and type members. Of these, three can be used with classes: abstract, sealed and static.
abstract
Indicates that a class is to be used only as a base class for other classes. This means that you cannot create an instance of the class directly. Any class derived from it must implement all of its abstract methods and accessors. Despite its name, an abstract class can possess non-abstract methods and properties.
sealed
Specifies that a class cannot be inherited (used as a base class). Note that .NET does not permit a class to be both abstract and sealed.
static
Specifies that a class contains only static members (.NET 2.0).
Virtual keyword
The virtual keyword allows polymorphism too. A virtual property or method has an implementation in the base class, and can be overriden in the derived classes.
To create a virtual member in C#, use the virtual keyword:
public virtual void Draw()
To create a virtual member in VB.NET, use the Overridable keyword:
Public Overridable Function Draw()
Override keyword
Overriding is the action of modifying or replacing the implementation of the parent class with a new one. Parent classes with virtual or abstract members allow derived classes to override them.
To override a member in C#, use the override keyword:
public override void CalculateArea()
To override a member in VB.NET, use the Overrides keyword:
Public Overrides Function CalculateArea()
What's the difference between override and new in C#?
This is all to do with polymorphism. When a virtual method is called on a reference, the actual type of the object that the reference refers to is used to decide which method implementation to use. When a method of a base class is overridden in a derived class, the version in the derived class is used, even if the calling code didn't "know" that the object was an instance of the derived class. For instance:
public class Base
{
public virtual void SomeMethod()
{
}
}
public class Derived : Base
{
public override void SomeMethod()
{
}
}
...
Base b = new Derived();
b.SomeMethod();
will end up calling Derived.SomeMethod if that overrides Base.SomeMethod.
Now, if you use the new keyword instead of override, the method in the derived class doesn't override the method in the base class, it merely hides it. In that case, code like this:
public class Base
{
public virtual void SomeOtherMethod()
{
}
}
public class Derived : Base
{
public new void SomeOtherMethod()
{
}
}
...
Base b = new Derived();
Derived d = new Derived();
b.SomeOtherMethod();
d.SomeOtherMethod();
Will first call Base.SomeOtherMethod , then Derived.SomeOtherMethod . They're effectively two entirely separate methods which happen to have the same name, rather than the derived method overriding the base method.
If you don't specify either new or overrides, the resulting output is the same as if you specified new, but you'll also get a compiler warning (as you may not be aware that you're hiding a method in the base class method, or indeed you may have wanted to override it, and merely forgot to include the keyword).
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment