Mohamed Sanaulla | My Blog | Author of Java 9 Cookbook | Java 11 Cookbook
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.
Anton Sotnikov wrote: ... only as parameter for static method as superclass ...
Anton Sotnikov wrote: ... use upcasting and downcasting ...
Mohamed Sanaulla | My Blog | Author of Java 9 Cookbook | Java 11 Cookbook
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.
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? ...
Mohamed Sanaulla | My Blog | Author of Java 9 Cookbook | Java 11 Cookbook
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 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?
Mohamed Sanaulla | My Blog | Author of Java 9 Cookbook | Java 11 Cookbook
Have you read the article here? http://www.javaranch.com/campfire/StoryPoly.jsp
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.java dog.DogDemo Fido Buster Granby Fang
BowWow!
BowWow!
Woof! Woof!
Bark! Woof!
Nonononononononono. No.Anton Sotnikov wrote: . . . . . . Necessary to use that code above?
The idea of polymorphism is not to use casts.
No.Anton Sotnikov wrote: . . .
Is it correctly?
Anton Sotnikov wrote:
. . .
Is it correctly?
No.
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.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.
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?
Campbell Ritchie wrote:
It is poor design. The fact that it “works” does not make it correct code.Anton Sotnikov wrote: . . . I checked it - it works . . .
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"
Don't get me started about those stupid light bulbs. |