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

reg overriding

swapna hyderabad
Greenhorn

Joined: Nov 20, 2008
Posts: 20
I have a super/sub class as below



the 1 line will call the sub class method but I want it should call only super class method with out creating object of super class........

Please any suggestions.....solution.....help:-)
[ November 20, 2008: Message edited by: Martijn Verburg ]
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 19073
    
  40

the 1 line wiill call the sub class method but i want it shoud call only super class method with out creating object of super class........


It is *not* creating object of super class -- it is the sub class that is being created. Remember a sub class IS-A super class. It can be used where the super class is used.

And no, you can't decide that the sub class is no longer a sub class. The overridden version will be called because it IS-A sub class.

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
swapna hyderabad
Greenhorn

Joined: Nov 20, 2008
Posts: 20
My question is ::

super s=new sub();

s.dispaly();//1

This line 1 shoud call the super cls method only...means i want to be excute super cls method implementaion..is it possible or not?
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 40052
    
  28
You can call super.display() but only from inside a subclass object (ie not static).
You cannot write "super s"; that will always throw a compiler error because you have the keyword super out of its correct context.
swapna hyderabad
Greenhorn

Joined: Nov 20, 2008
Posts: 20
You can call super.display() but only from inside a subclass object (ie not static).


thanks for reply.....
but i want only it shoud execute super cls method,i.e i dont want to excute sub class implementation at all......any solution?
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 40052
    
  28
You can only call the superclass implementation from inside the subclass. If you want the superclass implementation from outside that, you have to make sure your runtime type of the object is a superclass type. Two possible solutions I can think of:What you want to learn is object-oriented programming, not a particular language.
Using a superclass method which has been overridden in the object you are using breaches the customs of inheritance and polymorphism; it ceases to be true object-oriented programming.
swapna hyderabad
Greenhorn

Joined: Nov 20, 2008
Posts: 20
Thanks...Campbell ...you solved my problem........THANKS AGAIN
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 40052
    
  28
You're welcome
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: reg overriding