Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

How to call a function/method in the child class from thr father class?

Posted on 2006-11-02
4
Medium Priority
?
890 Views
Last Modified: 2012-06-21
I request this question again because I have another problem now.

My code look as follow:

public classFather {
...
   protected abstract doNewA();
   protected abstract doNewB();
...
   doNewA();
   doNewB();
...
}

public classChildA extends classFather {
...
   doNewA();
   doNewB();
...
}

public classChildB extends classFather {
...
   doNewB();
   doNewA();
...
}

The problem is: If I call doNewA() in ClassA from father class, the doNewA in ClassB is called. How can I avoid it?
0
Comment
Question by:chenwei
  • 2
4 Comments
 

Accepted Solution

by:
carljokl earned 120 total points
ID: 17866810
I think that this may be a potential design issue. If I had a better idea of what you wish to accomplish by calling the specific child's method. I would question whether a problem like this might be an indication that the design of your program might not be appropriate to the task. I am not sure if there is a way to force a specific overidden method in a class hierachy to be run. I think the reason is that it should not be neccecary.

If a subclass has the need to overide methodA() in order to work properly with class ChildA then it was overidden in order to make the method work safely therefore forcing the class to run one of it's parents version of the method (except in the case that the subclass it's self decides that you should do so using the super.methodA()) might cause potential problems.

I must say I don't know if what you are trying to do is possible. I don't know the specific problem you are working on but suspect that there is an alternative way of designing your program to achieve the same results. Sometimes Java does not let you do something because it is not good practice.
0
 

Expert Comment

by:bbkeppler
ID: 17868294
I would look closely at which class you are actually instantiating.  Consider the following code:

...
Father a = new ChildA();
a.doNewA();  // This will call the overridden doNewA() method defined by ChildA.
Father b = new ChildB();
b.doNewA();  // This will call the overridden doNewA() method defined by ChildB.
...

Again, make sure that the object you have a reference to is actually of the class you think it is.  ChildA is not aware of ChildB because ChildB is not in its class hierarchy, so it would be impossible for an object whose concrete class is ChildA to exhibit behavior specific to ChildB.
0
 

Expert Comment

by:bbkeppler
ID: 17868395
One more point on the above comment.  Consider the following code:

public class Father {
  ...
  public void foo() {
    ...
    doNewA();
    ...
  }
  ...
  public void doNewA() {
    ...
  }
  ...
}
...
Father a = new ChildA();
a.foo();
Father b = new ChildB();
b.foo();
...

Assume that ChildA and ChildB do not override foo().  Now, the a.foo() call will result in the doNewA() method defined by ChildA being called even though the method call is made in the foo() method defined in Father.  Likewise, the b.foo() call will result in the doNewA() method defined by ChildB being called.
   
0
 

Author Comment

by:chenwei
ID: 17872373
Many thanks to bbkeppler's example codes. But I think carljokl's commment comes near to my points. :-)
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
Suggested Courses

824 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question