0

I am trying to teach myself how to program in java and right now I'm trying to teach myself how to use inheritance.

I want to create a class named Pushbutton which inherits from the class Door. In my class Pushbutton, I have a method called status, which takes a boolean value called button and depending on the value button, will either call the method open or close in the class Door.

The issue I'm having is that in my main method, I am trying to call the method status as seen on line 25, but I'm getting an error message saying

The method status(boolean) is undefined for the type Main.

I can see how to fix this issue without using inheritance, but then that defeats the purpose of what I'm trying to accomplish. Does anyone know how I can fix this issue while still using inheritance?

I have tried making the classes Pushbutton and Door public as well but then I get new error messages saying the classes must be in their own file. Also, the original error message doesn't go away.

class Door{
    public void open() {
        System.out.println("Door is opened");
    }
    public void close() {
        System.out.println("Door is closed");
    }
}

class Pushbutton extends Door{
    public void status(boolean button) {
        if (button==true) {
            super.open();
        }
        else {
            super.close();
        }
    }

}

public class Main {
    public static void main(String[] args) {
        boolean button=true;
        status(button); //line 25
    }
}
4
  • 1
    You need to create an instance of Pushbutton before you can call the status method Commented May 5, 2019 at 23:41
  • 2
    ...for this case, Pushbutton should be a member variable of Door, because that's a "has a" relationship, and it would make sense that a Door "has a" Pushbutton. Commented May 5, 2019 at 23:52
  • Just because you can implement something via inheritance doesn't mean you should. While I encourage experimentation, I'm wary of inheritance models that don't make any sense, for fear that they'll lead you down a path of not understanding where OOP makes sense, and where it doesn't. Commented May 5, 2019 at 23:52
  • In general, a superclass is usually a generalization, and subclasses are more specific instances of that generalization. So, for example, you might have an "Animal" superclass, and then a bunch of subclasses of "Animal", like "Monkey", "Pig", Bear", Frog", etc. - The canonical example is "Shape", with subclasses "Circle", "Square", etc. The superclass would define a "draw" method, and then each subclass would implement their own version of "draw" to draw the shape they represent. Commented May 5, 2019 at 23:54

2 Answers 2

1

you need to create object of type Pushbutton first to be able to use a non-static method that exists in Pushbutton class

here:-

class Door{
    public void open() {
        System.out.println("Door is opened");
    }
    public void close() {
        System.out.println("Door is closed");
    }
}

class Pushbutton extends Door{
    public void status(boolean button) {
        if (button==true) {
            super.open();
        }
        else {
            super.close();
        }
    }

}

public class Main {
    public static void main(String[] args) {
        boolean open=true;

        // create object of type Pushbutton
        Pushbutton button = new Pushbutton();

        // call the method status from the object
        // and pass it the boolean parameter
        button.status(open); //line 25
    }
}

also i think you should not call it "Pushbutton", a button is not a Door (logically), maybe call it HomeDoor because home doors open and close?

Sign up to request clarification or add additional context in comments.

Comments

0
  1. The class Main with the static main method is the entry of your program.
  2. Whenever you want to invoke a non-static method of a class in another class, you must define the instance of the class before invoking its methods. You can directly call a method in another method if these methods belong to the same class.
  3. So you want to invoke status method of Pushbutton class, first declare the instance of Pushbutton, PushButton pushbutton = new Pushbutton(), then call its method pushbutton.status().
public class Main {
    public static void main(String[] args) {
        boolean open=true;
        // create an instance of Pushbutton
        Pushbutton pushbutton = new Pushbutton();
        // invoke the method and pass it a boolean value
        button.status(open); //line 25
    }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.