File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Difference between calling an object in 2 ways(please read for more info)

 
Ransika deSilva
Ranch Hand
Posts: 524
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello World,
I have seen and used these two methods to invoke a class which extends a JFrame.
JFrame jf = new SomeClass();
jf.show()

and
SomeClass obj = new SomeClass();
obj.setVisible(true);
Could you please tell me the difference between these two ways. Infact is there a difference?
Thanks
 
Sanjeev Kaushik
Ranch Hand
Posts: 105
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In the first case, You can call all the methods which are defined in JFrame only, but you can not call any method is which is added in SomeClass. Because parent class does not know what has been added in its child class.
In the second case, you can call all the methods of SomeClass and all the methods inherited from its parent class JFrame.
Hope it is clear.
Cheers,
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The advantage of the first style is that you can switch from SomeClass to a different implementation by just changing the instantiation - you *know* that all the code just needs a subclass of JFrame.
The disadvantage is, of course, that all the code can only use methods defined by JFrame.
My advice would be to go with the first approach as long as you can. That is, always code to the least specific interface possible.
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic