aspose file tools*
The moose likes Beginning Java and the fly likes overriding in inheritance (just try & reply) Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "overriding in inheritance (just try & reply)" Watch "overriding in inheritance (just try & reply)" New topic
Author

overriding in inheritance (just try & reply)

mohan gavande
Ranch Hand

Joined: Oct 07, 2004
Posts: 39
Hello Everybody,
I have gone in one interview & they ask me one basik question that
as followes
see first example :

class Test
{
void meth()
{
System.out.println( "You r in Test" );
}
}

class Test1 extends Test
{
void meth()
{
System.out.println(" Now u r in Test1" );
}
}

class DTest
{
public static void main( String str[] )
{
Test1 t1 = new Test1();
t1.meth() //here definatly Test1's method is call
// now how should here i call Test's Method?
// Please give me solution.
}
}


Here how should i call Test's method without using referance
& also the function of both should be same means (return type & parameters both should same)

Thanking in advance if u solve this
Barry Higgins
Ranch Hand

Joined: Jun 05, 2003
Posts: 89
I'm sure there is a much better way out of this problem but here's the only way I can see. Use reflection.



I'd be very interested to see if anyone has a better way of doing this!
Barry
Jeroen Wenting
Ranch Hand

Joined: Oct 12, 2000
Posts: 5093
if someone wrote such language in a piece of code during an interview I was conducting I'd decide there and then to not hire them no matter how good the code was...

Use proper English (or your native language at home) and not SMS shorthand. We don't charge by the second nor by the byte so there's no reason except to show ignorance and/or laziness.


42
Mike Gershman
Ranch Hand

Joined: Mar 13, 2004
Posts: 1272
the function of both should be same means (return type & parameters both should same)

What on earth does that mean?


Mike Gershman
SCJP 1.4, SCWCD in process
Joel McNary
Bartender

Joined: Aug 20, 2001
Posts: 1821

Originally posted by Barry Higgins:


The problem here is that you are not calling the method on the t1 object; you are creating a new instance of the Superclass (which you may or may not be able to do, depending on whether or not the super class is abstract). For all you did, you might as well just do:


The answer is: short of re-rwiting Test and Test1, you can't. The designers of the classes say that "meth" does such-and-such, and you can't change it. Imagine:


If you have a Monotreme (a Platypus or an Echidna), and you issue the command "giveBirth", it will lay eggs. Short of re-writing the classes (sort of genetic engineering), you can't make a Monotreme give birth to live young -- even though other Mammals can. That's just not how Monotremes work.


Piscis Babelis est parvus, flavus, et hiridicus, et est probabiliter insolitissima raritas in toto mundo.
mohan gavande
Ranch Hand

Joined: Oct 07, 2004
Posts: 39
Hi Everybody,
Thanks to reply me.
if there is any other way to call Test's (Super's) method without using reflection?
Also actually that code is just an example for understanding how would
call Super's meth() in subclass's instance

waiting for reply....


Best part of Programming is that it can be do by other way.
Barry Higgins
Ranch Hand

Joined: Jun 05, 2003
Posts: 89
The problem here is that you are not calling the method on the t1 object; you are creating a new instance of the Superclass (which you may or may not be able to do, depending on whether or not the super class is abstract). For all you did, you might as well just do:



That is true but if for some reason (unlikely in fairness) the caller did not know the name of the superclass they would be forced to use this technique. This was as far as I gathered the point of the exercise.

With regard to the question on there being any other way to tackle this problem I would agree with Joel and say that you can't.
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
What does "without reference" mean in the original post? Does it make super.meth() incorrect?


A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
Mike Gershman
Ranch Hand

Joined: Mar 13, 2004
Posts: 1272
Every call to a method requires an explicit or implied (this) reference.

The problem with super.meth() is that it will only invoke Test.meth() from within Test1 and DTest has no hierarchical relationship to Test or Test1.
You could do "class DTest extends Test" because the problem statement prohibited changing the method signatures, not the class definitions.
[ November 26, 2004: Message edited by: Mike Gershman ]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: overriding in inheritance (just try & reply)