• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Ron McLeod
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

Why can only "real" dog bark?

 
Ranch Hand
Posts: 192
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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()
 
Sheriff
Posts: 7023
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 269
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Sheriff
Posts: 9109
12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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 ]
 
Marilyn de Queiroz
Sheriff
Posts: 9109
12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 192
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 787
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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();
}
}
 
Ranch Hand
Posts: 1055
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You can also visit the Sun tutorial on inheritance.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic