locked
Abstraction RRS feed

  • Question


  • Hi,

      Sorry for this simple question, but i am still confused about this.
    What is abstraction in OOPs? Hw can we implement this in C#? is it anything related to abstract class?
    can anyone help me with examples in C#?

    thanks in advance..!


    Monday, July 30, 2007 5:12 PM

Answers

  •  shinu wrote:

    Hi,

      Sorry for this simple question, but i am still confused about this.
    What is abstraction in OOPs? Hw can we implement this in C#? is it anything related to abstract class?
    can anyone help me with examples in C#?

    thanks in advance..!




    Abstract classes are not what software engineers are referring to when the speak of Abstraction.  Abstract classes could help us achieve abstraction.

    Abstraction is the concept of taking a specific, detailed problem and creating a more general abstract solution.  The main idea is that if you abstract away the details then your solution will be more flexible.  Therefore, if your problem changes slightly your solution will hopefully still work, or at most require a small change.


    From Wikipedia

    "Abstraction is simplifying complex reality by modeling classes appropriate to the problem, and working at the most appropriate level of inheritance for a given aspect of the problem.
    For example, Lassie the Dog may be treated as a Dog much of the time, a Collie when necessary to access Collie-specific attributes or behaviors, and as an Animal (perhaps the parent class of Dog) when counting Timmy's pets.
    Abstraction is also achieved through Composition. For example, a class Car would be made up of an Engine, Gearbox, Steering objects, and many more components. To build the Car class, one does not need to know how the different components work internally, but only how to interface with them, i.e., send messages to them, receive messages from them, and perhaps make the different objects composing the class interact with each other."



    Monday, July 30, 2007 5:41 PM
  • "abstraction" and "abstract" are different.  An abstraction is a generalization, something that has very little dependencies or is very loosely coupled.  An abstract class is one that isn't fully implemented, it's meant only to be derived from and to have its abstract methods overridden.

    Tuesday, July 31, 2007 7:46 PM
    Moderator
  •  

    Abstraction is concerned with hiding, or providing detail at a later date.  An class that is abstracted is something where you provide an interface that must be implemented within the class, but that class itself contains no implementation itself.

     

    The difference between an abstract class and an interface is that the abstract methods could be implemented by a derived class as well as the base class providing some functionality, or implementation as well.  An interface merely defines the contract the an implementation must adhere to, to be considered able to communicate using that interface.

     

    So a short example could be, and clearly this won't compile, but will give you some ideas and keywords to search on...

     

    public abstract class SomeClass

    {

    .

    .

    public abstract void SomeMethod

    .

    public void AnotherMethod()

    {

    Console.WriteLine("Hello.")

    }

    .

    }

     

    Then a derived class:

     

    public class SomeOtherClass : SomeClass()

    {

    // You have to provide the implementation here, as this is abstract...

    public void SomeOtherMethod()

    {

    Console.Writeline("Hello again.");
    }

    .

    // Note that you get AnotherMethod() for "free"...

    .
    }

     

     

    I hope this helps you, as the above is a pretty simplistic answer.  If it doesn't make sense, shoot me another post, and I'll try again!

     

    Martin.

    Wednesday, August 1, 2007 2:21 AM

All replies

  •  shinu wrote:

    Hi,

      Sorry for this simple question, but i am still confused about this.
    What is abstraction in OOPs? Hw can we implement this in C#? is it anything related to abstract class?
    can anyone help me with examples in C#?

    thanks in advance..!




    Abstract classes are not what software engineers are referring to when the speak of Abstraction.  Abstract classes could help us achieve abstraction.

    Abstraction is the concept of taking a specific, detailed problem and creating a more general abstract solution.  The main idea is that if you abstract away the details then your solution will be more flexible.  Therefore, if your problem changes slightly your solution will hopefully still work, or at most require a small change.


    From Wikipedia

    "Abstraction is simplifying complex reality by modeling classes appropriate to the problem, and working at the most appropriate level of inheritance for a given aspect of the problem.
    For example, Lassie the Dog may be treated as a Dog much of the time, a Collie when necessary to access Collie-specific attributes or behaviors, and as an Animal (perhaps the parent class of Dog) when counting Timmy's pets.
    Abstraction is also achieved through Composition. For example, a class Car would be made up of an Engine, Gearbox, Steering objects, and many more components. To build the Car class, one does not need to know how the different components work internally, but only how to interface with them, i.e., send messages to them, receive messages from them, and perhaps make the different objects composing the class interact with each other."



    Monday, July 30, 2007 5:41 PM
  • "abstraction" and "abstract" are different.  An abstraction is a generalization, something that has very little dependencies or is very loosely coupled.  An abstract class is one that isn't fully implemented, it's meant only to be derived from and to have its abstract methods overridden.

    Tuesday, July 31, 2007 7:46 PM
    Moderator
  •  

    Abstraction is concerned with hiding, or providing detail at a later date.  An class that is abstracted is something where you provide an interface that must be implemented within the class, but that class itself contains no implementation itself.

     

    The difference between an abstract class and an interface is that the abstract methods could be implemented by a derived class as well as the base class providing some functionality, or implementation as well.  An interface merely defines the contract the an implementation must adhere to, to be considered able to communicate using that interface.

     

    So a short example could be, and clearly this won't compile, but will give you some ideas and keywords to search on...

     

    public abstract class SomeClass

    {

    .

    .

    public abstract void SomeMethod

    .

    public void AnotherMethod()

    {

    Console.WriteLine("Hello.")

    }

    .

    }

     

    Then a derived class:

     

    public class SomeOtherClass : SomeClass()

    {

    // You have to provide the implementation here, as this is abstract...

    public void SomeOtherMethod()

    {

    Console.Writeline("Hello again.");
    }

    .

    // Note that you get AnotherMethod() for "free"...

    .
    }

     

     

    I hope this helps you, as the above is a pretty simplistic answer.  If it doesn't make sense, shoot me another post, and I'll try again!

     

    Martin.

    Wednesday, August 1, 2007 2:21 AM

  • Thanks for your help..

    So implementing abstraction in our code depends on design or on how do we create classes and their hierarchies?


    Wednesday, August 1, 2007 10:27 AM