What is the output of this Java program?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
|
I am not a Java person myself. A friend sent me this. You could help him by adding a solution.
This section requires Javascript.
You are seeing this because something didn't load right. We suggest you, (a) try
refreshing the page, (b) enabling javascript if it is disabled on your browser and,
finally, (c)
loading the
non-javascript version of this page
. We're sorry about the hassle.
So the first is M a m m a l reference that points to some instance of H o r s e object.That means that first object named ('h') can access the method in class H o r s e when ever it is called but still refer to M a m m a l .
The second declaration is where C a t t l e reference points to some instance of H o r s e object. That means simply like in the first , that object named ('c') can access the method from class H o r s e if it is called.
So in the example we have call from object reference C a t t l e where the method has as it's argument reference of class M a m m a l .
By the concept of inheritance when some class B extends from some class A , class B can access all public and protected members and methods.
In our example the second object named ('c') must call the method which has as argument reference of class M a m m a l and it also extends all methods which are public from that class (in our example it inherits the method e a t ( ) where the argument is reference from class M a m m a l and that method is in class M a m m a l ) therefore output must be: "Mammal eats food" .