aspose file tools*
The moose likes Beginning Java and the fly likes Why can only Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Why can only "real" dog bark?" Watch "Why can only "real" dog bark?" New topic
Author

Why can only "real" dog bark?

Thomas Markl
Ranch Hand

Joined: Mar 08, 2001
Posts: 192
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()
Dirk Schreckmann
Sheriff

Joined: Dec 10, 2001
Posts: 7023
Thomas,
Please note that your code would be much easier to read if you were to surround it with the [code] and [/code] UBB Tags.


[How To Ask Good Questions] [JavaRanch FAQ Wiki] [JavaRanch Radio]
Wilfried LAURENT
Ranch Hand

Joined: Jul 13, 2001
Posts: 269
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 ]
Marilyn de Queiroz
Sheriff

Joined: Jul 22, 2000
Posts: 9046
    
  10

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: 9046
    
  10
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
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.

Thomas.
Barkat Mardhani
Ranch Hand

Joined: Aug 05, 2002
Posts: 787
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();
}
}
Anthony Villanueva
Ranch Hand

Joined: Mar 22, 2002
Posts: 1055
You can also visit the Sun tutorial on inheritance.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Why can only "real" dog bark?