Abstraction is "To represent the essential feature without representing the background details."
Abstraction lets you focus on what the object does instead of how it does it.
Abstraction provides you a generalized view of your classes or objects by providing relevant information.
Abstraction is the process of hiding the working style of an object, and showing the information of an object in an understandable manner.
Real-world Example of Abstraction
Suppose you have an object Mobile Phone.
Suppose you have 3 mobile phones as in the following:
Nokia 1400 (Features: Calling, SMS)
Nokia 2700 (Features: Calling, SMS, FM Radio, MP3, Camera)
Black Berry (Features:Calling, SMS, FM Radio, MP3, Camera, Video Recording, Reading E-mails)
Abstract information (necessary and common information) for the object "Mobile Phone" is that it makes a call to any number and can send SMS.
So that, for a mobile phone object you will have the abstract class as in the following:
abstract class MobilePhone
{
public void Calling();
public void SendSMS();
}
public class Nokia1400 : MobilePhone
{
}
public class Nokia2700 : MobilePhone
{
public void FMRadio();
public void MP3();
public void Camera();
}
public class BlackBerry : MobilePhone
{
public void FMRadio();
public void MP3();
public void Camera();
public void Recording();
public void ReadAndSendEmails();
}
Abstraction means putting all the variables and methods in a class that are necessary.
For example: Abstract class and abstract method.Abstraction means putting all the variables and methods in a class that are necessary.
Abstraction is a common thing.
Example
If somebody in your collage tells you to fill in an application form, you will provide your details, like name, address, date of birth, which semester, percentage you have etcetera.
If some doctor gives you an application to fill in the details, you will provide the details, like name, address, date of birth, blood group, height and weight.
See in the preceding example what is in common?
Age, name and address, so you can create a class that consists of the common data. That is called an abstract class.
That class is not complete and it can be inherited by other classes.
Encapsulation
Wrapping up a data member and a method together into a single unit (in other words class) is called Encapsulation.
Encapsulation is like enclosing in a capsule. That is enclosing the related operations and data related to an object into that object.
Encapsulation is like your bag in which you can keep your pen, book etcetera. It means this is the property of encapsulating members and functions.
class Bag
{
book;
pen;
ReadBook();
}
Encapsulation means hiding the internal details of an object, in other words how an object does something.
Encapsulation prevents clients from seeing its inside view, where the behaviour of the abstraction is implemented.
Encapsulation is a technique used to protect the information in an object from another object.
Hide the data for security such as making the variables private, and expose the property to access the private data that will be public.
So, when you access the property you can validate the data and set it.
Example 1
class Demo
{
private int _mark;
public int Mark
{
get { return _mark; }
set { if (_mark > 0) _mark = value; else _mark = 0; }
}
}
Real-world Example of Encapsulation
Let's use as an example Mobile Phones and Mobile Phone Manufacturers.Real-world Example of Encapsulation
Suppose you are a Mobile Phone Manufacturer and you have designed and developed a Mobile Phone design (a class). Now by using machinery you are manufacturing Mobile Phones (objects) for selling, when you sell your Mobile Phone the user only learns how to use the Mobile Phone but not how the Mobile Phone works.
This means that you are creating the class with functions and by with objects (capsules) of which you are making available the functionality of your class by that object and without the interference in the original class.
Example 2
TV operation
It is encapsulated with a cover and we can operate it with a remote and there is no need to open the TV to change the channel.
Here everything is private except the remote, so that anyone can access the remote to operate and change the things in the TV.
Abstraction
Example: I have class Animal and i am making a sub class of the class Animal called Tiger.
Then i will say that its colour is yellow and running is very fast.
Also i can make the class Hippo but it's colour is black and running is very slow.
This is an example of Abstraction.
Encapsulation:
Example: Just think of a capsule that the doctor gives us.
We just have to take the capsule to get better. We don't have to worry about what medicine is inside the capsule or how it will work on our body.
What it means that we give access to our class via proper methods and properties. The user does not have to worry how this methods and properties work.
Abstraction meas hiding the complexcity of program i.e. it is the manner how write a program in simplest way.
Encapsulation is, wrapping a data and method into a single unit(Class) i.e. hiding all internal details of an objects to outside.
No comments:
Post a Comment