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

  • SEARCH
  • Home
  • 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 3219264
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T15:37:30+00:00 2026-05-17T15:37:30+00:00

I have a class say ‘A’, an object ‘a’ of which creates an object

  • 0

I have a class say ‘A’, an object ‘a’ of which creates an object ‘b’ of class’B’. Now ‘b’ runs a method which wants to create a new instance of b’s parent(please dont confuse it with inheritance) – the class which created ‘b’, which is ‘A’ as in here.

Is this anyhow possible in Java ? Or maybe some design suggestion may work.

Thanks !

Edit : As per suggestions, I am adding my design requirement too. I have an “ExitController” class, which works differently as per the caller function – like if it is invoked by a UserX, it terminates program, if it is invoked by a UserY it logs him off, etc.
Typically I should be placing an ExitController method in my User interface, but there are some constraints due to which I cant. Also, passing the parent object/classname is seeming very expensive compared to the use it offers.

  • 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-05-17T15:37:31+00:00Added an answer on May 17, 2026 at 3:37 pm

    Basically, you’ve to pass the parent itself or the parent type into B’s constructor. The below example illustrates the last case.

    class A {
        B b;
    
        A() { 
            b = new B(getClass());
        }
    }
    
    class B {
        Class<?> parentClass;
    
        B(Class<?> parentClass) {
            this.parentClass = parentClass;
        }
    
        void createParent() throws Exception {
            Object o = parentClass.newInstance();
        }
    }
    

    With generics you could make it more type safe. But since the functional requirement is not entirely clear, I’ve left that away.


    Update as per your update, creating instances is pointless. You could just check the class type and handle accordingly. Instead of createParent() in above example, do:

    void exit() {
        if (parentClass == UserX.class) {
            // Terminate program.
        } else if (parentClass == UserY.class) {
            // Logoff user.
        }
    }
    

    This is however plain ugly. Are you really not allowed to add another method to the User interface? Otherwise the following would be a better design (called the Strategy pattern):

    interface User {
        void exit();
    }
    
    class ExitController {
        void exit(User user) {
            user.exit();
        }
    }
    
    class UserX implements User {
        public void exit() {
            // Terminate program.
        }
    }
    
    class UserY implements User {
        public void exit() {
            // Logoff user.
        }
    }
    

    As per your last statement:

    Also, passing the parent object/classname is seeming very expensive compared to the use it offers

    This makes no sense. Have you measured it? Show us the results.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Let's say you have a class called Customer, which contains the following fields: UserName
Let's say I have a class that has a member called data which is
I have a class that contains a dynamically allocated array, say class A {
Say I have class A with class A { final String foo() { //
Say we have: class Base { virtual void f() {g();}; virtual void g(){//Do some
Let's say I have a class: class Foo { public string Bar { get
Let's say you have a class with a Uri property. Is there any way
Let's say I have a class in my web app called class Foo. It
Let's say I have one class Foo that has a bunch of logic in
Let's say I have a class that implements the IDisposable interface. Something like this:

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.