java - How does polymorphism helps a programmer? -


I am trying to understand the polymorphism concept in java. I fully understand the concept of polymorphis that the method of Subcalss to use an interface object to call. I have been reading an article. Which says:

To give Java Virtual Machine, depending on the actual class of method implementation object, how do you feel the full power of polymorphism in your programs?

So I understood that jvm is so powerful that it can call the method based on the actual class of the object. But how am I going to be a programmer? In the form I could have made the object of the subclass and could call its method? Why does polymorphism in any way make things easier for a programmer?

This is about design practices and design patterns before you understand the depth hiding in these concepts. Examples of design patterns (like this can be an example) read. But by any means, this is a quick rundown:

Design practice: Design for an interface Assume that you have a list of the contents of the interface, let's say, the animal says:

  list & gt; Animals & gt; Animal = new Arraylight & lt; & Gt; (); Animals.add (new cat ()); Animals.add (new dog ());   

And if you put some objects into the classes implementing the interface, then you can do this for example:

  for (Animals Animals: Animals) {Animals. Printnewtime (); }   

The animal should be a cat, it will print "Meow", but it can be a dog and the print can be "vapor". The thing is that instead of the code where you use the interface, you do not want to worry about the implementation.

Comments

Popular posts from this blog

ios - Adding an SKSpriteNode to SKScene from a child SKSpriteNode -

Matlab transpose a table vector -

c# - Textbox not clickable but editable -