Links

Java QA: Why do we need Abstract Classes in Java?

thanassis | Java Questions And Answers | Wednesday, January 3rd, 2007

We need the notion of a parent class (from which we can inherit) that is prohibitted from being instantiated. We can think about abstract classes as semicomplete generalised implementations that can not exist as objects on their own, they can only exist as parts of child objects.

For example, think of a “Shape” hierarchy with subclasses such as Rectangle, Circle, Line etc. As shapes share data and implementation (i.e. coordinates, ability to paint themselves) it makes sense generalising, that is, creating a parent class (Shape) that contains all common aspects of the shapes. So at this point “Shape” is just a superclass.
It does not make sense however being able to instantiate an object of the class Shape because it misses critical information. To prohibit it, we make the class an abstract class.

No Comments

No comments yet.

Leave a comment

RSS feed for comments on this post. TrackBack URI