This week's book giveaway is in the General Computing forum. We're giving away four copies of Arduino in Action and have Martin Evans, Joshua Noble, and Jordan Hochenbaum on-line! See this thread for details.
Why can only „real“ dog bark? Hello, consider this code: class Animal{ void play(){System.out.println("Animal.play()"); }; void move(){System.out.println("Animal.move()"); }; } class Dog extends Animal{ //overriding in play / move method void play(){System.out.println("Dog.play()");}; void move(){System.out.println("Dog.move()");}; void bark(){System.out.println("Dog.bark()");}; } public class TestPolymorphismOverriden1{ public static void main(String args[ ]){ //(Late)Binding because the right object(Dog) will be assigned at run time. to the Animal reference "d". Dog d = new Dog(); d.play(); d.move(); d.bark(); } } C:\Java\EigeneJavaProgramme>javac TestPolymorphismOverriden.java TestPolymorphismOverriden.java:17: cannot resolve symbol symbol : method bark () location: class Animal d.bark(); ^ 1 error In this case a Dog object is referred to by an Animal reference. What does this mean when I invoke a Dog method with this Dog object with Animal reference? It means that according to late binding (Polymorphism) JVM recognizes that d is a Dog object and calls the play-method of Dog. So d.play whould lead to output: „Dog.play“. what I don’t understand is when d is a Dog object why can’t I call d.bark which is not an overriden method from animal But an Dog specific method. In the example as follows I create a „real“ Dog object (Dog reference and Dog object) and this dog can Bark wheras a Dog Object with an Animal reference cannot bark? class Animal{ void play(){System.out.println("Animal.play()"); }; void move(){System.out.println("Animal.move()"); }; } class Dog extends Animal{ //overriding in play / move method void play(){System.out.println("Dog.play()");}; void move(){System.out.println("Dog.move()");}; void bark(){System.out.println("Dog.bark()");}; } public class TestPolymorphismOverriden{ public static void main(String args[]){ //(Late)Binding because the right object(Dog) will be assigned at run time. to the Animal reference "d". Animal d = new Dog(); d.play(); d.move(); Dog d1 = new Dog(); d1.bark(); } } C:\Java\EigeneJavaProgramme>java TestPolymorphismOverriden1 Dog.play() Dog.move() Dog.bark()
You wrote: Dog d = new Dog(); but you meant Animal d = new Dog(); don't you? If so, don't mess between compilation and execution. Late binding is performed at execution. Here the problem is arising at compile time. Animal d = new Dog(); declares 'd' as Animal. The animal knows how to play, move and that's it. For the compiler it is thus an error to send the message 'bark' to d, which is an animal. W. [ August 03, 2002: Message edited by: Wilfried LAURENT ]
What I don't understand is when d is a Dog object why can't I call d.bark which is not an overriden method from animal But an Dog specific method.
All you say is true.
Dynamic method binding means that the method selection is determined by the type of the object rather than the type of the reference.
However, overriding occurs when two methods with the same name and type signature are defined in both a superclass and a subclass.
In the case of the bark() method, since your reference is an Animal reference and animal has no bark() method, the bark() method in Dog is not overriding the bark() method in Animal.
Java sees that it has an Animal reference, looks for the method in the Animal class. If it finds a method there, it looks at the type of object the Animal reference is referring to and goes to the method with the same signature in the object's (the Dog's) class.
Therefore your bark() method is specific to a Dog object being assigned to a Dog reference. It will also work if you add an empty bark() method to your Animal class for your Dog class to override:
[ August 03, 2002: Message edited by: Marilyn de Queiroz ]
JavaBeginnersFaq "Yesterday is history, tomorrow is a mystery, and today is a gift; that's why they call it the present." Eleanor Roosevelt
Marilyn de Queiroz
Sheriff
Joined: Jul 22, 2000
Posts: 9033
10
posted
0
Originally posted by Thomas Markl:
class Animal{ void play(){System.out.println("Animal.play()"); }; void move(){System.out.println("Animal.move()"); }; }
class Dog extends Animal{ //overriding in play / move method void play(){System.out.println("Dog.play()");}; void move(){System.out.println("Dog.move()");}; void bark(){System.out.println("Dog.bark()");}; }
By the way, you don't need the extra semicolons at the end of your method declarations. [ August 03, 2002: Message edited by: Marilyn de Queiroz ]
Thomas Markl
Ranch Hand
Joined: Mar 08, 2001
Posts: 192
posted
0
Hello Dirk, thanks for the information about -tag. I can now keep the formatting of the Java coding when I copy it from Word into the HTML-Window of my Javaranch posting.
The Polymorphism in Java, as I understand, requires that all needed methods in derived class(es) must be declared in parent class. The default behavior is coded in these methods in parent class or they are empty. In all derived classes "exceptions" in default behaviour are coded and methods that have no exceptions may not be declared at all. At compile time you do not know which derived class type object you want to create. So you write code to create a parent class type object. At run time, actual object type is known (which is one of the derived types). And methods that you have called on your object will actually be derived type methods, if there are any. Otherwise, the default method from parent class are launched. Your code did not include bark() method in parent class. Here is sample code: class animal { void play(){ System.out.println("Animal Play"); } void run(){ System.out.println("Animal Run"); } void bark(){ } } class dog extends animal { void run(){ System.out.println("Dog Run"); } void bark(){ System.out.println("Dog bark"); } } public class Polymorphism { public static void main(String[] args) { animal a = new dog(); a.bark(); a.play(); a.run(); } }