How can abstract classes have references but not objects?

后端 未结 2 1982
你的背包
你的背包 2021-02-07 23:47

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

2条回答
  •  自闭症患者
    2021-02-08 00:35

    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!

提交回复
热议问题