Solved

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

Posted on 2006-11-02
4
878 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 30 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

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
nestparen challenge 4 57
wordappend challenge 8 88
create a gui in perl 3 46
@SBGen Method 3 25
For customizing the look of your lightweight component and making it look opaque like it was made of plastic.  This tip assumes your component to be of rectangular shape and completely opaque.   (CODE)
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 “while” loop and how to utilize it correctly in Java. Additionally, viewers begin exploring how to include conditional statements within a while loop and avoid an endless loop. Define While Loop: Basic Example: Explanatio…
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…

744 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now