• Post Reply Bookmark Topic Watch Topic
  • New Topic

Do I make it wrong?  RSS feed

 
Max Bean
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
public class Animal {
private int height;
private int weight;
public void makeNoise() {
System.out.println("This is a super class assasination");
}

class Dog extends Animal {
public void makeNoise() {
super.makeNoise();
}
public void makeNoise(1) {
System.out.println("ruff ruff!");
}
}

class Cat extends Animal {
public void makeNoise() {
super.makeNoise();
}
public void makeNoise(1) {
System.out.println("meow meow!");
}
}

public class ShowSub {
public static void main(String args[]) {
Animal[] animals() = new Animal[2];
animals[0] = new Dog();
animals[1] = new Cat();
animals[0].makeNoise();
animals[0].makeNoise(1);
}
}

and then the ShowSub can't be compiled as the compiler blame: cannot applied int to the overridden and overloaded makeNoise(1) of mine.

I'm crying.....






 
Ajay A Patil
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The method declarations in Cat and Dog class need correction.
 
Max Bean
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
LOL

it's just copy and paste mistake.

I, indeed, declare them as
class Cat extends Animal {
public void makeNoise() {
super.makeNoise();
}
public void makeNoise(int x) {
System.out.println("meow meow");
}
}
and the same for Dog.
 
Max Bean
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Another correction is that I declare an array as

Animal[] animals = new Animal[2];
 
Max Bean
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
My point is why the new instances still use the method from the superclass and why the compiler don't know the overridden method.

It keep using the superclass method and I do this code following the Head First Java.

Why can't I confuse?
 
Ajay A Patil
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Declare makeNoise(int i) in the super class. i.e Animal.java

public void makeNoise(int i) {
System.out.println("This is a super class assasination");
}
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Animal contains a makeNoise() method that takes no arguments.

Dog overrides makeNoise(). In addition, Dog also overloads makeNoise(int i) to take an int argument.

So in Animal, there is only one makeNoise method. But in Dog, there are two.

Now, suppose you create an instance of Dog and upcast its type to Animal. When you call makeNoise() on this instance, the overriden method in Dog will be invoked.

Animal a = new Dog();
a.makeNoise();

However, you cannot call the overloaded method on this Animal reference, because an Animal does not have a makeNoise(int i) method that takes an int. This is what the compiler is telling you.

To call the overloaded method (which is in Dog but not Animal), you would need to downcast the Animal reference back to its original type of Dog.

Dog d = (Dog)a;
d.makeNoise(3);

Also consider this: If an overridden method simply calls the super version, then how do you know which one is invoked?
 
Max Bean
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Many thnx for both of you.

Now my cerebrum is a little sharper than ever.


And now I can move on the next page of the book and come closer to pass the SCJP exam.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!