Question:
Java Netbeans, How to create a class that has a superclass?
Danny
2010-11-16 03:51:03 UTC
Im new to Java, I have created a class called marble for a chess game, now I need to inherite the other pieces from it, How can I create my new classes telling them who is your superclass (father)?
Is there any wizard style to do it like Eclipse?

Thanks
Three answers:
2010-11-16 04:55:06 UTC
Assuming your class was called Marble and has a default constructor your child classes should look something like this:



public class BabyMarble extends Marble

{

    // members go here



    /**

     * Cosntructor:

     */

    public BabyMarble()

    {

        super(); //must be first statement in cosntructor.



        //do your initialisation here

    }



    // methods go here



}



The use of 'super();' invokes the construction of a parent class object first, which will then be extended by this class to become a new object. you have to have this call as the first statement in your constructor, you can't do it afterwords. super(); basically means 'this = new Marble()' and can be swapped for any other constructor that the parent class provides, so for example if the parent class has only one constructor that requires two string arguments then you would call:



super(arg1, arg2);



In its place. Remember your constructor of the child object is still a normal constructor so here's another example that uses a non-default cosntructor [i.e. the 2 string constructor]



public class BabyMarble extends Marble

{

    // a new third argument provided for BabyMarbles

    private String myArg;



    /**

     * Cosntructor:

     */

    public BabyMarble(String arg1, String arg2, String arg3)

    {

        super(arg1, arg2);

        myArg = arg3;

    }



    // methods go here



}
?
2010-11-16 04:08:45 UTC
As a project evolves, you might need to add levels to your inheritance hierarchy. For example, if you have two or more classes with essentially duplicate methods that are not formally related, you might want to create a superclass to hold these common methods. Doing so will make your code easier to read, modify, and extend, whether now or in the future.



You can use the Extract Superclass command to create such a superclass based on methods in one of the classes that you want to turn into a subclass. For each method that you add to the superclass, the Extract Superclass command enables you to choose between the following two options:



Moving the whole method to the superclass

Creating an abstract declaration for the method in the superclass and leaving the implementation in the original class

To extract a new superclass:



In the Source Editor or the Projects window, select the class that contains the methods that you want to be extracted into the new superclass.

Choose Refactor | Extract Superclass.

In the Extract Superclass dialog box, select the checkbox for each method and field that you want to be moved to the new superclass. Private methods and private fields are not included.



If you want to leave a method implementation in the current class and create an abstract declaration for the method in the superclass, select the Make Abstract checkbox for the method.



If the class from which you are extracting a superclass implements an interface, a checkbox for that interface is included in the Extract Superclass dialog box. If you select the checkbox for that interface, the interface is removed from the implements clause of the class that you are extracting from and moved to the implements clause of the new superclass.



Click Next.



If you have deselected the Preview All Changes checkbox, the changes are applied immediately.



If you leave the Preview All Changes checkbox selected, the Refactoring window appears with a preview of the changes.



In the Refactoring window, look at the preview of the code to be changed. If there is a modification that you do not want to be made, deselect the checkbox next to the line for that change.

Click Do Refactoring.



If you later find that the refactoring has had some consequences you would like to reverse, you can choose Refactor | Undo.



After you have extracted the superclass, you can use a combination of the following techniques to complete the code reorganization:



Add the name of the new superclass to the extends clause of any other classes that you want to extend the new superclass.

Use the Pull Up command to move methods from other classes to the new superclass. As with the Extract Superclass command, you can move whole methods or merely create abstract declarations for the methods in the new superclass. See Moving Class Members to Other Classes earlier in this chapter.

For other classes that you want to extend the new superclass, use the Override and Implement Methods feature (Ctrl-I) to add methods from the superclass to those classes. See Generating Methods to Override from Extended Classes earlier in this chapter.

Use the Use Supertype Where Possible command to change references in your code to the original class to the just created superclass. See Changing References to Use a Supertype below.
2016-02-27 03:36:32 UTC
This is the java! You have to write the code for your requirement. If you want to create GUI application, you can create new JFrame in the menu File>new If you want to create Console applications, first thing is to create the main method in your class.(It can be generated via wizard) or type psvm and press tab button on the keyboard. Now you can see the main method of your class. Then you have to learn java. write this code as your first program Type sout in the main method and the press a tab.You can see println method call. or System.out.println("This is my HeLLo app"); then run the file by right clicking on it.


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...