• 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

sending message to object

 
Ranch Hand
Posts: 71
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi, there
As far as i know, when we send a message to a object, we use the method of this object, am i right?
for instance: system.out.println("something"); as far as i know the method println() is belong to the instance(system.out)of printStream class.
however, i don't understand the following code; the compiler objects to the line1.
this.add(third) means send a message to this object(current object). As far as i know, the method add()is belong to this object which is an instance of TestLayout class. So what is wrong with the code?(someone said it is because there is no add()method in Object class, but my argument is we don't need the method of Object class.)
import java.awt.*;
public class TestLayout extends Frame{
public TestLayout(){
Button first=new Button("first");
Component second=new Button("second");
Button third= new Button("third");
setLayout(new FlowLayout());
this.add(first);
this.add(second);
this.add(third);//line1
pack();
setVisible(true);
}
public static void main(String args[]){
TestLayout tf=new TestLayout();
}
}
 
Ranch Hand
Posts: 464
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
No problemo...
Compiles just fine... and displays 3 buttons nicely
 
fengqiao cao
Ranch Hand
Posts: 71
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi, there
As far as i know, when we send a message to a object, we use the method of this object, am i right?
for instance: system.out.println("something"); as far as i know the method println() is belong to the instance(system.out)of printStream class.
however, i don't understand the following code; the compiler objects to the line1.
this.add(third) means send a message to this object(current object). As far as i know, the method add()is belong to this object which is an instance of TestLayout class. So what is wrong with the code?(someone said it is because there is no add()method in Object class, but my argument is we don't need the method of Object class.)
import java.awt.*;
public class TestLayout extends Frame{
public TestLayout(){
Button first=new Button("first");
Component second=new Button("second");
Object third= new Button("third");
setLayout(new FlowLayout());
this.add(first);
this.add(second);
this.add(third);//line1
pack();
setVisible(true);
}

sorry, third should be type Object.
 
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The problem is not that Object has no "add" method. As you correctly figured out, that isn't what's happening here. The problem is that Frame has no "add" method that accepts an argument of type Object. You can only "add" Component's and PopupMenu's. You can't "add" Object's. Even though it happens that the Object reference is actually pointing to a Component (Button), the compile will require an explicit cast to Component or to a sublcass of Component before it will accept the statement.
 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ah well in that case, since the add() method of Frame (inherited from Container) expects a reference to a Component as it's argument, you must make an explicit cast to satisfy the compiler:
this.add((Component)third);
You could equally cast it to a Button.
 
fengqiao cao
Ranch Hand
Posts: 71
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
nice shot. thank you very much.
 
reply
    Bookmark Topic Watch Topic
  • New Topic