i find so many times developers confusing with the term Abstract or abstraction in OOPS. Here's a perfect description and definition of abstraction in OOPs. Hope it will help u better understanding of Abstract and OOPs.
Abstraction
Abstraction is another good feature of OOPS. Abstraction means to show only the necessary details to the client of the object. Do you know the inner details of the Monitor of your PC? What happen when you switch ON Monitor? Does this matter to you what is happening
inside the Monitor? No Right, Important thing for you is weather Monitor is ON or NOT. When you change the gear of your vehicle are you really concern about the inner details of your vehicle engine? No but what matter to you is that Gear must get changed that’s
it!! This is abstraction; show only the details which matter to the user.
Let’s say you have a method "CalculateSalary" in your Employee class, which takes EmployeeId as parameter and returns the salary of the employee for the current month as an integer value. Now if someone wants to use that method. He does not need to care about
how Employee object calculates the salary? An only thing he needs to be concern is name of the method, its input parameters and format of resulting member, Right?
So abstraction says expose only the details which are concern with the user (client) of your object. So the client who is using your class need not to be aware of the inner details like how you class do the operations? He needs to know just few details. This
certainly helps in reusability of the code.
As I have generally seen developers are not very much comfortable with the database programming. Let’s say you are designing a class that is used to interact with the database and to perform some of database operations. Now client of your class need not to
be aware of database programming, he just need to be aware of some of the details of your class and easily can perform the database operations exposed by your class without deep knowledge of database programming.
The best thing of abstract is that this decouples the user of the object and its implementation. So now object is easy to understand and maintain also. As if there is any change in the process of some operation. You just need to change the inner details of
a method, which have no impact on the client of class.
Sudhir Thanki
Software Engineer
Microsoft Certified Technical Specialist.
Abstraction means that you have some class that is more common than others that extend it. By example, if you have classes Triangle and Rectangle:
class Triangle
{
public double a;
public double b;
public double c;
public double Area
{
get { return triangle's area }
}
}
class Rectangle
{
public double a;
public double b;
public double Area
{
get { return rectangle's area }
}
}
These classes have something in common - they have property called Area plus also sides (a and b) but I don't see any reason to make sides abstract. Let's add also Circle and we have no point of sides at all. Now let's generalize these classes and let's
create one more common class called Shape. Also, let's make it abstract class so this class cannot be created separately - it can be created only through extending.
public abstract class Shape
{
public double Area();
}
And now let's write previous classes so they extend Shape class.
class Triangle : Shape
{
public double a;
public double b;
public double c;
public double Area
{
get { return triangle's area }
}
}
class Rectangle : Shape
{
public double a;
public double b;
public double Area
{
get { return rectangle's area }
}
}
So, what's the win, you may ask? Okay, not of these classes use Shape as their base class. So does Circle. In the context where we don 't care about specific properties of object we can handle all extended objects as Shape. By example, let's calculate
total area of Shapes in list.
List<Shape> shapes = ListShapes() // contains circles, triangles and rectangles
double area = 0;
foreach(Shape shape in shapes)
area += shape.Area;
// do something useful with area here
Without extending Shape class we should write separate loop for each shape type. And also we have to have separate methods to get different types of shapes. Now we had only one method to list shapes and one loop to handle them as one - we didn't cared
about lengths of sides or radiuses or any other specific properties. Same way you can make many other abstractions. By example, you can add coordinates to Shape class if shape's positioning is required.
Don't forget to mark solution providing post as "Answered".
Click "Mark as Answer" on the post that helped you.
This earns you a point and marks your thread as Resolved so we will all know you have been helped.
FAQ on the correct forum http://forums.asp.net/p/1337412/2699239.aspx#2699239
To me abstraction is the way we see things for example in my car I never know that how many devices are used in my engine, how petrol burns, how gear system changes the speed of my car, I only knows that when I press accelator my car moves fast and when
I press breaks my car slows down and I can adjust speed of my car using gears.
Similarly in object oriented world, we always try to hide the complexity of a system from its end user e.g. I have a chat system which interacts with another chat client. Then i will try to create a abstraction layer which will hide all the complexity of
TCP/IP protocols and give user simple functions like SendMessage or RecieveMessage.
Thanks,
Zeeshan Umar ~Please Mark As Answer, one or multiple posts, which helped you. So that it might be useful for others~
Abstraction means focus on the most important part at first. It comes in levels. Highest level focus on the most important aspect of the proposed syste. Next level contains more details and as we come down in lower levels, we solve detailsstep by step. This
ease over all development.
<div style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;" id="_mcePaste">Well as per my understanding inheritance and Abstract class are both .net way of implementing Abstraction.</div> <div
style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;" id="_mcePaste"></div> <div style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y:
hidden;" id="_mcePaste">Interfaces are a contract, that would tell you what you need to access, like for a "Icar" you have gear,steering. (you decide where and how your steering is (left/right/power/nonpower etc) ) but you sign a contract with "car" saying
that for every car you would have gear and steering.</div> <div style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;" id="_mcePaste"></div> <div style="position: absolute; left: -10000px; top:
0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;" id="_mcePaste">Abstract Class: Now imagine you want to provide functionality along with this contract, i.e you say gear and steering are yours to implement but I would also provide you
with a method of autogear(implementation of which AbstractCar class would define)</div>
Well as per my understanding interface and Abstract class are both .net way of implementing Abstraction.
Interfaces are a contract, that would tell you what you need to access, like for a "Icar" you have gear,steering. (you decide where and how your steering is (left/right/power/nonpower etc) ) but you sign a contract with "car" saying that for every car you
would have gear and steering.
Abstract Class: Now imagine you want to provide functionality along with this contract, i.e you say gear and steering are yours to implement but I would also provide you with a method of autogear(implementation of which AbstractCar class would define)
Example:
using System;
namespace AbstractCarExample
{
class Program
{
static void Main()
{
(new Honda()).DriveHonda();
(new Nissan()).DriveNissan();
Console.ReadKey();
}
}
//Abstraction using interface
//Notice that a contract has to be followed for Honda to be ICar,
//also note that ICar does not have any functionality
public interface ICar
{
void Steering();
void Gear();
}
public class Honda:ICar
{
public void Steering()
{
Console.WriteLine("Location: Left, Power");
}
public void Gear()
{
Console.WriteLine("5 spd Automatic.");
}
public void Start()
{
Console.WriteLine("Honda defines its own start mechanism for car.");
}
public void DriveHonda()
{
Console.WriteLine("Driving Honda Car:");
Start();
Steering();
Gear();
}
}
//Abstract Class can encapsulate default functionality
//Here we have provided start mechanism.
//Also, notice that abstract class becomes ideal candidate for base class.
public abstract class CarBase
{
//Abstract class gives you freedom to restrict access you may not do it in interface.
protected abstract void Steering();
internal abstract void Gear();
//Here you define Start mechanism for all your child classes
public void Start()
{
Console.WriteLine("Car should start with a key.");
}
}
public class Nissan : CarBase
{
//Interface and abstract class both ensured that you write only those methods
//which are absolutly necessary for you.
protected override void Steering()
{
Console.WriteLine("Location: Left, Power");
}
internal override void Gear()
{
Console.WriteLine("5 spd Automatic.");
}
//Here abstract class gives you benifit of using encapsulation to call car start.
//I am hiding my method just for demo :) not a good idea :D in real life
Member
20 Points
12 Posts
What is abstraction in OOPS???
May 09, 2008 04:17 AM|softsan|LINK
Hi,
i find so many times developers confusing with the term Abstract or abstraction in OOPS. Here's a perfect description and definition of abstraction in OOPs. Hope it will help u better understanding of Abstract and OOPs.
Abstraction
Abstraction is another good feature of OOPS. Abstraction means to show only the necessary details to the client of the object. Do you know the inner details of the Monitor of your PC? What happen when you switch ON Monitor? Does this matter to you what is happening inside the Monitor? No Right, Important thing for you is weather Monitor is ON or NOT. When you change the gear of your vehicle are you really concern about the inner details of your vehicle engine? No but what matter to you is that Gear must get changed that’s it!! This is abstraction; show only the details which matter to the user.
Let’s say you have a method "CalculateSalary" in your Employee class, which takes EmployeeId as parameter and returns the salary of the employee for the current month as an integer value. Now if someone wants to use that method. He does not need to care about how Employee object calculates the salary? An only thing he needs to be concern is name of the method, its input parameters and format of resulting member, Right?
So abstraction says expose only the details which are concern with the user (client) of your object. So the client who is using your class need not to be aware of the inner details like how you class do the operations? He needs to know just few details. This certainly helps in reusability of the code.
As I have generally seen developers are not very much comfortable with the database programming. Let’s say you are designing a class that is used to interact with the database and to perform some of database operations. Now client of your class need not to be aware of database programming, he just need to be aware of some of the details of your class and easily can perform the database operations exposed by your class without deep knowledge of database programming.
The best thing of abstract is that this decouples the user of the object and its implementation. So now object is easy to understand and maintain also. As if there is any change in the process of some operation. You just need to change the inner details of a method, which have no impact on the client of class.
Software Engineer
Microsoft Certified Technical Specialist.
Contributor
4050 Points
1004 Posts
MVP
Re: What is abstraction in OOPS???
May 09, 2008 05:26 AM|DigiMortal|LINK
Also visit my ASP.NET blog or follow me @ Twitter:twitter.com/gpeipman
Member
161 Points
46 Posts
Re: What is abstraction in OOPS???
May 09, 2008 01:49 PM|anand_vijayan|LINK
I agree with DigiMortal. Abstraction in the object oriented world generalizes the characteristics and behaviours. The following classes describe this
public class Mammal
{
public string m_color; // Abstract Charactersistic 1
public string m_height; // Abstract Charactersistic 2
public string m_weight; // Abstract Charactersistic 1
public Mammal()
{
}
public void Move()
{
// Abstract behaviour
}
}
// Elephant is a Mammal hence it extends abstract class Mammal
public class Elephant : Mammal
{
public string m_color = "gray"; // Gray color
public int m_height = 120; // 10 feet or 120 inches
public int m_weight = 1500; // 1500 pounds
public Elephant()
{
}
public void Move()
{
// Implement the Elephant Walks Behaviour
}
}
// A whale is also a mammal hence it extends the Mammal class
public class Whale: Mammal
{
public string m_color = "darkgray"; // Dark Gray color
public int m_height = 60; // 5 feet or 60 inches
public int m_weight = 2000; // 2000 pounds
public Whale()
{
}
public void Move()
{
// Implement the way a Whale swims
}
}
Anand Vijayan
--------------------------------------------------
If this is your answer please mark it as answered.
None
0 Points
1 Post
Re: What is abstraction in OOPS???
Dec 11, 2009 07:53 AM|ashes_onfire|LINK
The example that you have about abstraction depicts inheritance
1. Can you create an object of Abstract class Shape? i guess no
2. What did you achieve by using Abstract class when this could have been done using Inheritance anyways
I'm still not sure about what does Abstraction mean
None
0 Points
2 Posts
Re: What is abstraction in OOPS???
Feb 01, 2010 07:42 AM|KKhushboo|LINK
"Abstraction" simply means showing only those details to the user which are of use to them , and hiding up the unnecessary
portion.
All-Star
44551 Points
13496 Posts
MVP
Re: What is abstraction in OOPS???
Feb 04, 2010 03:05 AM|TATWORTH|LINK
Please see
This earns you a point and marks your thread as Resolved so we will all know you have been helped.
FAQ on the correct forum http://forums.asp.net/p/1337412/2699239.aspx#2699239
Star
10444 Points
2463 Posts
Re: What is abstraction in OOPS???
Feb 12, 2010 06:53 AM|sirdneo|LINK
To me abstraction is the way we see things for example in my car I never know that how many devices are used in my engine, how petrol burns, how gear system changes the speed of my car, I only knows that when I press accelator my car moves fast and when I press breaks my car slows down and I can adjust speed of my car using gears.
Similarly in object oriented world, we always try to hide the complexity of a system from its end user e.g. I have a chat system which interacts with another chat client. Then i will try to create a abstraction layer which will hide all the complexity of TCP/IP protocols and give user simple functions like SendMessage or RecieveMessage.
Zeeshan Umar
~Please Mark As Answer, one or multiple posts, which helped you. So that it might be useful for others~
None
0 Points
1 Post
Re: What is abstraction in OOPS???
Feb 22, 2010 09:05 PM|pointdesi|LINK
Abstraction is the process or result of generalization by reducing the information content of a concept or an observable phenomenon
Member
31 Points
48 Posts
Re: What is abstraction in OOPS???
Apr 02, 2010 04:38 PM|gauravvmahhajan|LINK
Abstraction means focus on the most important part at first. It comes in levels. Highest level focus on the most important aspect of the proposed syste. Next level contains more details and as we come down in lower levels, we solve detailsstep by step. This ease over all development.
ASP.NET abstraction gaurav mahajan
None
0 Points
2 Posts
Re: What is abstraction in OOPS???
Jun 29, 2010 04:15 PM|mayank.me|LINK
<div style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;" id="_mcePaste">Well as per my understanding inheritance and Abstract class are both .net way of implementing Abstraction.</div> <div style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;" id="_mcePaste"></div> <div style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;" id="_mcePaste">Interfaces are a contract, that would tell you what you need to access, like for a "Icar" you have gear,steering. (you decide where and how your steering is (left/right/power/nonpower etc) ) but you sign a contract with "car" saying that for every car you would have gear and steering.</div> <div style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;" id="_mcePaste"></div> <div style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;" id="_mcePaste">Abstract Class: Now imagine you want to provide functionality along with this contract, i.e you say gear and steering are yours to implement but I would also provide you with a method of autogear(implementation of which AbstractCar class would define)</div>Well as per my understanding interface and Abstract class are both .net way of implementing Abstraction.
Interfaces are a contract, that would tell you what you need to access, like for a "Icar" you have gear,steering. (you decide where and how your steering is (left/right/power/nonpower etc) ) but you sign a contract with "car" saying that for every car you would have gear and steering.
Abstract Class: Now imagine you want to provide functionality along with this contract, i.e you say gear and steering are yours to implement but I would also provide you with a method of autogear(implementation of which AbstractCar class would define)
Example:
using System;
namespace AbstractCarExample
{
class Program
{
static void Main()
{
(new Honda()).DriveHonda();
(new Nissan()).DriveNissan();
Console.ReadKey();
}
}
//Abstraction using interface
//Notice that a contract has to be followed for Honda to be ICar,
//also note that ICar does not have any functionality
public interface ICar
{
void Steering();
void Gear();
}
public class Honda:ICar
{
public void Steering()
{
Console.WriteLine("Location: Left, Power");
}
public void Gear()
{
Console.WriteLine("5 spd Automatic.");
}
public void Start()
{
Console.WriteLine("Honda defines its own start mechanism for car.");
}
public void DriveHonda()
{
Console.WriteLine("Driving Honda Car:");
Start();
Steering();
Gear();
}
}
//Abstract Class can encapsulate default functionality
//Here we have provided start mechanism.
//Also, notice that abstract class becomes ideal candidate for base class.
public abstract class CarBase
{
//Abstract class gives you freedom to restrict access you may not do it in interface.
protected abstract void Steering();
internal abstract void Gear();
//Here you define Start mechanism for all your child classes
public void Start()
{
Console.WriteLine("Car should start with a key.");
}
}
public class Nissan : CarBase
{
//Interface and abstract class both ensured that you write only those methods
//which are absolutly necessary for you.
protected override void Steering()
{
Console.WriteLine("Location: Left, Power");
}
internal override void Gear()
{
Console.WriteLine("5 spd Automatic.");
}
//Here abstract class gives you benifit of using encapsulation to call car start.
//I am hiding my method just for demo :) not a good idea :D in real life
public new void Start()
{
base.Start();
}
public void DriveNissan()
{
Console.WriteLine("Driving Nissan Car:");
Start();
Steering();
Gear();
}
}
}
<div></div>