-1

In the below code in Animal Class when I remove the ClimbTrees() Method why is it generating Error

public class Refr1 
{
    public static void main(String[] args) 
    { 
      Animal objChimp = new Chimp();
      objChimp.ClimbTrees();     
    }
}


class Animal
{
    void ClimbTrees()
    {
     System.out.println("I am Animal Which Climb Tree");    
    }
}


class Chimp extends Animal 
{
    void ClimbTrees()
    {
        System.out.println("I am Chimp Which Climb Tree");  
    }
}

If I Remove the ClimbTrees() in Animal Class its showing Below Error

Exception in thread "main" java.lang.Error: Unresolved compilation problem: 
    The method ClimbTrees() is undefined for the type Animal
Luiggi Mendoza
  • 85,076
  • 16
  • 154
  • 332
Java Beginner
  • 1,635
  • 11
  • 29
  • 51
  • 1
    Obviously, because you are calling the method you just removed, in your main method. You need to read up on how method overriding works (hint, the method may be defined in `Chimp`, but the object in your main class is an `Animal`). – Perception Mar 30 '13 at 01:33
  • But it is still in Chimp class right – Java Beginner Mar 30 '13 at 01:34
  • @JavaBeginner - if you want to call a method that only exists in a class, then you need to declare your object as that class type. – Perception Mar 30 '13 at 01:35
  • Thanks for Reply. But when I create object with Chimp Reference I can call method which is not in Animal Class but in chimp class. At this stage is still polymorphism in code – Java Beginner Mar 30 '13 at 01:38
  • @JavaBeginner - please see the link i added in my answer – Apple Grinder Mar 30 '13 at 01:47
  • @JavaBeginner last case you ask is not polymorphism, since you need to know the exact child and make a downcasting in order to call the method. – Luiggi Mendoza Mar 30 '13 at 01:49
  • Plz correct me.In Animal objChimp = new Chimp(); the objChimp creation happens at compile time and its assignment new Chimp() happens at run time right – Java Beginner Mar 30 '13 at 02:00

3 Answers3

1
when I remove the ClimbTrees() Method why is it generating Error

Its simply because using the objChimp instance you can call the methods in Animal class. Since ClimbTrees() method is not in Animal class you are getting this error.

Edit: I think you are trying to learn overriding and Polymorphism. You should get more details here. In your case below is true. Am not explaining you the WHY factor in below examples I will leave it for your research.

// a. You can call only methods/variables is Animal class using objChimp instance
// b. If you are calling overridden methods, the method in Chimp class will be called in run time
Animal objChimp = new Chimp();

// a. You can call methods/variables both in Animal class and Chimp class using objChimp instance
// b. If you are calling overriden methods, the method in Chimp class will be called in runtime
Chimp objChimp = new Chimp();
Jayamohan
  • 12,734
  • 2
  • 27
  • 41
  • Thanks for Reply. But when I create object with Chimp Reference I can call method which are not in Animal Class. At this stage there is no polymorphism in code – Java Beginner Mar 30 '13 at 01:37
  • I understand when you say "method which are not in Animal Class." it means the methods in Chimp class. Is it right?. If you are saying yes, that method call is nothing to do with polymorphism, its just invoking a method in a class. – Jayamohan Mar 30 '13 at 01:40
  • Yes Methods in Chimp class but not in animal Class – Java Beginner Mar 30 '13 at 01:41
  • Yeah. In that case the method call has nothing to do with Polymorphism. Its Just another method call in a class. – Jayamohan Mar 30 '13 at 01:45
1

This is the error you will get - The method ClimbTrees() is undefined for the type Animal. Why does it happen ?

The compiler checks the static type of objChimp. It is animal. The dynamic type of objChimp is Chimp.

The compiler first checks if there is a method called ClimbTrees() in the static type of objChimp. If it does not find it, then it throws an error. But, when you don't remove the method, the compiler sees the static type and finds ClimbTrees(). Only when it finds that, it will let you compile your code. During run time, its checked if there is also a ClimbTrees() in the dynamic type of objChimp. If found, then execute the ClimbTrees() of chimp and not of animal. If not found, then execute ClimbTrees() of static type of objChimp, that is ClimbTrees() of Animal (comment the climb trees of chimp and see what happens).

Notes -

http://en.wikipedia.org/wiki/Type_system

Apple Grinder
  • 3,573
  • 7
  • 22
  • 35
  • Plz correct me.In Animal objChimp = new Chimp(); the objChimp creation happens at compile time and its assignment new Chimp() happens at run time right – Java Beginner Mar 30 '13 at 01:56
0

Since your objChimp is declared from Animal type, you can only use the Animal attributes/methods which you have access. If you want to call a specific attribute/method from a child, you should downcast it. This can make your code work (assuming you deleted ClimbTrees method from Animal class):

Animal objChimp = new Chimp();
((Chimp)objChimp).ClimbTrees(); 

However, you must be sure you can downcast the class to the specific class. Check Downcasting in Java for more info.

Community
  • 1
  • 1
Luiggi Mendoza
  • 85,076
  • 16
  • 154
  • 332