Ploymorphism in Java

Ploymorphism in Java - Steve Houseman Page 1 Polymorphism The three main programming mechanisms in Java that constitute objectoriented

Info iconThis preview shows pages 1–2. Sign up to view the full content.

View Full Document Right Arrow Icon
Steve Houseman Page 1 10/5/2011 Polymorphism The three main programming mechanisms in Java that constitute object–oriented programming (OOP) are encapsulation, inheritance, and polymorphism. Encapsulation refers to the hiding of data and methods within an object. Inheritance is the ability to create classes that share the attributes and methods of existing classes, but with more specific features. A final important concept in object-oriented programming is Polymorphism . Polymorphism describes the feature of languages that allows the same word to be interpreted correctly in different situation based on the context. For example in English the word “run” means different things if you use it with a “Marathon race”, a “business” or a “computer”. You understand the word based on the other words used with it. Literally polymorphism means “ many forms ”. Many forms of action take place, even though you use the same word to describe the action. In other words, many forms of the same word exist, depending on the object associated with the word. Polymorphism is a fundamental principle in object-oriented programming that allows you to make changes in the method definition for the derived classes and have those changes to apply to the software written in the base or superclass. This happens automatically in Java. For example, if you think of MusicalInstrument as a class, you can think of play() as a method of the class. If you think of various subclasses such as Guitar and Drum , you know that you carry out play() method quite differently for each subclass. Inheritance via Polymorphism: When we create a method in a subclass that has the same signature (same name and argument list) as a method in superclass, we override the method of the superclass in the subclass. When you use the method name with the subclass object, the subclass version of the method is used. However, suppose we can not override superclass methods. For example suppose the play() method of the MusicalInstrument superclass can not be overridden in the subclasses Guitar , and Drum . Here, we could then create a unique method for each of the subclass method, for example playGuitar() , for the Guitar subclass and playDrum() for the Drum subclass, but the classes you create are easier to write and understand if you use one reasonable name for methods that do essentially the same thing, such as the play() method. Polymorphism allows us to observe and attain the inheritance mechanism in class hierarchies by applying any operation that has multiple meaning. Polymorphism works in the following way; One very interesting and odd thing about inherited classes is that it is legal for a reference variable of a superclass type to refer to an object of one of its subclasses. Referring to our inheritance document presented to you earlier, for example look at the following statement that declares a reference variable named exam , using the GradedActivity class; GradedActivity exam;
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Image of page 2
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 10/05/2011 for the course CS 38 taught by Professor Stevehouseman during the Summer '09 term at Irvine Valley College.

Page1 / 17

Ploymorphism in Java - Steve Houseman Page 1 Polymorphism The three main programming mechanisms in Java that constitute objectoriented

This preview shows document pages 1 - 2. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online