locked
What is the purpose of non abstract method in abstract class? RRS feed

  • Question

  • Why we define non abstract methods in abstract class while we can not create the instance of abstract class.how to access these non abstract methods?What is the significance of non-abstract method.


    Sharad Sharma Pursuit Technologies Bangalore
    Monday, April 12, 2010 12:47 PM

Answers

  • Not quite.  Not all members of a class are inherited.  Private members are not inherited, and neither are static members.

    Abstract classes are commonly used to abstractly define the composition of an object, just like an interface would.  But, an abstract class also allows you to define default functionality for consumers.  I like to use their constructors as object factories.

    Rudy  =8^D


    Mark the best replies as answers. "Fooling computers since 1971."
    • Proposed as answer by ofarooq Monday, April 12, 2010 3:24 PM
    • Marked as answer by Liliane Teng Tuesday, April 13, 2010 6:05 AM
    Monday, April 12, 2010 1:01 PM
  • Here is a concrete example that might help understanding:

    The System.IO.Stream type is abstract. It implements IDisposable. Stream.Dispose is a non-abstract method that calls the virtual Close method (not abstract, but it could be in a similar type). This allows derived types to support IDispose without having to write any code for it, it's already implemented by the base class. The work of Disposing is done by the Close method.

    This is a common pattern throughout the .NET Framework: Both Close and Dispose are always synonymous. I suppose the reason why is to allow those who are accustomed to the old-school Close methods to still call them, even though what they really do is Dispose.

    • Marked as answer by Rudedog2 Tuesday, April 20, 2010 10:27 PM
    Monday, April 12, 2010 3:11 PM

All replies

  • When a non-abstract class implements an abstract class , All the non-abstract members of the abstract class are simply inherited. And these methods can be accessed as of any child class would access the parent class methods .

    Regards,

    Omer

    Monday, April 12, 2010 12:53 PM
  • Not quite.  Not all members of a class are inherited.  Private members are not inherited, and neither are static members.

    Abstract classes are commonly used to abstractly define the composition of an object, just like an interface would.  But, an abstract class also allows you to define default functionality for consumers.  I like to use their constructors as object factories.

    Rudy  =8^D


    Mark the best replies as answers. "Fooling computers since 1971."
    • Proposed as answer by ofarooq Monday, April 12, 2010 3:24 PM
    • Marked as answer by Liliane Teng Tuesday, April 13, 2010 6:05 AM
    Monday, April 12, 2010 1:01 PM
  • Thanks Rudy for correcting me .
    Monday, April 12, 2010 1:15 PM
  • To add, you will have non abstract methos in an abstract class if there is any behaviour that is not going to change across the derived class in addition to the behaviour that changes among the derived classes (abstract classes).
    Monday, April 12, 2010 2:35 PM
  • simply, because you can reuse it  withouth implement the abstract class, for more info see the msdn.
    Monday, April 12, 2010 3:09 PM
  • Here is a concrete example that might help understanding:

    The System.IO.Stream type is abstract. It implements IDisposable. Stream.Dispose is a non-abstract method that calls the virtual Close method (not abstract, but it could be in a similar type). This allows derived types to support IDispose without having to write any code for it, it's already implemented by the base class. The work of Disposing is done by the Close method.

    This is a common pattern throughout the .NET Framework: Both Close and Dispose are always synonymous. I suppose the reason why is to allow those who are accustomed to the old-school Close methods to still call them, even though what they really do is Dispose.

    • Marked as answer by Rudedog2 Tuesday, April 20, 2010 10:27 PM
    Monday, April 12, 2010 3:11 PM
  • Here is a concrete example that might help understanding:

    The System.IO.Stream type is abstract. It implements IDisposable. Stream.Dispose is a non-abstract method that calls the virtual Close method (not abstract, but it could be in a similar type). This allows derived types to support IDispose without having to write any code for it, it's already implemented by the base class. The work of Disposing is done by the Close method.

    This is a common pattern throughout the .NET Framework: Both Close and Dispose are always synonymous. I suppose the reason why is to allow those who are accustomed to the old-school Close methods to still call them, even though what they really do is Dispose.


    " But, an abstract class also allows you to define default functionality for consumers. "  Thanks, for the example.

    Mark the best replies as answers. "Fooling computers since 1971."
    Monday, April 12, 2010 3:15 PM