• Post Reply Bookmark Topic Watch Topic
  • New Topic

Help with Inheritance (super) and extends?

 
Avin Sinanan
Ranch Hand
Posts: 109
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello am kinda new with inheritance and extending
JButtons. Ive been trying to write the code to do the following:
1).There are 2 classes. One class makes a JButton
with a the label "hello" and sets the border painted around the button to false.
2)The second class is suppose to extend this class
and give that button additional features. It is suppose to add a picture to the button.
3) So when the main motho calls the second class it returns a button with the label "hello" , a false painted border and a gif picture attached to it.
I know you must be saying shy didn't I just combine everything into one class..I can do that.. but I have to get familiar with inheritance so that I can do a bigger project later on.
here is the code..can someone correct it for me please.. thanks in advance
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
import java.util.*;

public class ButtonExtend
{
public static void main(String[] args)
{
JFrame frame = new JFrame("Hello");
JPanel pane = new JPanel();
Button2 item = new Button2();
JButton b1 = item.button2();
pane.add(b1);

frame.getContentPane().add(pane);
frame.setSize(400,400);
frame.setVisible(true);


}
}
/*------------------------------*/
class Button1
{
public Button1()
{
JButton buttonOne = new JButton("Hello");
buttonOne.setBorderPainted(false);
}

public void button1()
{
}
}
/*--------------------------------*/
class Button2 extends Button1
{
public Button2()
{

}

public JButton button2()
{
super.button1();
JButton button = new JButton("",new ImageIcon("HLR.gif"));
return button;
}
}
 
Manfred Leonhardt
Ranch Hand
Posts: 1492
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Avin,
First off you are not extending JButton in any classes shown. Extension doesn't mean "has a" it means "is a". What that means is that you don't create a JButton inside a JButton extension because it is already a JButton. The following code performs what you want by extending JButton as you have described.

Regards,
Manfred.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!