Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: Java
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 17:09pm
Comments
Sergey Alexandrovich Kryukov at 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
Rate this: bad
good
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.
 
—SA
  Permalink  
Rate this: bad
good
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()
{
    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]
  Permalink  
v2

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

  Print Answers RSS
Your Filters
Interested
Ignored
     
0 Shai Vashdi 1,518
1 Tadit Dash 350
2 Manas Bhardwaj 319
3 OriginalGriff 243
4 Sergey Alexandrovich Kryukov 205
0 Sergey Alexandrovich Kryukov 9,530
1 OriginalGriff 5,716
2 Peter Leow 4,320
3 Maciej Los 3,540
4 Abhinav S 3,373


Advertise | Privacy | Mobile
Web03 | 2.8.140415.2 | Last Updated 8 Oct 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Use
Layout: fixed | fluid