Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

reference to abstract class

What does it mean when there is a reference to an abstract class? I found it in code and I can't understand it.

I thought that an abstract class can't be instantiated. How can you give it a reference?

like image 578
lital maatuk Avatar asked Feb 16 '11 16:02

lital maatuk


2 Answers

An abstract class is designed to be derived from. The Liskov substitution principle roughly states that anything that uses the abstract parts of types derived from an abstract base should work equally well using the base polymorphically. That means a reference or pointer to the base should be used.

like image 169
Johann Gerell Avatar answered Oct 13 '22 11:10

Johann Gerell


A reference to an abstract class is just like a pointer to an abstract class: it needs to reference an object of some non-abstract subclass of the abstract class. You can use a reference like that to call virtual methods on the referenced class using the . syntax, in a way similar to a pointer to an interface in Java.

like image 29
Jeremiah Willcock Avatar answered Oct 13 '22 10:10

Jeremiah Willcock