Friday, November 4, 2016

Abstract Class


In object-oriented programming, objects are described based on classes. However, not all classes are used for describing objects. If a class do not have enough information to describe a concrete object, this class can be an abstract class. Here is the definition of abstract class from Barron’s AP Computer Science book: “an abstract class is a superclass that represents an abstract concept, and therefore should not be instantiated.”

For example, there are many different shapes, such as circle, triangle, rectangle, trapezoid, pentagon. These shapes share certain features: they all have perimeters, and there are ways to calculate their areas, etc. In other words, they are different, but meanwhile they all belong to shapes. Therefore, they can be declared as subclasses of the abstract class Shape. This program will create circle object, triangle object, rectangle object, and so on.


In most cases, an abstract class contains abstract method, which is a method that only have a header but no implementation code. When these is no appropriate default code for the method, abstract method makes sense. In the example of shape, the ways to calculate the areas of each shape are different. Thus, the method of calculating area in shape class can be an abstract method. Then, this method is overridden in each subclass. For example, in circle class, the method is that Pi times the square of radius.

---------------------------------------------------------------------------------

Writing References:
1.Barron's AP Computer Science
2.http://baike.baidu.com/link?url=QbjPQsHgVJfkS4SMDaayINec0145teiseSbzYn0uLFpRKOtt1czagquNHxJtXB9nGSatRM4f9KvjtOU8puSLSDYym-qPSXwqA61-7P0jbwJhmvCFKiTGOiCxiYo-rEHz
Picture References:
1.http://imgs.g4estatic.com/interface-abstract-class/interface-abstract-class.png
2.http://www.codemedad.com/images/abstract.gif

1 comment:

  1. I think your blog is really useful for us to do the previous program assignment! I wish I can read this earlier! This can really help us to form our own object-oriented framework that we have for our final project

    ReplyDelete