• Post Reply Bookmark Topic Watch Topic
  • New Topic

Parent class method without super  RSS feed

 
Lakshaya Sharma
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is there any possible way in this universe that we can call the Parent class overridden method without using super keyword?
 
Campbell Ritchie
Marshal
Posts: 55760
163
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch

Don't say parent; say superclass.
You can only use super.foo() in instances of the subclass. From anywhere else you cannot even say super.foo(). Casting to the superclass won't do that. So, the short answer is no.
 
Lakshaya Sharma
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the help
 
Darryl Burke
Bartender
Posts: 5167
11
Java Netbeans IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How about with a copy constructor in the superclass?
 
Winston Gutkowski
Bartender
Posts: 10573
65
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Darryl Burke wrote:How about with a copy constructor in the superclass?

Don't confuse the poor lad ... and anyway, should that subclass of yours be called 'Double'?

Oh, and welcome to Java Ranch, Lakshaya.

Winston
 
Campbell Ritchie
Marshal
Posts: 55760
163
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What difference is there from this?
return super.toString() + ...;

No, I think Darryl's solution has two drawbacks. One is that you are not using a subclass object but a superclass object, so I don't think that counts. Another is that I doubt the arithmetic will print the correct answer to “four and four makes







forty‑four”
 
Darryl Burke
Bartender
Posts: 5167
11
Java Netbeans IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I would argue that it fulfils the requirement.
Lakshaya Sharma wrote:.. call the Parent class overridden method without using super keyword?
 
Campbell Ritchie
Marshal
Posts: 55760
163
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
All right, I'll give you that one then.
 
Sachin Tripathi
Ranch Hand
Posts: 368
3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
By using object of Super always the overrided method in Super class gets invoked ,what is the use of copy constructor in it
 
Campbell Ritchie
Marshal
Posts: 55760
163
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It gets you out of using the super keyword.
 
Sachin Tripathi
Ranch Hand
Posts: 368
3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't think so.If you are talking in polymorphic sense:
object of subclass is passed as argument to copy constructor,it assigns the same value to fields of Super as the values for Sub
But if we call the method toString() by the reference variable s
Then it would have been doing the polymorphic way

While by calling toString() by reference variable of object of Super class,would be as simply calling a method of a Particular class by the reference variable of the same class object
 
Mike. J. Thompson
Bartender
Posts: 689
17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There are two variables called 's' in the code you're quoting. If you are talking about the one in the twoString method of Sub then no, calling toString on that s would call the method in Super.
 
Sachin Tripathi
Ranch Hand
Posts: 368
3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No I am talking about s in the constructor of Super class which has parameter as a variable of Super type
 
Mike. J. Thompson
Bartender
Posts: 689
17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sachin Tripathi wrote:No I am talking about s in the constructor of Super class which has parameter as a variable of Super type


I had missed that one. That makes 3 variables called s!

The point of the copy constructor in Daryl's code is to turn an instance of Sub into an instance of Super. Once you have an instance of Super then no method is overriding toString(), the Super.toString version will be called.

This isn't something that we're advising should be done, but Daryl was just showing that there is a way to call the superclass version of a method without using the super keyword.
 
Sachin Tripathi
Ranch Hand
Posts: 368
3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yeah I understand this,I tried to write the same thing in reply to Campbell's post
 
Darryl Burke
Bartender
Posts: 5167
11
Java Netbeans IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mike. J. Thompson wrote:This isn't something that we're advising should be done

That's putting it too mildly. This is something that should never be done!
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!