0

I am fairly new to Java and I am writing a program that has two child classes using the same parent method, and I wanted to make a way to track which one calls the method.

Example:

public class Parent(){
    public static int ChildOneUses, ChildTwoUses;
    public void Example(){
        /* code */
        if(/*ChildOne called the method*/)
            ChildOneUses++;
        else if(/*ChildTwo called the method*/)
            ChildTwoUses++;
    }
}

public class ChildOne extends Parent(){
    /* code */
}
public class ChildTwo extends Parent(){
    /* code */
}

public static void main(String[] args){
    ChildOne ChildOne = new ChildOne();
    ChildTwo ChildTwo = new ChildTwo();

    ChildOne.Example();

    ChildTwo.Example();
    ChildTwo.Example();
}

/* If this code were to run, ChildOneUses would equal 1 and ChildTwoUses would equal 2 */

I am not sure that this question even has an answer other than just moving the parent method to each of the child classes to track them on their own.

Zelkins
  • 723
  • 1
  • 5
  • 22
  • 1
    Even if there's a way to do it, this is almost certainly bad oop style. – Mshnik Dec 23 '16 at 21:13
  • _Why_ do you want to track the method calls like this? What would be the purpose? – Mick Mnemonic Dec 23 '16 at 21:21
  • I am still in the process of learning java and I like to write random programs that I can use to help me learn and cement these new ideas in my memory, and sometimes they get me in positions where I need to learn new things (Like this), to which I then try to find the answers – Zelkins Dec 23 '16 at 21:23

2 Answers2

2

You could set a childType in the parent class.

public class Parent(){
   public static int childOneUses, childTwoUses;
   public static int childType;

   public Parent(int childType) {
      this.childType = childType;
   }

   public void example(){
    if(childType == 1) {
        childOneUses++;
    }
    else if(childType == 2) {
        childTwoUses++;
    }
   }
}

public class ChildOne extends Parent(){
  public ChildOne() {
    super(1);
  }
}

public class ChildTwo extends Parent(){
  public ChildTwo() {
    super(2);
  }
}

public static void main(String[] args){
ChildOne childOne = new ChildOne();
ChildTwo childTwo = new ChildTwo();

childOne.example();

childTwo.example();
childTwo.example();
}
Brian Pipa
  • 808
  • 9
  • 23
  • Thanks, really helpful! I'm still getting the hang of parent-child classes and this is a cool trick to know that can be applied to other things! – Zelkins Dec 23 '16 at 21:35
0

u can make in the child class a method that show the name of the class or u can make in the parent class this method and let the other childs earn the method now evry time u want to track u let the ocject call this method nameOfObject.getClass().getName()

for example if u take string s and write s.getClass().getName() the output is java.lang.String now u can play with this output and take only the last word if u want and u got the class name

shmuel
  • 13
  • 6