14

Note that you cannot construct an object of an abstract class, but you can still have an object reference whose type is an abstract class. Of course, the actual object to which it refers must be an instance of a concrete subclass:

Account anAccount; // OK
anAccount = new Account(); // Error—Account is abstract
anAccount = new SavingsAccount(); // OK
anAccount = null; // OK

Not understanding why you can have an object reference to an abstract class...

templatetypedef
  • 362,284
  • 104
  • 897
  • 1,065
user2809437
  • 500
  • 1
  • 6
  • 21

2 Answers2

17

When you have an object reference whose type is an abstract class, that reference doesn't mean "I'm referencing an abstract class." Instead, it means "I'm referencing some actual object that's a subclass of that abstract class." This is why you can have the reference refer to a SavingsAccount, which is a non-abstract class that subclasses from Account, but you can't have it point to a new Account(), since you can't actually instantiate Account.

Note that the reference itself isn't an actual instance of the abstract class.

Hope this helps!

templatetypedef
  • 362,284
  • 104
  • 897
  • 1,065
  • Ok so when you do things like Account anAccount; what is that saying? it's not actually pointing to anything? Even if it wasn't an abstract class lets say String tmp; you haven't given it an address yet? – user2809437 Oct 28 '13 at 03:06
  • @user2809437- That's correct. When you say `Account anAccount`, you get a reference called `anAccount` that initially holds `null`, meaning that it's not pointing at anything. It's the same with `String tmp`; you get a reference to a `String`, but it points to `null` because you haven't assigned it to point to a `String` yet. – templatetypedef Oct 28 '13 at 03:07
  • One more question...what's the point of doing this? If you cannot create an instance of the class, why both with the reference? – user2809437 Oct 28 '13 at 03:21
  • 3
    @user2809437- Suppose that you want to manipulate an account, but you're not sure what kind of account. Maybe it's a `SavingsAccount`, or maybe it's a `CheckingAccount`, a `MoneyMarketAccount`, a `TreasuryAccount`, etc. Having a reference of type `Account` means that you can have a single variable that stores a reference to an object of any of those types, which makes it possible to write code that works on "accounts in general" without duplicating it for each of these types of accounts. – templatetypedef Oct 28 '13 at 03:25
3

The problem is that you cannot call abstract member routines.
When you call ’new’ you're actually calling the class's constructor.
Because you're trying to call an abstract member function you get an error.

You can reference an abstract class, because it is just a blueprint for a real class that derives from it. A bit like an interface but with inheritance.
Just like you cannot instantiate an interface you cannot instantiate an abstract class.

This of course is part of polymorphism.
The differences between abstract classes and interfaces are remarkably small, see:Interface vs Abstract Class (general OO)

Community
  • 1
  • 1
Johan
  • 74,508
  • 24
  • 191
  • 319