Object-Oriented Programming Concepts

Naveed Mulla
5 min readAug 22, 2019

Object-Oriented Programming:

· A good understanding of OOPs concepts can help in decision making when designing an application.

  • OOP took the best ideas of Structured Programming and combine with serval new concepts

· Object-Oriented Programming associated with concepts like Class, Object, Abstraction, Encapsulation, Polymorphism, and Inheritance.

Object:

· object is any entity that has state and behavior.

· It is an instance(detailed information) of a Class.

· A real-time example of Object is:

I am Naveed, and I am object/Instance of class Man.

· General Syntax of Object

Man naveed = new Man();

Class:

· Class is which defines the properties and functions of any functional Entity. Like humans having body parts, clothes, etc.

· Collection of an object is Class.

· We define a class using the ‘class’ keyword.

· Ex.

public class humanBeing
{
// declare field properties, event, delegate, and method
}

Abstraction:

· Abstraction is a process of showing only the necessary data to the end-user.

· Abstraction is done while Designing phase.

· The Real-time example of abstraction is Washing Machine.

We just put dirty clothes and adding the washing powder. Washing machine working internally brews the cloths and gives the clean cloths.

You don’t need to know what is happening inside the washing machine. Someone else worried about created a washing machine and that’s acts as “Abstraction” which shows you the only necessary things.

Abstract Class:

·Abstract class is a half define class.

·Abstract class can have a Constructor that is a major difference between abstract class and Interface.

·The Abstract classes are typically used to define a base class in the class hierarchy.

·This class must contain at least one abstract method

·Abstract Method: Abstract Method has no “body” and declared inside the abstract class only.

public abstract void Apple();
// this indicates the method 'Apple()' is abstract

abstract class Fruit
// this indicates the class 'Fruit' is abstract

Encapsulation:

· It is a process of Hiding data from another Developer.

· Encapsulation is implemented by using access specifiers. Like Public, Private, Protected.

§ Public: Any public member can be accessed from outside the class.

§ Private: Only functions of the same class can access its private members.

§ Protected: Protected access specifier allows a child class to access the member variables and member functions of its base class.

· For example

When we buy a new Mobile phone, we don’t see how circuit board works or how memory is managed. This irrelevant information is hidden. That hidden information is called as “Encapsulation”

Polymorphism:

· If one task is performed in different ways, it is known as polymorphism.

· It means to process objects differently based on their data type.

· It is like one method with multiple implementations, and which implementation to be used is decided at run-time depending upon the situation.

· Real-Time example is a Car, you applying the gear but based on the type of gear the action behaves differently. In short ‘Polymorphism’ means many forms

·Polymorphism is also often expressed by one phrase “One Interface, Multiple methods”.

· Polymorphism could be static or dynamic

· The static polymorphism is Method Overloading and dynamic polymorphism called Method Overriding.

For Example:

using System;

namespace polymorphismExample

{

public class Numbers

{

public void addition(int a, int b)

{

Console.WriteLine(a + b);

}

public void addition(int a, int b, int c)

{

Console.WriteLine(a + b + c );

}

}

public class Display

{

static void Main(string[] args)

{

Numbers obj = new Numbers();

obj.addition(3, 7);

obj.addition(9, 4, 2);

Console.ReadKey();

}

}

}

Output:

10

15

Here we implemented Addition but we changed the how many numbers to be added, and then results are different for one addition program.

Inheritance:

· Inheritance allows you to code re-usability when the class includes properties of another class.

· In short one class acquires the properties of another class called “Inheritance.” This is important because it supports the concept of hierarchical classification.

· For Example:

Consider a group of vehicles. You need to create class for Bus and car.

Which has a method of fuelAmount() will the same for both the classes.

For example:

namespace InheritanceExample

{

public class Bus

{

public fuelAmount()

{

Console.WriteLine(“Bus Fuel amount is 40L”);

}

}

public class Car:Bus

{

public Car()

{

Console.WriteLine(“car Fuel Amount is 25L”);

}

}

Output:

Bus fuel amount is 40L

Car fuel amount is 25L

What is Impact analysis?

· It is a key to making sure any change request is delivered on time and in the budget.

· To perform a valid impact analysis, you need a good understanding of the implications of a proposed change.

Relationships

1. Is-a:

Is-a relationships are derived child classes that inherit attributes and methods from their parent class

For example:

class car { }

class BMW extends car { }

This means BMW IS-A car

2. Has-a:

It simply means a composition relationship where class whose attributes are comprised of other classes.

For example:

class wheels{ }

class car{

private wheels wheel;

}

This means car Has-a Wheels

3. Association Relationship:

The association defines the multiplicity between objects. One-to-one, one-to-many, many-to-one, many-to-many all these words define an association between objects.

For example

a student has many subjects (one-to-many)

4. Aggregation Relationship:

When an object ‘has-a’ another object, then you have got an aggregation between them. It specified which object contains the other object.

An aggregation is a special form of association.

5. Composition Relationship:

When an object contains the other object, if the contained object cannot exist without the existence of a container object, then it is called composition.

For Example, A classroom contains students. A student cannot exist without a classroom. There exists a composition relationship between class and students.

A composition is a special form of an aggregation relationship.

#If you are a new learner to C# and .NET technology then recommending you to start with following practical project oriented video: -

--

--

Naveed Mulla

📖Learning is essential to your personal development so keep Learning. ✍