• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

overriding in inheritance (just try & reply)

 
mohan gavande
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 89
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 5093
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Mike Gershman
Ranch Hand
Posts: 1272
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
the function of both should be same means (return type & parameters both should same)

What on earth does that mean?
 
Joel McNary
Bartender
Posts: 1840
Eclipse IDE Java Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
mohan gavande
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 89
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What does "without reference" mean in the original post? Does it make super.meth() incorrect?
 
Mike Gershman
Ranch Hand
Posts: 1272
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic