Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7880271
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T04:06:19+00:00 2026-06-03T04:06:19+00:00

I have this Java Interface: public interface Box { public void open(); public void

  • 0

I have this Java Interface:

public interface Box {
   public void open();
   public void close();
}

This interface is extended by this class:

public class RedBox implements Box {

   public void open() {

   }

   public void close() {

   }
}

The problems is that I’m looking to add other classes in the future that will also implement the Box Interface. Those new classes will have their own methods, for example one of the classes will have a putInBox() method, But if I add the putInBox() method to the Box Interface, I will also be forced to add an empty implementation of putInBox() method to the previous classes that implemented the Box Interface like the RedBox class above.

I’m adding putInBox() to the Box Interface because there is a class Caller that takes an object of classes that implemented the Box Interface, example:

public class Caller {

    private Box box;
    private int command;

    public Caller(Box b) {
        this.box = b;
    }

    public void setCommandID(int id) {
        this.command = id;
    }

    public void call() {

        if(command == 1) {
            box.open();
        }
        if(command == 2) {
            box.close();
        }

        // more commands here...
    }
}

Caller c = new Caller(new RedBox());

c.call();

How do I implement the Box Interface in the new classes without been forced to add empty implementation of new methods to each of the previous classes that implemented the Box Interface.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-03T04:06:20+00:00Added an answer on June 3, 2026 at 4:06 am

    You are not limited to a single interface – you can build an entire hierarchy! For example, you can make these three interfaces:

    public interface Box {
        public void open();
        public void close();
    }
    public interface LockableBox extends Box {
        public void lock();
        public void unlock();
    }
    public interface MutableBox extends Box {
        public void putItem(int item);
        public void removeItem(int item);
    }
    

    Now your boxes can implement an interface from the hierarchy that fits your design.

    public class RedBox implements LockableBox {
        public void open() {}
        public void close() {}
        public void lock() {}
        public void unlock() {}
    }
    
    public class BlueBox implements MutableBox {
        public void open() {}
        public void close() {}
        public void putItem(int item) {}
        public void removeItem(int item) {}
    }
    

    With a hierarchy in place, you can continue programming to the interface:

    MutableBox mb = new BlueBox();
    mb.putItem(123);
    LockableBox lb = new RedBox();
    lb.unlock();
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have this class public class wordObject implements java.io.Serializable { String wordName; int occCount;
I have this class called PollFrame that extends JFrame in a file called PollFrame.java
Let's say I have the following Java interface that I may not modify: public
I have this Player class which implements the Comparable interface. Then I have an
I have this generic interface: public interface TjbListener<T> { public void hearChange(T t); }
So I have this interface public interface EventHandler<E extends EventObject> { public void handleEvent(E
I have a Java interface in an API that contains a single method: public
I have this structure: ///Creep.java/// public interface Creep extends Movable<Position2D> { ... } ///Movable.java///
I have this java servlet that grabs information from a form, I need to
I have this Java code which is used for JSF pagination: public List<ActiveSessionObj> list(int

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.