Professional Documents
Culture Documents
Programming
Concepts
Abstraction
Example
Advantages of Abstraction
Helps to manage the complexity of a large
system.
Supports our quality goals of modifiability
and reusability.
Most modifications can be localized to just a
few modules.
Supports thecreation of generic modules
that can be used in other systems.
Abstraction in .NET
Encapsulation
Encapsulation is way to hide data, properties and
methods from outside the world oroutside of the
class scope and exposing only necessary thing.
Encapsulation complements Abstraction.
Abstraction displays only important featuresof a
class and Encapsulation hides unwanted data or
private data from outside of aclass.
Encapsulation can be described as a protective
barrier that prevents the code and data from
being randomly accessed by other code defined
outside the class.
Benefits of Encapsulation
The fields of a class can be made read-only
or write-only.
A class can have total control over what is
stored in its fields.
The users of a class do not know how the
class stores its data. A class can change the
data type of a field and users of the class do
not need to change any of their code.
Access Modifiers
public: Access is not restricted.
protected: Access is limited to the
containing class or types derived from the
containing class.
Internal: Access is limited to the current
assembly.
private: Access is limited to the containing
type.
Property
Properties provide the opportunity to
protect a field in a class by reading and
writing to it through the property.
In other languages, this is often
accomplished by programs implementing
specialized getter and setter methods.
C# properties enable this type of protection
while also letting you access the property
just like it was a field.
Traditional Encapsulation
Without Properties
Creating Auto-Implemented
Properties
Composition
Also known as has-a relationship
It allows an object to be composed of other
objects
Example: A person is composed of a head
object, a body object, 2 hand objects, etc.
Inheritance
Types of Inheritance
Inheritance terminology
Inheritance Benefits
Extensibility
Reusability
Provides abstraction
Eliminates redundant code
Inheritance Example
Class Hierarchies
Generalization and
Specialization
The general characteristics are specified
high up in the hierarchy
More specific characteristics are specified
below the hierarchy
Inheritance in .NET
Simple Inheritance
Example
public class Mammal
{
public int Age { get; set; }
public Mammal(int age)
{
this.Age = age;
}
public void Sleep()
{
Console.WriteLine("Shhh! I'm
sleeping!");
}
}
Inheritance and
Accessibility
class Creature
{
protected string Name { get; private
set; }
private void Talk()
{
Console.WriteLine("I am
creature ...");
}
Inheritance: Adding
Functionality
Subclasses have all of the data members
and methods of the superclass
Subclasses can add to the superclass
Inheritance: Important
Aspects
Polymorphism
Polymorphism
Polymorphism: How it
works?
Polymorphism ensures that the appropriate
method of the subclass is called through its
base class' interface
Polymorphism is implemented using a
technique called late method binding
Virtual Methods
Virtual method is method that can be used in
the same way on instances of base and
derived classes but its implementation is
different
A method is said to be a virtual when it is
declared as virtual
Example:
publicclass Shape
{
publicvirtualvoid Draw()
{
Console.WriteLine("Performing base class drawing tasks");
}
}
-----------------------------------------------------class Circle : Shape
{
publicoverridevoid Draw()
{
Console.WriteLine("Drawing a circle");
base.Draw();
}
}
class Program
{
staticvoid Main()
{
Shape shape = new Shape();
Shape circle = new Circle();
shape.Draw();
circle.Draw();
Console.WriteLine("Press any key to exit.");
Console.ReadKey();
}
}
OUTPUT:
Performing base class drawing tasks
Drawing a circle
Performing base class drawing tasks
Abstract Class
Interface
Declaring an Interface
interfaceIMyInterface
{
voidMethodToImplement();
}
public Pug(string n)
{
name = n;
}
public Lab(string n)
{
name = n;
}
Let's say we run a dog kennel and we need to keep track of all
the dogs were housing. We need to store the pugs and
labradors in separate arrays:
public class Kennel
{
Pug[] pugs = new Pug[10];
Lab[] labs = new Lab[10];
interface IDog
{
public string Bark();
public string GetName();
public boolean HasCurlyTail();
}
Here's where all the extra work pays off. The Kennel class
become much simpler. We need only one array and
oneaddDogmethod. Both will work with any object thatisa dog;
that is, objects that implement the Dog interface.
Another Example: