Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations biv343 on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Overriding clairification please... 1

Status
Not open for further replies.

asiavoices

Programmer
Sep 20, 2001
90
CA
Hello again all,

I have a question regarding how a method's result would look like if it was inherited down to 4 levels in a class hierarchy.

I understand that if you want to use a parent's method, you use a super.methodname(); but what if....

For example I have a base class called Person with a method called print() - just prints the common variables like name, address & phone number, etc..

Then I did this.... (each print() method in the subclasses has their own info to be printed).

Student extends Person
{
.....
print();
}

GradStudent extends Student
{
....
print();
....
}

PhdStudent extends GradStudent
{
.....
print();
.....
}

Am I correct that if I want to print all the "stuff" in the print methods in the above the hierarchy in the last one (PhdStudent) , this is what I have to do.

Person.print();
Student.print();
GradStudent.print();

Just wondering...

Thanks,

Christopher
 
The solution you gave only works if the print-methods are static. At least if you write Person.print() you actually execute a static print-method from the class Person.

I'm not completely sure it works that way(I could try it, but it's kinda late already), but a better solution might be:

public class PhdStudent {
...
public void print() {
((Person) this).print();
((Student) this).print();
((GradStudent) this).print();
}
...
}


Hope it works.

Swamphen
 
Hi Swamphen,

this is interesting... I never thought of using static to make it a class method.

you suggestion looks intriguing too although I've haven't learned that technique yet. It does make sense though because of the "this" referencing to the Class you want to use.

thanks,

Christopher.

P.s. any other suggestions or clarifications are welcome!

 
The technique is called class-casting.
You force the object to be of the class between the brackets.
That way the print method according to the class between the brackets will be used.

greetings,
Swamphen
 

So you can cast both primitive and reference types then, correct?

cheers,

Christopher
 
The use of static methods should not be use rampantly. In this case it probably doesn't make sense to have print() as a static method. The print() method is most likely going to be tied directly to the instance.
FYI, to call the method of a parent class use the super keyword. For the most part you will never really go more than one class up the class chain. But you could do:
Code:
super.super.print()
(and so on...) if you really needed to. Good design will almost always solve any need to do this type of thing.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top