In: Computer Science
Create a sub class of the class Box and call it SBox. The SBox class has inherited the list of variables and methods from Box & has its own members too.
a. It has a variable color having a String data type.
b. This class also has a method called SetColor( ) which read the color from the user (Use JOptionPane), set the color to the value given by the user and display it.
import javax.swing.*;
public class Box
{
private String name;
protected String getname()
{
return this.name;
}
protected void setname()
{
this.name=JOptionPane.showInputDialog(null,"Please
enter box name");
}
protected void displayName (JFrame jFrame)
{
JOptionPane.showMessageDialog(jFrame,"Box name is" + this.name);
}
public static void main(String[] args)
{
SBox sBox= new SBox();
sBox.setName();
sBox.setcolor();
JFrame frame = new JFrame("SBox");
frame.setSize(200,200);
frame.setLocationRelativeTo(null);
frame.setVisible(false);
sBox.displayName(frame);
sBox.displayColor(frame);
}
class SBox extends Box
{
private String color;
public void setColor()
{
this.color = JOptionPane.showInputDialog(null,"Please
enter box color:");
}
public void displayColor(JFrame jFrame)
{
JOptionPane.showMessageDialog(jFrame, "Color of box
named" + this.getName() + "is" + this.color);
}
}