1
public class Test {

    public static void main(String[] args) {
        // TODO Auto-generated method stub

        Animal a=new Animal();
        a.speak();

        Animal d=new Dog();
        d.speak(3);//           
    }

    class Animal
    {
        public float speak()
        {
            System.out.println("I am a animal!");
            return 0;
        }
    }

    class Dog extends Animal
    {
        public double speak(int a)
        {
            System.out.println("Dog sparks!");
            return 0;
        }
    }
}

I just learned override and overload in java. Here what i am trying to do is to implement a overload.But the compiler shows that d.speak(3) is wrong.

I think the dog inherit the animal. So it has a speak() func. And I add a speak(int) to overload. Why I am wrong? thank u.

4 Answers 4

4

Only Dog can speak with an int argument.

Dog dog = new Dog();
dog.speak(3);

If you want to call speak as an Animal without a cast, then the argument list needs to be the same for all objects

Sign up to request clarification or add additional context in comments.

Comments

2

It's because the Animal class doesn't have a speak method with that signature (taking an int parameter). Therefore the compiler cannot find the definition in the Animal class. You have to downcast it before, like

Animal d = new Dog();
((Dog)d).speak(3);

EDIT: or, as Reimeus suggested, declare it directly as a Dog.

2 Comments

So when I want to implement polymorphism,first I have to ensure the inherit tree has same signature. Or I have to 'cast' it to proper data types?
@AlexWei Polymorphism applies just with the same signature. So if the method interface (given by name, return type and parameters names and types) is the same throughout the classes in the hierarchy, you can call that method from a pointer to the base class (in this case Animal) and the runtime will invoke the method on the actual class (the Dog in this case) of the instance.
0

You are using reference of Animal and instance of Dog.
In case of overridden methods, it points to the method available in referenced class.
Here, it will point to speak() method of Animal class because it is the referenced class.
Therefore, it is unable to find parametrized method speak(int a).

//Points to speak() of Animal
Animal a=new Animal();
a.speak();

//Points to speak(int ) of Dog class
Dog d=new Dog();
d.speak(3);

//Points to speak() of Animal class
Animal d=new Dog();
d.speak(3); //Results in compilation error as speak(int) is not available with Animal reference

This situation is called SuperClass reference for SubClass object in object oriented programming terminology.

Comments

0

You are overloading the speak method in Dog. And Java use static binding for overloading method which means the declared type has to have the overloaded method. In your case, Animal is the declared type but it doesn't have the speak(int a). And that why you have to type cast it: ((Dog)d).speak(3) to make the compiler stop complaining.

1 Comment

I just searched static binding in your answer. It really helps.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.