> What is encapsulation, inheritance, polymorphism and abstraction in c#?

What is encapsulation, inheritance, polymorphism and abstraction in c#?

Posted at: 2014-12-18 
What is encapsulation, inheritance, polymorphism and abstraction in c#?

I need clear and simple answers.

Note: Expect supplied examples to contain typos and bugs. They are supplied for example and are not fully tested.

Encapsulation: http://en.wikipedia.org/wiki/Encapsulati...

Encapsulation means that an internal class object is hidden from view outside the definition. Lets try an easy example with the class Foo and the variable Bar. Without encapsulation:

class Foo {

public:

int bar;

}

An instance of Foo (I.E: Foo bat; bat in this case) has access and can change the value of bar; However:

class Foo {

public:

int getBar() { return bar; }

int setBar( int x ) { bar = x; }

int addBar( int x ) { bar += x; }

private:

int bar;

}

In this example an instance of Foo can not directly see or change the interger bar, it is encapsulated inside the class. To change the value Foo's functions would have to be called. I.E. Foo bat; bat.setBar(10); rather than the uncapsulated version where we would do: Foo bat; bat.bar = 10;

-------------

Polymorphism: http://en.wikipedia.org/wiki/Polymorphis...

Polymorphism is a way of treating an instance of different objects the same even though the only thing they have in common is the core class. I.E.

class Foo {

virtual std::string saySomething() { std::cout << Foo; }

}

class Bar: public Foo {

std::string saySomething() { std::cout << "Bar"; }

}

class Bat: public Foo {

std::string saySomething() { std::cout << "Bat"; }

Foo* myObject;

myObject = new Bar;

myObject->saySomething; // would say "Bar" since that's the object we created.

delete( MyObject );

myObject = new Bat;

myObject->saySomething; // would say "Bat" since that's the object we created this time

Even though myObject was declared as a Foo pointer (Foo*) we actually put instances of Bar and Bat in there, since they are derived from Foo it is legal. When the object is actually used there is some magic that happens that figures out what type of object it actually is (polymorphism) and calls the correct function. Logically we see it as there is an object of Bat there and so Bat's function is called. If it's an object of Bar then Bar's function will be called.

--------------------------

Inheritance: en.wikipedia.org/wiki/Inheritance_(objec...

In my polymorphism example Bat and Bar both "inherited" from Foo since Foo was the base class. That is, Bat and Bar will have everything that Foo has, variables, functions, etc... It "inherited" them from it's "parent" (the base class).

----------------------------

Abstraction: http://en.wikipedia.org/wiki/Abstraction...

In C++ (C#) this is usually handled by virtual methods such that the function is not defined in the parent class but only declared. It is up to the child classes to define the implementation.

Lets relook at the polymorphism example. In Foo we declared the saySomething method using the virtual keyword.

virtual std::string saySomething() { std::cout << Foo; }

Without that virtual keyword the compiler wouldn't know that this method was going to be overridden by child classes and without it any call to .saySomething() would respond with "Foo" because then the saySomething class wouldn't be abstract.

We can even do pure abstraction:

class Foo {

virtual std::string saySomething() = 0;

}

that = 0 specially means this is a "pure virtual" method. A derived class *must* declare a saySomething method or it won't compile. If it is not pure vritual a derived class without the method definition would use the base clases definition.

So basically abstraction means that this method is an abstract concept that can be (or must be) changed by derived classes.

Polymorphism deals with objects. Inheritance deals with methods.

What is encapsulation, inheritance, polymorphism and abstraction in c#?

I need clear and simple answers.