• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Extend confused

 
Roldan Baldo
Ranch Hand
Posts: 99
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 188
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 99
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
which class would be better to use? the extend one or the instantiated one?
 
Rahul P Kumar
Ranch Hand
Posts: 188
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 99
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks for the answer. i now understand a little bit.
 
Rahul P Kumar
Ranch Hand
Posts: 188
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
welcome
 
s.palanivel rajan
Ranch Hand
Posts: 41
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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...
 
Ruben Guillen
Greenhorn
Posts: 28
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Consider Paul's rocket mass heater.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic