Help coderanch get a
new server
by contributing to the fundraiser
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Q about the Object need your help?

 
Ranch Hand
Posts: 77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
public class Test1
{
public static void main(String arg[])
{
Object bb=new Test1();
bb.speak();
}
void speak()
{
System.out.print("I am OK!");
}
}
when I comile the code the following is print out.
I think when the speak is invoke ,it only execute the speak of the instance method ,and the instance method speak has been defined,it should not give a copile error.
But ........
//---------compile error------------//
Test1.java:6: ������������
���������� speak ()
�������� in java.lang.Objec
bb.speak();
^
1 ������
//------------end-----------------//
Can you give me a explain about the above.
 
Author & Gold Digger
Posts: 7617
6
IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
First of all, bb is of type Object and speak() is not defined in Object. You have to cast bb to Test1 in order to get the compilation work !
------------------
Valentin Crettaz
Sun Certified Programmer for Java 2 Platform
 
Gong James
Ranch Hand
Posts: 77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think :
the method's calling is according to the type of the instance ,so when invoke the speak,it will call the speak method in class Test1 ,it will not to call the method in Object.
 
Ranch Hand
Posts: 5399
1
Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Gong James:
when I comile the code the following is print out.
But ........
//---------compile error------------//
Test1.java:6: ������������
���������� speak ()
�������� in java.lang.Objec
bb.speak();
^
1 ������
//------------end-----------------//
Can you give me a explain about the above.


Hi Gong
when I compile, got this error:
//---------compile error------------//
D:\JavaTp\Test011114.java:6: Method speak() not found in class java.lang.Object.
bb.speak();
^
1 error
//------------end-----------------//

I think now u must have got what is the error you are getting.
and methods are checked by the object reference variable(at compile time). That's why for overriding method should have same signature and return type.
at the runtime method is invoked of the object whom that ref variable is pointing.
HTH
CMIW
------------------
Regards
Ravish

[This message has been edited by ravish kumar (edited November 14, 2001).]
 
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
at compile time,the JVM check the oo as a Object Class,but the Class haven't a method called speak,so caused a error,if the Object Class have a speak() method too,then no error and at the runtime the "i am OK"will be output,because at runtime ,oo is regarded as a Test1 Class~
 
Don't listen to Steve. Just read this tiny ad:
We need your help - Coderanch server fundraiser
https://coderanch.com/t/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic