1

With refrence to C#, it is possible to define abstract class containing no abstract methods, then whats the use of defining class as ABSTRACT?

Talha Majid
  • 117
  • 2
  • 12

9 Answers9

2

an abstract class with properties can be used to represent a base class in inheritance. An abstract class can not be instantiated. that may fit well as we dont want to let otheres to create objects for our base class.

You can define abstract class to define the abstract nature of the object. Ex: Animal has Eyes. Dog can be inherited from Animal so that it will have Eyes. We may create a Dog object. But we don't want to create an Animal object.

from msdn,

The purpose of an abstract class is to provide a common definition of a base class that multiple derived classes can share. For example, a class library may define an abstract class that is used as a parameter to many of its functions, and require programmers using that library to provide their own implementation of the class by creating a derived class.

Shyju
  • 214,206
  • 104
  • 411
  • 497
  • Blanket Statement... WTF are u even talking about? I can put properties on anything... Are Dynamic Objects abstract ? :P – Jay May 31 '12 at 18:48
  • 1
    Gave u back ur vote because atleast your answer makes some type of sense now :P – Jay May 31 '12 at 19:25
1

With refrence to C#, it is possible to define abstract class containing no abstract methods

Yes, absolutely

then whats the use of defining class as ABSTRACT?

Mostly to communicate the fact the the class should be derived. A similar effect can be achieved by making the constructor protected: no one will be able to create an instance of the class without inheriting from it.

Thomas Levesque
  • 286,951
  • 70
  • 623
  • 758
  • @TalhaMajid: Enum, Delegate, Array, MarshalByRefObject, Attribute, WaitHandle.... Use this query to find more: http://pastebin.com/zjzGNpA9 – Thomas Levesque May 31 '12 at 19:36
1

Defining the class as abstract prevents it from being directly instantiated.

itsme86
  • 19,266
  • 4
  • 41
  • 57
1

Basically, I use as a rule of thumb that you should always define classes abstract when they MUST be inherited to be instantiated. Because abstract classes themselves cannot.

For example. Say you have a Building class, which is abstract. It must then be instantiated through a derived class, for example, a Bank or House. The fact that there are no abstract methods in the Building class has nothing to do with it being abstract or not.

antonijn
  • 5,702
  • 2
  • 26
  • 33
1

A simple rule is that make abstract classes (no matter it does or does not contain abstract methods) whenever u feel that this particular class does not make sens if it stands alone ...

Thus use abstract class when

  • u do not allow the instantiation of the object of an abstract class as its too general for that purpose
  • but at the same time allows you to give some concrete implementation of methods that can be inherited by other classes which is fruitful for your hierarchical structure that you have designed
Sana.91
  • 1,999
  • 4
  • 33
  • 52
  • i am still confused because i cant see any practical example around. – Talha Majid Jun 03 '12 at 18:20
  • 1
    For example if u r designing some School Registration system (SRS) .. a part of the system definitely would keep track of / and info about people associated with the system ...in this case u define a class Person (Class .. Too general for its object to be instantiated) ** which would be therefore, abstract of course , define some abstract or non abtract methods useful for furthes class hierarchy** .. and derive Students, Professor class from this abstract class which are definite candidates of being instantiated for this SRS .. – Sana.91 Jun 03 '12 at 18:44
  • so u see **YOU HAVE TO USE COMMON SENS** in designing solution to scenario that comes in front of u. Decide whats too general/specific **(Designing hierarchy for abstract classes)** and what shares nothing to ur classes but just needs a contract with ur classes or merely share function signatures **(interfaces)**... – Sana.91 Jun 03 '12 at 18:48
  • The great exapmle, Thanks alot :) – Talha Majid Jun 04 '12 at 05:52
  • more explaination for different views: http://www.csharptuts.net/use-of-non-abstract-method-of-abstract-class-in-c/ .. happy learning! – Sana.91 Aug 24 '12 at 17:09
0

You could define a class as abstract and have no methods in it... However, You would not be able to do anything with it since it can only be inherited by other classes and not instantiated on its own.

TRR
  • 1,637
  • 2
  • 26
  • 43
aserwin
  • 1,040
  • 2
  • 16
  • 34
0
  1. You can not create an instance of abstract class.
  2. The abstract keyword enables you to create classes and class members that are incomplete and must be implemented in a derived class.
  3. The purpose of an abstract class is to provide a common definition of a base class that multiple derived classes can share.
Md Kamruzzaman Sarker
  • 2,387
  • 3
  • 22
  • 38
0

An abstract class can contain abstract methods as well as normal (concrete methods).

The purpose of an abstract class is to be used as a base class that can be inherited by other classes. The advantage of doing this is that multiple child classes can take advantage of methods inside the base class(abstract class) without having to rewrite the same methods again.

Internet Engineer
  • 2,514
  • 8
  • 41
  • 54
0

You define a class abstract to preserve a possibility to define an abstract members.

It's true you can avoid do that, but in that case it just meaning less.

Other question like this can be done:

Why can I define some function virtual, even if noone will override it. ?

These are different concepts, for sure, but the idea of asking the question is the same.

Hope this helps.

Tigran
  • 61,654
  • 8
  • 86
  • 123