0

In my class hierarchy, there's a superclass called "Media type" ."Sound" and "Image" are two sub-classes of it.For both Sound and Image classes I want to have a method as quality().But the way the method quality() works when it is used under sound class and image class is different.What I want to know is is in the superclass "Media type" can I add two methods with the same name quality(),which according to the object type(whether it is sound or image type object) decides which quality() method should be functioned. Is this what is known as polymorphism?

clarkson
  • 539
  • 1
  • 13
  • 30

4 Answers4

4

What I want to know is is in the superclass "Media type" can I add two methods with the same name quality(),which according to the object type(whether it is sound or image type object) decides which quality() method should be functioned.

Well, you've partially understood polymorphism. The beauty of polymorphism is that you don't need two methods - you only need one. Define method quality() in the parent class, and then implement the specific behavior in each child class. If you have a few Media objects you can call quality() on them all - at run time, the correct concrete class' implementation will be called.

ArrayList<Media> m = new ArrayList<>();
m.add(new Sound());
m.add(new Image());

for(Media obj: m) {
    obj.quality();  // polymorphism in action!
}

Try it out. Good luck!

Meesh
  • 379
  • 1
  • 13
  • @Lokesh did you downvote everybody who answered this question? – Meesh Oct 06 '13 at 05:53
  • Only the answers which didn't address the point . "What I want to know is is in the superclass "Media type" can I add two methods with the same name quality(),which according to the object type(whether it is sound or image type object) decides which quality() method should be functioned." This is what is asked. – Lokesh Oct 06 '13 at 06:00
  • @Lokesh You're right - the OP asked if he can add two methods. And I've addressed this point now. Thank you! – Meesh Oct 06 '13 at 06:15
2

You add a method named quality() to MediaType class and then you override it in Sound and Image classes. This way whenever quality() is called any MediaType, the right quality() method for each object (Sound or Image) is called. And yes, it's polymorphism this way.

Ali Hashemi
  • 2,457
  • 3
  • 29
  • 39
1

This is an example of Polymorphism:

You have a general (parent) class where method() exists and you add subclasses (inherit) where the same method() exists, but might be overriden.

For all of the classes above you can call method() and the corresponding method() would be called.

Shady
  • 162
  • 1
  • 8
-3

I am a beginning programmer but I might be able to help you a little bit. As I understand, polymorphism is the ability of a class to behave as if it had two different types. The compile-time (current type) type and the run-time (true type) type. If you have a sound type instance and assign it to a media type instance, its run-time type will be sound, but its compile-time type will be media. This means that to call a method that method will need to exist in your compile-time type class (media), but the actual method called will be the method with the same features contained within your run-time class (sound).

alessfg
  • 37
  • 6
  • That is **not** what polymorphism is. – Rohan Oct 06 '13 at 05:52
  • The dictionary definition of polymorphism refers to a principle in biology in which an organism or species can have many different forms or stages. This principle can also be applied to object-oriented programming and languages like the Java language. Subclasses of a class can define their own unique behaviors and yet share some of the same functionality of the parent class. – alessfg Oct 06 '13 at 05:58
  • I think you might be getting tripped up in thinking that these classes are behaving differently at run time vs compile time. – Jason Sperske Oct 06 '13 at 06:05
  • If you don't know the answer, then don't answer the question. – Dawood ibn Kareem Oct 06 '13 at 07:28