The moose likes Beginning Java and the fly likes Difference between calling an object in 2 ways(please read for more info) Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Difference between calling an object in 2 ways(please read for more info)" Watch "Difference between calling an object in 2 ways(please read for more info)" New topic
Author

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

Ransika deSilva
Ranch Hand

Joined: Feb 18, 2003
Posts: 524
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


SCJP 1.4, SCMAD 1.0<br />SCWCD, SCBCD (in progress)
Sanjeev Kaushik
Ranch Hand

Joined: Aug 01, 2002
Posts: 105
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,


Sanjeev Kaushik
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
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.


The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
 
 
subject: Difference between calling an object in 2 ways(please read for more info)