Click here to Skip to main content
15,881,027 members
Please Sign up or sign in to vote.
2.33/5 (3 votes)
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
Comments
Sergey Alexandrovich Kryukov 8-Oct-12 2:13am    
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?
--SA

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.

—SA
 
Share this answer
 
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:
C#
public virtual void DoSomething()
{
    DoStep1();
    DoStep2();
}

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]
 
Share this answer
 
v2

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900