polymorphic Arrays. | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

polymorphic Arrays.

i was practicing polymorphism by making a super class and subclasses. I made a superclass Animals and the subclases, dog,lion and cat. I connected them to the super class through inheritance. Then i made a method in the superclass and then i overrode them in each subclass except for the dog subclass. Then i made use of polymorphism in my main class when establishing the object to call each method , all the methods ran perfect for each class except the dog class. I then overrode the method in the dog class and it worked fine. Why is this so?

18th Apr 2017, 5:37 AM
Mogammad Shameer Losper
Mogammad Shameer Losper - avatar
11 Answers
+ 2
I fixed your code to run, now it seems fine. Try it and tell me what is the problem with it, what do you expect? https://code.sololearn.com/c92UMYVkHc5Y/?ref=app
18th Apr 2017, 6:20 AM
Tamás Barta
Tamás Barta - avatar
+ 1
Link here you example.
18th Apr 2017, 5:11 AM
Tamás Barta
Tamás Barta - avatar
+ 1
your fixed does exactely what I expected. the dog uses animals method, the other two their own implementation.
18th Apr 2017, 6:25 AM
Volker Milbrandt
Volker Milbrandt - avatar
+ 1
Did you see my fixed code? Your expectations were good. You needn't override that method in Dog and it works.
18th Apr 2017, 6:42 AM
Tamás Barta
Tamás Barta - avatar
+ 1
And @Override annotation should be used to avoid typo problems.
18th Apr 2017, 7:05 AM
Tamás Barta
Tamás Barta - avatar
0
https://code.sololearn.com/cKbNOkDyT6zo/#java i just made this so ignore all the errors but this will allow you to understand what i said.
18th Apr 2017, 5:26 AM
Mogammad Shameer Losper
Mogammad Shameer Losper - avatar
0
please fix the example code so that it will compile. - public and class in lowercase - Animal without () braces - there is an unmatched } in your main method - assiggnments need to goto the array Ani, not the class Animal
18th Apr 2017, 6:21 AM
Volker Milbrandt
Volker Milbrandt - avatar
0
@Volker Thank you for the help @ Tamas Thank you for fixing my code. My initial idea was that i wanted to override all the subclass methods to the sound each animal made but since i already made the method in the super class Animal which outputs "woof-woof"- which is the sound made by a dog, i thought it would be unnecessary to to override the method in the dog subclass since it already inherited the correct method.from the super class. The problem- when i did not override the dog class method, it gave me a fault when i made the object and tried calling the method with the correct syntax.
18th Apr 2017, 6:38 AM
Mogammad Shameer Losper
Mogammad Shameer Losper - avatar
0
as it is not necessary to make all classes static, I also fixed the code. please observe that usually you should - declare a package for your code (not working in the playground) - you should split your classes in several files. rule of thumb: each class in it's own file (except for inner and anonymous classes). As far as I know this is also not possible in the playground. additionally I put a second output loop in the main using foreach to iterate on the animals. https://code.sololearn.com/cE234wsv26N1/?ref=app
18th Apr 2017, 7:02 AM
Volker Milbrandt
Volker Milbrandt - avatar
0
@Override is nice, but at least for implementing an interface IT's misleading IMHO. similar like repeating virtual each time in C++ derived classes, you will find here discussions if and when to do. I added the annotation to my code :)
18th Apr 2017, 7:12 AM
Volker Milbrandt
Volker Milbrandt - avatar
0
@Tamas, yes i did and understand what the problem was ,Thank you!
18th Apr 2017, 8:36 AM
Mogammad Shameer Losper
Mogammad Shameer Losper - avatar