2

I understand that you cannot make instances of an Abstract class,and that subclasses of Abstract classes can inheritance properties of first Abstract class, but I fail to see why Abstract classes need to exist at all when you have the normal Super classes and their sub-classes. What is the benefit/application of an Abstract class?

user3013144
  • 61
  • 2
  • 13

3 Answers3

3

Abstract classes prevent an "incomplete" abstract implementation, from being instantiated.

For example, an abstract class might have abstract methods without any implementation. It wouldn't be valid to actually call such methods.

Alternatively, an abstract class may not have any actual abstract methods -- but may still be expected to add additional functionality in the subclass API, for it be useful.

The abstract keyword is used for both of these reasons & prevents the abstract class from being instantiated.

Thomas W
  • 13,940
  • 4
  • 58
  • 76
1
abstract class Animal { }
class Dog extends Animal { }
class Cat extends Animal { }

What would the Animal be if you could instantiate it?

You should use an abstract class when the super class is not meant to be instantiated: it serves as a base for extending classes to rely on, but on its own it doesn't mean anything.

By having the Animal class serve as a baseclass for Dog and Cat, you can define the common behaviour in there instead of having to redefine it in every subclass. Think about properties like Name, Species and FirstSighting: these will be used in both Cat and Dog, so why not put them in a baseclass to avoid extra work and inconsistency issues?

On the other hand you don't want to just create a new Animal either: it says nothing about the type of the animal. That's why you make it abstract.

Jeroen Vannevel
  • 43,651
  • 22
  • 107
  • 170
  • What are the downvotes for? – Jeroen Vannevel Nov 24 '13 at 00:07
  • as long as the class Animal has no use, it has no right to exist at all, regardless of if its abstract or not. – b.buchhold Nov 24 '13 at 00:07
  • 1
    Animal can contain certain properties like `name`, `species`, `first sighting`, etc. Would you redefine this in every subclass? – Jeroen Vannevel Nov 24 '13 at 00:08
  • If I instantiate Animal,I could get Dogs and Cats...SO I am now wondering why I would actually need an Abstract Animal, if I could just instantiate it from Animal (of non Abstract class)...Sorry,I think I may be missing your point here.... – user3013144 Nov 24 '13 at 00:09
  • +1, IMO this answer is perfectly valid & useful. – Thomas W Nov 24 '13 at 00:09
  • 1
    can / could. as long as you do not include such properties in the answer, it provides no value toanyone that doesn't already know about abstract classes. your answer is not wrong, just not particulary helpful. – b.buchhold Nov 24 '13 at 00:09
  • I have updated my post with some more information. – Jeroen Vannevel Nov 24 '13 at 00:12
0

The main purpose of abstract classes/interfaces is to model an abstraction of a problem - design. The real implementation would then behave according to the abstraction.

epinuj
  • 142
  • 2