What is Object Oriented Programming?
Procedural programming is about writing procedures or methods that perform operations on the data, while object-oriented programming is about creating objects that contain both data and methods.
Object-oriented programming has several advantages over procedural programming:
- OOP is faster and easier to execute
- OOP provides a clear structure for the programs
- OOP helps to keep the code DRY “Don’t Repeat Yourself”, and makes the code easier to maintain, modify and debug
- OOP makes it possible to create full reusable applications with less code and shorter development time
The Four Pillars of Object Oriented Programming

Encapsulation
Encapsulation is the act of hiding data implementation by restricting access to public methods – Instance variables are set private and accessible only via public methods.
Using Encapsulation we can hide the attributes of _firstName and _lastName to private variables with public accessor methods.
public class Person
{
private string _firstName;
private string _lastName;
public string GetFirstName()
{
return _firstName;
}
public void SetFirstName(string value)
{
_firstName = value;
}
public string GetLastName()
{
return _lastName;
}
public void SetLastName(string value)
{
_lastName = value;
}
}
Abstraction
Abstraction is the process of taking away or removing characteristics from something in order to reduce it to a set of essential characteristics.
To have a concept or idea which is not directly associated to any concrete instance.
Defined in coding terms as “one class should not know the inner details of another in order to use it, just knowing the interfaces should be good enough”
Inheritance
Inheritance is the mechanism of basing an object or class upon another object.
Inheritance often expresses a “is-a” or “has-a” relationship between two objects.
is-a
Here Foo can be defined as “is-a” IBar
public interface IBar
{
}
public class Foo : IBar
{
}
has-a
Here Foo can be defined as “has-a” IBar
public interface IBar
{
}
public class Foo
{
private IBar _bar;
public Foo(IBar bar)
{
_bar = bar;
}
}
The concept of “is-a” is based on class inheritance.
In derived classes we can reuse the code of existing super classes (providing they are not interface definitions)
For example, FileInputStream “is-a” InputStream that reads from a file.
Key Differences between an Abstract class & Interface
An abstract class allows you to create functionality that sub-classes can implement or override.
An interface only allows you to define functionality, not implement it.
Also a class can extend only one abstract class, but it can take advantage of multiple interfaces.
Note for Inheritance
Inheritance can often breaks encapsulation, given that inheritance exposes a subclass to the details of its parent’s implementation.
Polymorphism

Polymorphism means “many forms“, and it occurs when we have many classes that are related to each other by inheritance.
Inheritance lets us inherit attributes and methods from another class.
Polymorphism uses those methods to perform different tasks.
This allows us to perform a single action in different ways.
Example
Think of a super-class called Animal that has a method called Sound(). Sub-classes of Animal could be Pig, Cat, Dog, Bird
And they would also have their own implementation of an Animal Sound() (the pig oinks, and the cat meows, etc.):
class Animal
{
public void Sound()
{
Console.WriteLine("The animal makes a sound");
}
}
class Pig : Animal
{
public void Sound()
{
Console.WriteLine("oink!");
}
}
class Dog : Animal
{
public void Sound()
{
Console.WriteLine("bark!");
}
}
public static void Main(string[] args)
{
Animal myAnimal = new Animal();
Animal myPig = new Pig();
Animal myDog = new Dog();
myAnimal.Sound();
myPig.Sound();
myDog.Sound();
}
Console output:
The animal makes a sound
oink!
bark!
That’s Polymorphism!
Notes for Polymorphism
- A class must pass a “is-a” test for
objectand their owntypeto be polymorphic. - The reference variable’s type will determine the methods that can be invoked on the object for polymorphism – for the example above, the
myDogvariable can only access methods for theAnimalclass because themyDogwas declared as thattype. - Polymorphic method invocation applies only to the instance methods not to static methods or variables.

Leave a comment