Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

reg overriding

 
swapna hyderabad
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Marshal
Pie
Posts: 20902
76
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
swapna hyderabad
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 48445
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 48445
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks...Campbell ...you solved my problem........THANKS AGAIN
 
Campbell Ritchie
Sheriff
Posts: 48445
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're welcome
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic