Click here to Skip to main content
13,259,194 members (48,909 online)
Rate this:
Please Sign up or sign in to vote.
See more:
Hi morning,

Is a Possibility for Child class methods calls with parent class object?
I can instantiate parent class and also i want to retrieve Child class methods..

is it possible?

Than Q
Posted 7-Oct-12 18:09pm
What do you mean by "parent" and "child", exactly? Base and derived classes? What is "call with"? What is "retrieve"? Why not just reading of an elementary textbook or reference?
Rate this: bad
Please Sign up or sign in to vote.

Solution 1

There are questions here. In a first one, it's not clear what does it mean: with parent class object. In is ambiguous phrase. Do you mean with parameter of a "parent" class object, or what? Very generally, and object of a derived class ("child" and "parent" are not quite precise terms, probably you mean "base" and "derived" classes) can do everything an object of a base class do.

The second question is the opposite. If you instantiated an object of a base class, it does not no anything about its derived class. Besides, the term "retrieve" a method makes no sense. There is something similar in Reflection, but I don't thin this is related to what you meant to ask about.

If you mean something else, please explain. The question is full of uncertainties.

Rate this: bad
Please Sign up or sign in to vote.

Solution 2

Do you think of "strategy pattern"? Well, you do not call the methods of the derived class directly - but you do call them.
Imagine a function like:
public virtual void DoSomething()
protected abstract void DoStep1();
protected virtual void DoStep2()
    //some code

When DoSomething is called, the DoStep1 method, and possibly the DoStep2 method, of the derived class are being called.

[Edit] Those are C# signatures, but the concepts are also available in Java. [/Edit]

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy |
Web04 | 2.8.171114.1 | Last Updated 8 Oct 2012
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100