简体   繁体   中英

Calling method By reference type

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
 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();

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM