1. Data Abstraction
It can be defined as "presenting the functionality to the USER without presenting the
COMPLEXITY of creation..."
For example, a class Car would be made up of an Engine, Gearbox, Steering objects, and many more components. To build the Car class, one does not need to know how the different components work internally, but only how to interface with them, i.e., send messages to them, receive messages from them, and perhaps make the different objects
composing the class interact with each other.
2. Encapsulation
Encapsulation is a process of binding or wrapping the data and the codes that operates on the data into a single entity. This keeps the data safe from outside interface and misuse. This is implemented using classes.
3. Data Hiding
Data Hiding is implemented with the help of access specifiers.
The data defined by the class are referred to as member variables or instance variables. The code that operates on that data is referred to as member methods or just methods, which define the use of the member variables. Each method or variable in a class may be marked public or private. The private methods and data can only be accessed by the code, that is a member of the class. The public method has all details essential for external users.
3. Polymorphism
To be simple it is " One thing in many different forms”. It comes in two flavors.
Static Polymorphism - compile time polymorphism.
Dynamic Polymorphism - Runtime polymorphism.
Compile Time Polymorphism includes Operator and Function overloading. Run Time Polymorphism includes the Virtual Functions.
Function Overloading
Polymorphism means that functions assume different forms at different times. In case of compile time it is called function overloading. Two or more functions can have same name but their parameter list should be different either in terms of parameters or their data types. The functions, which differ only in their return types, cannot be overloaded. The compiler will select the right function depending on the type of parameters passed.
Operator Overloading
In polymorphism operators can also be overloaded (Compile time polymorphism). Operator Overloading means giving different meaning to the operators. With the help of operator overloading standard operations such as + , - , * , etc can be applied on the objects of the class.
Virtual Functions
A virtual function is a member function of the base class and which is redefined by the derived class. When a derived class inherits the class containing the virtual function, it has ability to redefine the virtual functions.
4. Inheritance
Inheritance is the process by which one object acquires the property of another object.
For instance, a "fruit" is a generalization of "apple", "orange", "mango" and
many others. One can consider fruit to be an abstraction of apple, orange, etc. Conversely, since apples are fruit (i.e., an apple is-a fruit), apples may
naturally inherit all the properties common to all fruit, such as being a fleshy
container for the seed of a plant.
In object oriented programming, inheritance is a way to form new classes using classes that have already been defined. The new classes, known as derived classes, take over (or inherit) attributes and behavior of the pre-existing classes, which are referred to as base classes (or ancestor classes). It is intended to help reuse existing code with little or no modification.
5. Abstract Classes
Abstract classes are one of the essential behaviors provided by .NET. Commonly, you would like to make classes that only represent base classes, and don’t want anyone to create objects of these class types. You can make use of abstract classes to implement such functionality in C# using the modifier abstract. An abstract class means that, no object of this class can be instantiated, but can make derivations of this.
An example of an abstract class declaration is:
abstract class AbsClass
{
}
An abstract class can contain either abstract methods or non abstract methods. Abstract members do not have any implementation in the abstract class, but the same has to be provided in its derived class.
An example of an abstract method:
abstract class AbsClass
{
public abstract void abstractMethod();
}
Also, note that an abstract class does not mean that it should contain abstract members. Even we can have an abstract class only with non-abstract members.
For example:
abstract class AbsClass
{
public void NonAbstractMethod()
{
Console.WriteLine("NonAbstract Method");
}
}
Important rules applied to abstract classes:
An abstract class cannot be a sealed class.
Declaration of abstract methods is only allowed in abstract classes.
An abstract method cannot be private.
The access modifier of the abstract method should be same in both the abstract class and its derived class. If you declare an abstract method as protected, it should be protected in its derived class. Otherwise, the compiler will raise an error.
An abstract method cannot have the modifier virtual. Because an abstract method is implicitly virtual.
An abstract member cannot be static.
6. Interfaces
An interface defines a contract. A class or struct that implements an interface must adhere to its contract. An interface may inherit from multiple base interfaces, and a class or struct may implement multiple interfaces.
An Interface is a reference type and it contains only abstract members. Interface's members can be Events, Methods, Properties and Indexers. But the interface contains only declaration for its members. Any implementation must be placed in class that realizes them. The interface can't contain constants, data fields, constructors, destructors and static members.*All the member declarations inside interface are implicitly public.*If*some interface is inherited, the program must implement all its declared members.
The interface doesn't actually do anything but only has a signature for interaction with other classes or interfaces. A good analogy for an interface is a pencil and a pencil sharpener. When we talk of a pencil and a pencil sharpener we generally know what they do and how they interact. We don't have to know the inner workings of a particular sharpener or pencil, only how to use them through their interface.
*
public interface IPencil
{
void Write();
bool IsSharp { get; }
}
*
public interface IPencilSharpener
{
void Sharpen(IPencil pencil);
}
*
* While the compiler does not enforce it, for consistency with the .NET framework it's a good idea to name interfaces starting with a capital 'I'.*
An interface may be considered as abstract base class containing only the function signatures whose implementation is provided by the child class. In C#, you define such classes as interfaces using the interface keyword. .NET is based on such interfaces. In C#, where you can't use multiple class inheritance, which was previously allowed in C++, the essence of multiple inheritance is achieved through interfaces. That's how your child class may implement multiple interfaces.
An interface has the following properties:
An interface is similar to a pure abstract base class: any non-abstract type inheriting the interface must implement all its members.
An interface cannot be instantiated directly.
Interfaces can contain events, indexers, methods and properties.
Interfaces contain no implementation of methods.
Classes and structs can inherit from more than one interface.
An interface can itself inherit from multiple interfaces
Eg:
using System;
interface myDrawing
{
int originx
{
get;
set;
}
int originy
{
get;
set;
}
void Draw(object shape);
}
class Shape: myDrawing
{
int OriX;
int OriY;
public int originx
{
get{
return OriX;
}
set{
OriX = value;
}
}
public int originy
{
get{
return OriY;
}
set{
OriY = value;
}
}
public void Draw(object shape)
{
... // do something
}
// class's own method
public void MoveShape(int newX, int newY)
{
.....
}
}
7. Virtual Functions
A virtual function is a member function that you expect to be redefined in derived classes. When you refer to a derived class object using a pointer or a reference to the base class, you can call a virtual function for that object and execute the derived class's version of the function. Virtual functions ensure that the correct function is called for an object, regardless of the expression used to make the function call.
Suppose a base class contains a function declared as virtual and a derived class defines the same function. The function from the derived class is invoked for objects of the derived class, even if it is called using a pointer or reference to the base class.
Eg:
class Shape
{
public virtual void Draw()
{
Console.WriteLine("Shape.Draw") ;
}
}
class Rectangle : Shape
{
public override void Draw()
{
Console.WriteLine("Rectangle.Draw");
}
}
class Square : Rectangle
{
public override void Draw()
{
Console.WriteLine("Square.Draw");
}
}
class MainClass
{
static void Main(string[] args)
{
Shape[] shp = new Shape[3];
Rectangle rect = new Rectangle();
shp[0] = new Shape();
shp[1] = rect;
shp[2] = new Square();
shp[0].Draw();
shp[1].Draw();
shp[2].Draw();
}
}
Output:
Shape.Draw
Rectangle.Draw
Square.Draw
Your scripts helping me to recovered my problems. Thanks a lot.
ReplyDelete