0% found this document useful (0 votes)
3 views

encapsulation inheritence

The document discusses key concepts in C# programming, focusing on encapsulation and inheritance. It explains encapsulation as a means to protect data through accessors and mutators, and outlines inheritance as a way to derive new classes from existing ones. Additionally, it covers sealed classes and methods, extension methods, and provides code examples to illustrate these concepts.

Uploaded by

Priyanka Naik
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

encapsulation inheritence

The document discusses key concepts in C# programming, focusing on encapsulation and inheritance. It explains encapsulation as a means to protect data through accessors and mutators, and outlines inheritance as a way to derive new classes from existing ones. Additionally, it covers sealed classes and methods, extension methods, and provides code examples to illustrate these concepts.

Uploaded by

Priyanka Naik
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 23

ENCAPUSULATION

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;
}
}

we can't access the private data departname from an


object instance.
We manipulate the data only using those two
methods.
 ENCAPSULATION USING PROPERTIES
using System;
public class Department {
private string departname;
public string Departname {
get {
return departname;
}
set {
departname = value;
}
}
}
public class Departmentmain {
public static int Main(string[] args) {
Department d = new Department();
d.departname = "Communication";
Console.WriteLine("The Department is :{0}", d.Departname);
return 0;
}
}
C# Inheritance
Inheritance (Derived and Base Class)
In C#, it is possible to inherit fields and
methods from one class to another.
We group the "inheritance concept" into two
categories:
Derived Class (child) - the class that inherits
from another class
Base Class (parent) - the class being inherited
from
To inherit from a class, use the : symbol.
In the example below, the Car class (child)
inherits the fields and methods from the
Vehicle class (parent):
class Vehicle // base class (parent)
{
public string brand = "Ford"; // Vehicle
field
public void honk() // Vehicle method
{
Console.WriteLine("Tuut, tuut!");
}
}
class Car : Vehicle // derived class (child)
{
public string modelName = “BMW"; // Car
field
}
class Program
{
static void Main(string[] args)
{
Car myCar = new Car(); // Create a myCar object

// Call the honk() method (From the Vehicle class) on the


myCar object
myCar.honk();

// Display the value of the brand field (from the Vehicle


class) and the value of the modelName from the Car class

Console.WriteLine(myCar.brand + " " +


myCar.modelName);
}
}
C# Inheritance in Constructors
In C#, both the base class and the derived class
can have their own constructor.
The constructor of a base class used to
instantiate the objects of the base class and the
constructor of the derived class used to
instantiate the object of the derived class.
In inheritance, the derived class inherits all the
members(fields, methods) of the base class,
but derived class cannot inherit the
constructor of the base class because
constructors are not the members of the class.
Instead of inheriting constructors by the derived
class, it is only allowed to invoke the constructor
of base class.
DEFINE BASE CLASS CONSTRUCTOR
There is no special rule for defining base
class constructors.
Rules are same as other class constructors.
You can define number of constructor as follows:
class baseclass
{
public baseclass()
{
}
public baseclass(string message)
{
}
}
ACCESS BASE CLASS CONSTRUCTOR IN
DERIVED CLASS
If base class has constructor then child class
or derived class are required to call the
constructor from its base class.
class childclass : baseclass
{
public childclass()
{
}
public childclass(string message): base(message)
{
}
}
Example code
using System;
namespace Inheritance_Constructors
{
class Program
{
static void Main(string[] args)
{
childclass ch = new childclass();
childclass ch1 = new childclass("Hello Constructor");

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;

bool result = i.IsGreaterThan(100);

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.

You might also like