Skip to content

OOPS Concepts in real life

Reading Time: 3 minutes

Oops concepts in real life

Objects:

It is considered to be an instance of class.It contains real values instead of variables.In real life we can consider an mobile company which manufacture mulitple models as instance to mobile.so here mobile models where each one has its unique characteristics acts as a object.

Example:

Mobile samsung=new Mobile();
Mobile nokia=new Mobile();

so here in the above example mobile is a class and its models samsung and nokia which has their own characteristics acts as an object.

Classes:

It is considered to be an blueprint for the objects created.In other words it defines the methods and variables for particular object.

For Example here also we can consider mobile as a class in real life .Mobile has some values for its variables like profile type,IMEI number,processor etc and it has methods like dial,receive,reject,hold,send mms,etc.

Example:

public class Mobile
{
private string IEMICode { get; set; }
public string SIMCard { get; set; }
public string Processor { get; }
public int InternalMemory { get; }
public bool IsSingleSIM { get; set; }

public void GetIEMICode()
{
Console.WriteLine("IEMI Code - IEDF34343435235");
}

public void Dial()
{
Console.WriteLine("Dial a number");
}
public void Receive()
{
Console.WriteLine("Receive a call");
}
public virtual void SendMessage()
{
Console.WriteLine("Message Sent");
}
}

As we said before here in this example mobile acts as a class and it has  methods such as getimeicode,sendmessage,dial,receive.

Abstraction:

It shows only the essential information rather than a very long description.

Consider an example where we are pressing on green color icon on our mobile to attend calls.But we are unaware of it inner functionality is what abstraction means that is it provides only relevant details to end user rather than irrelevant details.

See also  5 Easy Steps to Optimize PHP Scripts for Fast Loading 🚀

Another example we can say is dialling a number shows a number on our display immediately but we doesn’t know what functionality it is performing inside.

Example:

public void Dial()
{
//Write the logic
Console.WriteLine("Dial a number");
}

 

Encapsulation:

Access rights provided to certain details of the class.In other words it provides access rights to details provided by abstraction.

Consider an real world example where our mobile has bluetooth which we activate to connect to other mobile near by .Suppose say if mobile A is connected to B  via bluetooth at the same time mobile C tries to connect to mobile A it will surely gets failed .This is due to accessibility restriction provided by Encapsulation.It is performed by three access specifiers which are namely public protected and private.

Example:

private string IMEICode = "76567556757656";

 

Polymorphism:

It is defined as the same operation which can be done by providing different inputs at different times.

Consider an example where nokia mobile has 5MP camera which has a functionality cameraclick .But if we like to take a click with panaromal mode here the concept of polymorphism is applied to make it effect where the functionality of camera click is same but mode differs.

public class Samsumg : Mobile
{
public void GetWIFIConnection()
{
Console.WriteLine("WIFI connected");
}

//This is one mwthod which shows camera functionality
public void CameraClick()
{
Console.WriteLine("Camera clicked");
}

//This is one overloaded method which shows camera functionality as well but with its camera's different mode(panaroma)
public void CameraClick(string CameraMode)
{
Console.WriteLine("Camera clicked in " + CameraMode + " Mode");
}
}

It is an very good example for static or compile time polymorphism.It is also reffered as method overloading.

Another term dynamic polymorphism is one where functionality differs with the same method.

See also  Why Choose Laravel for Your Next Project? - 25 Laravel Advantages

Consider an real scenario where we are sending an message from our mobile to a single person or group of persons.Here operation  sending message which is same for both the different functionalities.It is also termed as method overriding.

Leave a Reply