my dog learned polymorphism*
The moose likes Beginning Java and the fly likes Extend confused Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Extend confused" Watch "Extend confused" New topic
Author

Extend confused

Roldan Baldo
Ranch Hand

Joined: Aug 11, 2009
Posts: 99
what is the difference between

class myClass extends JPanel{
}

and

class myClass{
JPanel panel = new JPanel();
}

because i got confused.
Rahul P Kumar
Ranch Hand

Joined: Sep 26, 2009
Posts: 188
in case of

class myClass extends JPanel{
}


myClass is child class of JPanel and inherits all methods of its super class. To use super class methods in any of this child class method you don't have to instantiate JPanel object. you can override the behavior of super class methods as well in myClass.

in case of

class myClass{
JPanel panel = new JPanel();
}


myClass is like any class, which might want to use methods of JPanel class. Two classes are not related any other way in this case.
Roldan Baldo
Ranch Hand

Joined: Aug 11, 2009
Posts: 99
which class would be better to use? the extend one or the instantiated one?
Rahul P Kumar
Ranch Hand

Joined: Sep 26, 2009
Posts: 188
if you want to override JPanel methods in some way, you want to extend it. If want to use some of the method, that's all and keep your class open to inherit other classes, instantiate JPanel.
Roldan Baldo
Ranch Hand

Joined: Aug 11, 2009
Posts: 99
thanks for the answer. i now understand a little bit.
Rahul P Kumar
Ranch Hand

Joined: Sep 26, 2009
Posts: 188
welcome
s.palanivel rajan
Ranch Hand

Joined: Sep 22, 2009
Posts: 40
according to me..
class a
{
jpanel p=new jpanel();
}

is the best way..
because whenever we need to extend any base class in future this method will helps that time...


With Regards,
S.Palanivel Rajan B.E.
Ruben Guillen
Greenhorn

Joined: Sep 02, 2009
Posts: 28
Dear Roldan

Please also note that you are using two OO concepts in your question. Inheritance and composition. Read these links to have a better understanding of your examples approach.

Regards.


SCJP, OCMJD
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Extend confused
 
Similar Threads
Confusion about class creation inside a class.
class
remove method
? from a newbie
Circular GC