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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T23:34:09+00:00 2026-06-04T23:34:09+00:00

I have following code. Parent Class: class Parent{ void a() { if(// some logic

  • 0

I have following code.

Parent Class:

class Parent{

        void a()
        {   
             if(// some logic to check whether child class is calling this method or some other class//)
             System.out.println("Child class is calling..")}
        }

Child Class :

class Child extends Parent{

        void b(){System.out.println(new Parent().a());}
}

Some Other Class :

 class Child1{

        void b(){System.out.println(new Parent().a());}
}

Here I am calling Parents a() method from one child class and from other class.

My question is what logic should i put in Parent’s a method’s if block so I can determine which class is calling this method .

I have some code but that is not working.Please help me what is wrong in this code.

if(this.getClass().getClassLoader()
            .loadClass(new Throwable().getStackTrace()[0].getClassName())
            .newInstance() instanceof Parent)
 {
           System.out.println("Child class is calling..")}
 }
  • 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-04T23:34:12+00:00Added an answer on June 4, 2026 at 11:34 pm

    You have these options:

    1. Move Parent to a different package and make a() protected. Since there are no other classes in the new package, only subclasses will be able to call the method.

    2. You can try the stack trace approach but the information you seek isn’t in the first frame – the first frame is where you call new Throwable(). Try frame 1 instead (that’s the place where a() was called):

      ...getStackTrace()[1]...
      

      If you care about performance, you should cache this result.

    That said, I’m wondering what the purpose of this code is. My gut feeling is that you’re fixing a deeper problem by creating a brittle work around which will cause different pains. Maybe you should tell us what you want to achieve (the big picture).

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

Sidebar

Related Questions

Say I have the following code: class Parent { static string MyField = ParentField;
I have the following code: public class Parent { public string MyField { get;
I have the following classes class Parent { virtual void doStuff() = 0; };
I have the following 2 code snippets. Assume I have a Parent class and
I have the following code: template<typename Parent, typename T, void (Parent::*Setter)(T), T (Parent::*Getter)()> struct
I have the following code fork()'s 2 children from a common parent and implements
I have the following code: <script> $(document).ready(function() { $('.toggle_section').click(function(e){ parent = $(e.target).closest(div) objChild =
I have following code in the html file: <div class=navbar navbar-fixed-top> <div class=navbar-inner> <div
I have the following code in Main.java : public class Main extends Activity implements
If I have 2 classes, one being the parent with the following: public class

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.