Reputation: 517
I have a problem where I have some class say
public class Foo {
public Foo() {
// Do stuff
}
public void generalMethod() {
//to general stuff
}
}
public class Bar extends Foo {
public Bar() {
//do stuff
}
public void uniqueMethod() {
//do stuff
}
}
public class Driver() {
public static void main(String[] args) {
Foo test = new Bar();
test.uniqueMethod(); //this causes an error
}
}
So i get an error which says, that the method uniqueMethod()
is undefined for Foo, but when I change the assignment to Bar test = new Bar();
the problem goes away. I dont understand as it should work with Foo test = new Bar();
Can someone suggest a reason for which why this error occurs?
Thank you in advance.
Upvotes: 1
Views: 90
Reputation: 41
useing late binding you must have the same method in the super class "Fathor" , or it will not work !
this work with me
//
public class foo {
public void print(){
System.out.println("Super,"+2);}
}
class bo extends foo {
public void print(){
System.out.println("Sup,"+9);
}
}
Test //
public abstract class test {
public static void main(String[] args) {
// TODO Auto-generated method stub
foo t = new bo();
t.print();
}
} ///
output
Sup,9
Upvotes: 2
Reputation: 3089
Although the method is there at runtime, the compiler only sees that the object test
of type Foo
DOES NOT have the method uniqueMethod. Java works with virtual method, where the method that will be invoked is determined at runtime
. So, again, the compiler does not see that this specific instance's type is a Bar
. If you really need to invoke that method, you could a cast and invoke the method:
((Bar)test).uniqueMethod();
Now, to the compiler, you have the type Bar
, and he can see all the methods that are available at Bar
type
Foo test = new Bar();
So, I have one variable that is of type Foo. I MUST validate that all subsequent calls to members of this class are OK - It will look for all members in Foo
class, the compiler doesnt even look for the instance type.
In runtime
, the JVM will know that we have a variable of type Foo
, but with instance type Bar
. The only difference here is that it, JVM, will look to the instance to make the virtual calls to the methods.
Bar test = new Bar();
So, I have one variable that is of type Bar. I MUST validate that all subsequent calls to members of this class are OK - It will look for all members in Foo
and 'Bar' classes, the compiler, again, doesnt even look for the instance type.
In runtime
, the JVM will know that we have a variable of type Bar
, and an instance of the same type:Foo
. Now we again, have access to all the members defined in Foo
and Bar
classes.
Upvotes: 2
Reputation: 4372
This error occurs because the compiler does not know that test
is of type Bar
, it only knows the declared type of Foo
.
public void test(Foo foo) {
// error! this method can accept any variable of type foo, now imagine if you passed it
// another class that extends Foo but didnt have the uniqueMethod. This just wont work
foo.uniqueMethod();
}
Upvotes: 1