• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

about polymorphism

 
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello everyone!
Unfortunately if true i didn't understand this conception.
Why we don't use only inheritance ? why does polymorphism needed or created?
It means:
Animals animals = new Animals(); //it means access only fields and methods of into Animals class.

Car morris = new Cat(); //so as Cat have inheritance of Animals - there is access for all fields(variables) and methods of both classes Cat and Animals

But polymorphism means:
Animals pat = new Cat(): // it means only access for all methods and variables of Animals class + overriden method of Cat, but we deny access for another Cat methods and variables. For what is it?





 
Bartender
Posts: 3225
34
IntelliJ IDE Oracle Spring Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Consider this example:


With Inheritance and programming to interfaces you would be able to add new implementations to the interface without changing the existing implementations. This is often called as Open Closed Principal. This feature is also the basis for Dependency Injection used by Spring and other DI frameworks.
 
Marshal
Posts: 79177
377
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It means you should avoid adding methods when you create subclasses, if possible.
It means that is you have a Cat object, you get the Cat version of the walk method. Have a look at our story about polymorphism.

And remember that polymorphism applies to non‑private instance methods. Not to static methods and not to fields.
 
Anton Sotnikov
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


With Inheritance and programming to interfaces you would be able to add new implementations to the interface without changing the existing implementations. This is often called as Open Closed Principal. This feature is also the basis for Dependency Injection used by Spring and other DI frameworks.



Is it means that is more siuteble when we use implementation of certain class (Cat, Dog) - animalWalk() without declaration of variables, only as parameter for static method as superclass and use upcasting and downcasting?
 
Campbell Ritchie
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Don't use casting if you can possibly avoid it.

I don't understand what you mean about static methods. It means that whenever your object is a Dog it will use the Dog version of the methods.
 
Mohamed Sanaulla
Bartender
Posts: 3225
34
IntelliJ IDE Oracle Spring Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Anton Sotnikov wrote: ... only as parameter for static method as superclass ...


No, can be used in any method not only in static context. The example was just because from static method one can invoke static elements only. So because main() method was static, I created another static method.

Anton Sotnikov wrote: ... use upcasting and downcasting ...


You shouldn't use casting. If you do then you are breaking your code. So when you need to implement a new Animal say Tiger, then you would have to come back to that method and add a check and cast to see if its Tiger. Something like:



so if we add Tiger class then we would have to update the above code with:



the above approach is a BAD design and is a clear violation of Open Closed Principal which states that "software entities must be open for extension but closed for modification". The way one can fix the above code is by removing the instance of check and the casts. One should not be worried about what kind of animal is coming in, as long as its an animal.

I would highly recommend you to read story about polymorphism link provided by Campbell, very useful article.
 
Anton Sotnikov
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


I would highly recommend you to read story about polymorphism link provided by Campbell, very useful article.




Many reasons. Here's a good one...
You have a program that uses many different animal subclasses, and tells all the animals to play().

You could put all of the different animal subclass objects into an Animal [] array rather than keeping them in separate arrays for each subclass type.



is it means that we can save mix of animals in one arraylist for example withot creating special for some type of animal?





 
Mohamed Sanaulla
Bartender
Posts: 3225
34
IntelliJ IDE Oracle Spring Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Anton Sotnikov wrote:
... is it means that we can save mix of animals in one arraylist for example withot creating special for some type of animal? ...


Yes, you can. But to provide specialized implementation i.e overriding the method in Animal to accomodate different types of Animal you would create new classes which extend Animal class.
 
Anton Sotnikov
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


But to provide specialized implementation i.e overriding the method in Animal to accomodate different types of Animal you would create new classes which extend Animal class.


inheritance have same property. Why did you write of? or is there yet something?
 
Mohamed Sanaulla
Bartender
Posts: 3225
34
IntelliJ IDE Oracle Spring Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Anton Sotnikov wrote:


But to provide specialized implementation i.e overriding the method in Animal to accomodate different types of Animal you would create new classes which extend Animal class.


inheritance have same property. Why did you write of? or is there yet something?


Have you read the article here? http://www.javaranch.com/campfire/StoryPoly.jsp
 
Campbell Ritchie
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Let's have some simple classes:Now, compile all those classes and print their methods with the javap tool:You notice:
  • 1: It doesn't make any difference that Sheepdog is marked abstract.
  • 2: All the Class objects have a constructor taking String and a makeNoise method, except Bulldog wich uses the unoverridden version from Dog.
  • Watch for more posts.
     
    Anton Sotnikov
    Ranch Hand
    Posts: 65
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator


    Have you read the article here? http://www.javaranch.com/campfire/StoryPoly.jsp



    Yes I have.



    Is it true that main purpose is if we have polymorphism statement as Animal morris = new Cat(); but if object-cat have yet own methods except overriding method from superclass inside, and need to invoke it (catIsJumping()). - Necessary to use that code above?
     
    Campbell Ritchie
    Marshal
    Posts: 79177
    377
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    You must pass a 4‑element array of names, or you suffer an exception. Using dogs[3] is not good design:-

    java dog.DogDemo Fido Buster Granby Fang
    BowWow!
    BowWow!
    Woof! Woof!
    Bark! Woof!

    For a plain simple Dog (Fido): at runtime the JVM finds the corresponding Class<Dog> object and looks for the makeNoise method. You can verify that method is there from the output of the javap tool earlier. So it uses that method.
    Similarly for Labradors and Collies (Granby and Fang) the JVM can find a makeNoise method in the corresponding Class object and use it. The same would happen for Chihuahuas or OldEnglishSheepdogs. It will not work for Sheepdogs however because Sheepdog is abstract.

    But for Buster (Bulldog) there is no makeNoise method as you can see in the javap output, so the JVM cannot find it in the Class<Bulldog> object. But the Class<Bulldog> object has a reference to its superclass, Class<Dog>, so it looks in that Class object and finds a makeNoise method, and uses that. So you can see that the first two lines appear the same.

    There was another discussion about the same topic recently: read this thread.
     
    Campbell Ritchie
    Marshal
    Posts: 79177
    377
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    Anton Sotnikov wrote: . . . . . . Necessary to use that code above?

    Nonononononononono. No.

    Don't use casts like that. The idea of polymorphism is not to use casts. The JVM does all that sort of thing automatically. Look at my Bulldog and Labrador classes above.
     
    Anton Sotnikov
    Ranch Hand
    Posts: 65
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator


    The idea of polymorphism is not to use casts.


    No I said of only that piece of code not exactly of all polymorphism.
    I just want to tell that:
    We use:

    only that enable access for another methods into cat-object, such as catIsJumping() for example.
    Is it correctly?
     
    Campbell Ritchie
    Marshal
    Posts: 79177
    377
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    Anton Sotnikov wrote: . . .
    Is it correctly?

    No.

    You expect to pass an Animal to that method so you should use methods found in Animal. If you want a Cat method you would need a method taking Cat as a parameter.That is overloading, and that is determined at compile time; if you do not declare your Cat object as Cat, it will be assumed to be an Animal.
    I would not say that overloading is a kind of polymorphism.
     
    Anton Sotnikov
    Ranch Hand
    Posts: 65
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator


    Anton Sotnikov wrote:
    . . .
    Is it correctly?
    No.



    I checked it - it works:




    or am i misunderstand you?
     
    Campbell Ritchie
    Marshal
    Posts: 79177
    377
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    Anton Sotnikov wrote: . . . I checked it - it works . . .

    It is poor design. The fact that it “works” does not make it correct code. It means your method has to know about Cat objects as well as about Animal objects.
     
    Campbell Ritchie
    Marshal
    Posts: 79177
    377
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Mohammed has already explained here why that is poor design.
     
    Anton Sotnikov
    Ranch Hand
    Posts: 65
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator


    It is poor design. The fact that it “works” does not make it correct code. It means your method has to know about Cat objects as well as about Animal objects.


    Which is correct way to rich design?
     
    Sheriff
    Posts: 17644
    300
    Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    Anton Sotnikov wrote:


    It is poor design. The fact that it “works” does not make it correct code. It means your method has to know about Cat objects as well as about Animal objects.


    Which is correct way to rich design?



    You meant "a better design"

    Examples like an Animal hierarchy are fine for understanding the various mechanisms available in Java to support polymorphism and inheritance but they aren't very helpful in exploring design options. You write a program so that you can solve some kind of problem or provide for some kind of need. What is the problem or need involved here? If there is none, then coming up with a design around this hierarchy is just a rather pointless thought exercise.

    Here are some criteria for "good" designs:

    1. Simple
    2. Easy to understand
    3. Easy to test
    4. Easy to modify/adapt when requirements change
    5. Easy to extend/adapt when you get new requirements

    Edit: forgot the most important criterion: Satisfies the current requirements.
     
    Junilu Lacar
    Sheriff
    Posts: 17644
    300
    Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    Campbell Ritchie wrote:

    Anton Sotnikov wrote: . . . I checked it - it works . . .

    It is poor design. The fact that it “works” does not make it correct code.


    In physics, work is defined as Force * Distance. If you move stack of bricks from point A to point B, then you've done some work. Interestingly enough, if you move the bricks back to Point A, the distance is 0 so the total amount of work you have done is 0! That has always bothered me because it didn't make sense when I related it to the real world.

    I think this concept can be adapted to design though: work = design * usefulness. That is, if there is no practical use for your design, then all the effort you put into the design doesn't really produce anything that "works"
     
    Anton Sotnikov
    Ranch Hand
    Posts: 65
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator


    In physics, work is defined as Force * Distance. If you move stack of bricks from point A to point B, then you've done some work. Interestingly enough, if you move the bricks back to Point A, the distance is 0 so the total amount of work you have done is 0! That has always bothered me because it didn't make sense when I related it to the real world.

    I think this concept can be adapted to design though: work = design * usefulness. That is, if there is no practical use for your design, then all the effort you put into the design doesn't really produce anything that "works"



    ok, thank you so much for your language is understandeble.
    today i realized some little bit things of polymorphism.
    I try to get more for next time closer than now during the way of better design.
     
    Don't get me started about those stupid light bulbs.
    reply
      Bookmark Topic Watch Topic
    • New Topic