• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Jeanne Boyarsky
  • Ron McLeod
  • Tim Cooke
Sheriffs:
  • Devaka Cooray
  • paul wheaton
  • Mark Herschberg
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Frits Walraven
  • Jj Roberts
Bartenders:
  • Carey Brown
  • salvin francis
  • Piet Souris

Interesting Problem...

 
Ranch Hand
Posts: 61
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi All.
I have encountered an interesting problem while writting some java code and would like your help to resolve the same.

The above code results in infinite loop..
output is something like
Client default testfunc
Client reg testfunc
Super default testfunc
Client reg testfunc
Super default testfunc
Client reg testfunc
Super default testfunc
...
..
.
Reason is that in Super's default testfunc instead of executing Super's reg testfunc it executes client's testfunc. How do I fix it ?
[ January 09, 2002: Message edited by: Gaurav Mantro ]
[ January 09, 2002: Message edited by: Gaurav Mantro ]
 
"The Hood"
Posts: 8521
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What is compound(1) ???
 
Gaurav Mantro
Ranch Hand
Posts: 61
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I noticed the error and tried to fix it by editing my post but I was late.
However I have edited my post above and it contains the correct call now which is
testfunc(1).
[ January 09, 2002: Message edited by: Gaurav Mantro ]
 
Ranch Hand
Posts: 5040
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Cindy looks like a typo from the output.....
It should be testfunc(1)...
In which case the program is correct. Works by design.
The clients' method is called since the runtime object is of class Client, which over-rides the Super method.
regds.
- satya
 
Gaurav Mantro
Ranch Hand
Posts: 61
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Madhav,
The program is correct but I want to invoke testfunc(1) of Super and not Client in Super's testfunc method. How do I achieve the same.
What should be the call in Super's testfunc so that Super's testfunc(1) is invoked and not Client's testfunc(1)?
 
Ranch Hand
Posts: 1873
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
make testfunc() private in Super class
hth,
maulin
 
Maulin Vasavada
Ranch Hand
Posts: 1873
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
sorry,
i meant make testfunc(int) private and not the function w/o argument in Super class so it wont get overridden and from Super's testfunc() it will call Super's testfunc(int) always.
regards
maulin
 
Gaurav Mantro
Ranch Hand
Posts: 61
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi.
Changing the access modifier's is not a possible option in this case.
 
Madhav Lakkapragada
Ranch Hand
Posts: 5040
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

What should be the call in Super's testfunc so that Super's testfunc(1) is invoked and not Client's testfunc(1)?

To call the Supers' testfunc(1), you need to change the Client. When a sub-class overrides the behavior of a parent class, always the sub-class method behavior is called for an oblect of type sub-class. You should implement the (sub-class) Client method to achieve the proper behavior.
This is fundamental to the design of sub-class methods which override parent class methods.
- satya
 
Madhav Lakkapragada
Ranch Hand
Posts: 5040
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

i meant make testfunc(int) private and not the function w/o argument in Super class so it wont get overridden and from Super's testfunc() it will call Super's testfunc(int) always

WOW!!!
Understand the fudamentals here.
<<<<<<< deep breath>>>>>>>>>>
Okay.................maybe thats one way to do it.
Many time YOU DON'T HAVE THE AUTHORITY TO CHANGE THE SUPER CLASS JUST BECAUSE YOU NEED SOME SPECIFIC BEHAVIOR IN YOUR SUB CLASS.
- SATYA
ps:
yes I am upset abt this suggestion. Sorry.........
 
Gaurav Mantro
Ranch Hand
Posts: 61
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Madhav.
Thanks for explaining it in detail. Although what I was trying to get into it is to find out a workaround for the given problem using some kind of type cast or other trick.
For example when we make a call to super.XXX() we are able to call the base class's method and not the overridden one.
I understand that currently the overridden method will get called because 'this' in Super class's testfunc is Client object and not the Super object. Just trying to figure out a way and also to see if this language feature can led to design pitfall if not taken care of at design time.
Just trying to explore Java for maximum flexibility.
[ January 09, 2002: Message edited by: Gaurav Mantro ]
 
Gaurav Mantro
Ranch Hand
Posts: 61
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Maulin, Vasavada,
Sorry for not providing all the information upfront like changing the Super or Client class is not an option as far as method declaration and overriding is concerned.
Although the code inside methods of Super as well as Client can be changed. As Madhav pointed out changing Super class is not always an option but I am saying that we can change Super so that we can find a way to code super class methods to get a greater flexibility (maybe we are on our way for another design pattern ...)
Isn't it fun to do challenging things? Although sometimes we can solve them because of other limitations but its a great learning.
[ January 09, 2002: Message edited by: Gaurav Mantro ]
 
Madhav Lakkapragada
Ranch Hand
Posts: 5040
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

we are able to call the base class's method and not the overridden one.

Hope you agree with me if I say thats because we used the super keyword.

I understand that currently the overridden method will get called because 'this' in Super class's testfunc is Client object and not the Super object. Just trying to figure out a way and also to see if this language feature can led to design pitfall if not taken care of at design time


Good luck.
- satya
ps:
I like your example, but wouldn't agree with the suggestion to modify the Super class. For what its worth....
 
Ranch Hand
Posts: 2120
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello Gaurav
Just for the sake of knowledge. Try using RTTI to call another method instead of testfun(1) when the object, on which the method is called, is of the derived type. This method would contain the same code as testfun(int) in Super, but it wouldn't be overloaded.
Maybe not very useful. But you could treat diferently an object of type Client than ,say an derived class of it, for which no such RTTI stuff is provided.
I am not saying this must be done in a production program. Just compile and tell us if it works.
 
Every time you till, you lose 30% of your organic matter. But this tiny ad is durable:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic