James Ray Anderson

James Ray Anderson
James Ray Anderson
0 comments

Quick and Easy OOP - Part V - Polymorphism

8:00 PM
Overview
Polymorphism is a programming concept that allows you to declare a class interface as a template for other objects to use - but without specifying the code.  The implementation of the interface is handled in the class that derives from it.  It is used to separate the interface and implementation.  This allows you to program the interfaces as you need to for specific situations.

For example:

A shape object has the following:
- name
- getArea()
- draw()

No code is done in the shape object.  It is just a shell.

Make a Rectangle Subclass
However a rectangle has:
- length
- width
- getArea()
- draw()
- name

The rectangle is a subclass of the shape class.  The code for the getArea() and draw() will be specific to the rectangle and therefore doesn't make sense to code it in the superclass.  However, you are using the superclass to keep your "is a shape" objects consistent.

Make a Circle Subclass
Lets add a circle:
- diameter
- getArea()
- draw()
- name

The circle keeps the pattern of the superclass but is allowed to write circle-specific code for getArea() and draw().



0 comments:

 
Toggle Footer
Top