Solved

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

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

[Webinar] Code, Load, and Grow

Managing multiple websites, servers, applications, and security on a daily basis? Join us for a webinar on May 25th to learn how to simplify administration and management of virtual hosts for IT admins, create a secure environment, and deploy code more effectively and frequently.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
runtime exception 2 67
restrict decimal places for double datatype 10 50
What browser will run Java? 7 186
Find out all repos that a user is most active on Github 1 58
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…
In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
This video teaches viewers about errors in exception handling.
Suggested Courses

710 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