aspose file tools*
The moose likes Beginning Java and the fly likes Parent Child Relation Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Parent Child Relation" Watch "Parent Child Relation" New topic
Author

Parent Child Relation

Tarun Singh
Greenhorn

Joined: Aug 10, 2004
Posts: 4
class SuperParent
{
public void printMessage()
{
System.out.println("Super Parent");
}
}
class Parent extends SuperParent
{
public void printMessage()
{
System.out.println("Parent");
}
}
public class Child extends Parent
{
public void printMessage()
{
System.out.println("Child");
}
public static void main(String s[])
{
???
}

}
How to call printMessage() of SuperParent class so, the output should be 'Super Parent'.


tarun kumar singh<br />Leader Soft LLC.<br />Dubai,UAE
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24187
    
  34

Well, of course, you can call it on an instance of SuperParent -- i.e., nothing stops you from doing this in Child.main():

SuperParent sp = new SuperParent();
sp.printMessage();

But I suspect what you're asking is whether there's any way for an instance of Child to invoke SuperParent's printMessage() on itself. The answer is no -- Java simply doesn't allow it, so don't waste any energy trying to figure out a way!


[Jess in Action][AskingGoodQuestions]
Tarun Singh
Greenhorn

Joined: Aug 10, 2004
Posts: 4
Hi Ernest Friedman,
My question is if i want to call SuperParent method printMessage() without creating it's Object.
we can call super.printMessage(), it will give you output "Parent". So there is any way to call SuperParent Class from the Child without creating it's object.
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24187
    
  34

As I said, no. A class can call methods it directly inherits from its superclass, but it can't call methods from the "super-superclass".
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Parent Child Relation