Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

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

Posted on 2006-11-02
4
Medium Priority
?
888 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
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

704 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