encapsulation inheritence
encapsulation inheritence
INHERITENCE
Encapsulation
Encapsulation is the procedure of encapsulating
data and functions into a single unit (called class).
Why do we need encapsulation
The need of encapsulation is to protect or prevent
the code (data) from accidental corruption due to
the silly little errors that we are all prone to make.
In Object oriented programming data is treated as
a critical element in the program development
and data is packed closely to the functions that
operate on it and protects it from accidental
modification from outside functions.
ENCAPSULATION USING ACCESSORS AND MUTATORS
To manipulate the data in that class (String departname)
we define an accessor (get method) and mutator (set
method).
Using System;
public class Department {
private string departname;
// Accessor.
public string GetDepartname()
{
return departname;
}
// Mutator.
public void SetDepartname(string a)
{
departname = a;
}
}
Public class program
{
public static int Main(string[] args)
{
Department d = new Department();
d.SetDepartname(“MCA");
Console.WriteLine("The Department is :" + d.GetDep
artname());
return 0;
}
}
Console.ReadKey();
}
}
class baseclass
{
public baseclass()
{
Console.WriteLine("I am Default Constructors");
}
public baseclass(string message)
{
Console.WriteLine("Constructor Message : " +
message);
}
}
public class childclass : baseclass
{
public childclass()
{
}
public childclass(string message): base(message)
{
}
}
}
What is sealed class and sealed method?
Sealed Class: Sealed class is used to define the inheritance
level of a class.
The sealed modifier is used to prevent derivation from a
class.
An error occurs if a sealed class is specified as the base class
of another class.
Some points to remember:
A class, which restricts inheritance for security reason is
declared sealed class.
Sealed class is the last class in the hierarchy.
Sealed class can be a derived class but can't be a base class.
A sealed class cannot also be an abstract class. Because
abstract class has to provide functionality and here we are
restricting it to inherit.
Sealed Methods
Sealed method is used to define the overriding level of a
virtual method.
Sealed keyword is always used with override keyword.
using System; public class
namespace Derived : BaseClass
sealed_class
{ {
class Program // this Derived class
{ can;t inherit
public sealed BaseClass because
class BaseClass it is sealed
{ }
public void static void
Display() Main(string[] args)
{
{ Console.Writ BaseClass obj
eLine("This is a = new BaseClass();
sealed class which obj.Display();
can’t be further
inherited"); Console.ReadLine();
Without sealed method With sealed method
example example
namespace SealedDemo namespace SealedDemo
{
{
class Parent
class Parent
{
{
public virtual void Show()
public virtual void Show() {}
{} }
} class Child : Parent
class Child : Parent {
{ public sealed override
public override void Show() void Show() { }
{} }
} class GrandChild : Child
class GrandChild : Child {
//'GrandChild.Show()':
{
cannot override inherited
public override void Show() member 'Child.Show()'
{} because it is sealed
The point that you need to
remember is every method by
default is sealed in C# and hence
they cannot be overridden under
the child class.
But if you declare a method as
virtual then that method can be
overridden under the child classes
as well as under the grandchild
classes also.
If you want to restrict the method,
not be overridden under the
grandchild classes then you need
C# - Extension Method
Extension methods, as the name suggests,
are additional methods.
Extension methods allow you to inject
additional methods without modifying,
deriving or recompiling the original class,
struct or interface.
Extension methods can be added to your
own custom class, .NET framework classes,
or third party classes or interfaces.
In the following
example, IsGreaterThan() is an extension
method for int type, which returns true if
the value of the int variable is greater than
The IsGreaterThan() method is not a method of int
data type (Int32 struct).
It is an extension method written by the
programmer for the int data type.
The IsGreaterThan() extension method will be
available throughout the application by including
the namespace in which it has been defined.
The extension methods have a special symbol in
intellisense of the visual studio, so that you can
easily differentiate between class methods and
extension methods.
An extension method is actually a special kind
of static method defined in a static class.
To define an extension method, first of all,
define a static class.
For example, we have created
an IntExtensions class under
the ExtensionMethods namespace in the
following example.
The IntExtensions class will contain all the
extension methods applicable to int data type.
namespace ExtensionMethods
{
public static class IntExtensions
{}
}
Now, define a static method as an extension
method where the first parameter of the extension
method specifies the type on which the extension
method is applicable.
We are going to use this extension method on int
type. So the first parameter must be int preceded
with the this modifier.
namespace ExtensionMethods
{
public static class IntExtensions
{
public static bool IsGreaterThan(this int i, int value)
{
return i > value;
}
}
}
Now, you can include the ExtensionMethods
namespace wherever you want to use this
extension method.
Example: Extension method
using ExtensionMethods;
class Program
{
static void Main(string[] args)
{
int i = 10;
Console.WriteLine(result);
}
}
Points to Remember :
Extension methods are additional custom
methods which were originally not included
with the class.
Extension methods can be added to
custom, .NET Framework or third party
classes, structs or interfaces.
The first parameter of the extension method
must be of the type for which the extension
method is applicable, preceded by
the this keyword.
Extension methods can be used anywhere in
the application by including the namespace of
the extension method.