Solved

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

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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Android development question 2 55
sql import cannot be resolved jsp 3 50
Notify sent to other threads in Java 9 34
American Express @Work site and Java 4 33
Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
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:
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.

820 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