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 8336485
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T04:00:20+00:00 2026-06-09T04:00:20+00:00

I have a class with objects Now i want to call a function from

  • 0

I have a class with objects
Now i want to call a function from Box and Toy object from outside of the Container class

class Container
{
   Box box1 = new Box();
   Toy toy1 = new Toy();
   public void open()
   {
      box1.open();
   }
   public void play()
   {
      toy1.play();
   }
}

How can i avoid recreating the methods and just sharing the methods with the Container class.
I can not use inheritance because I have 2 or more objects.

  • 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-09T04:00:21+00:00Added an answer on June 9, 2026 at 4:00 am

    You can do it as follows.

    public interface ToyInterface {
        public void play();
    }
    
    public class Toy implements ToyInterface {
        @Override
        public void play() {
            System.out.println("Play toy");
        }
    }
    
    public interface BoxInterface {
        public void open();
    }
    
    public class Box implements BoxInterface {
        @Override
        public void open() {
            System.out.println("open box");
        }
    }
    
    public class Container implements ToyInterface, BoxInterface {
        private BoxInterface box;
        private ToyInterface toy;
    
        public Container() {
            box = new Box();
            toy = new Toy();
        }
    
        public BoxInterface getBox() {
            return box;
        }
    
        public ToyInterface getToy() {
            return toy;
        }
    
        @Override
        public void play() {
            System.out.println("play container");
            this.toy.play();
        }
    
        @Override
        public void open() {
            System.out.println("open container");
            this.box.open();
        }
    }
    

    Then you can access methods of Box and Toy class outside of the Container.

    Container container = new Container();
    container.open();
    container.getBox().open();
    
    container.play();
    container.getToy().play();
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Let's say I have created two objects from class foo and now want to
I have an object now: class Items attr_accessor :item_id, :name, :description, :rating def initialize(options
Basically I have some class objects, each with three properties. Once one class object
I have class A and list of A objects. A has a function f
I have a vector of pointers to class objects. These class objects invoke new
I want to have a SelectAll function which takes in a few arguments (class,
I have two objects that are derived from same the base class. Lets say
I have protected function which creates a class object protected function x() { $obj
I have Class and Student objects. Both have collection of another as property. Which
In Java, do class objects have the same heritance relations as the classes they

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.