• Post Reply Bookmark Topic Watch Topic
  • New Topic

can method overriding be done from the same class?  RSS feed

 
rahul aditya
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
can method overriding be done from the same class??
 
Mohamed Sanaulla
Bartender
Posts: 3185
34
Google App Engine Java Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes provided the extending class is an Inner class (ie class defined with in the another class)
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mohamed Sanaulla wrote:Yes provided the extending class is an Inner class (ie class defined with in the another class)


But then the override is coming from the inner class. The fact that this happens to be contained inside the parent class is just an ugly complication that should probably never arise in real code.
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
rahul aditya wrote:can method overriding be done from the same class??


Why would you want to?

Keep in mind that the definition of overriding and the whole point of overriding revolves around the fact that a subclass is providing different behavior from its parent class.
 
Mohamed Sanaulla
Bartender
Posts: 3185
34
Google App Engine Java Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jeff Verdegan wrote:
Mohamed Sanaulla wrote:Yes provided the extending class is an Inner class (ie class defined with in the another class)


But then the override is coming from the inner class. The fact that this happens to be contained inside the parent class is just an ugly complication that should probably never arise in real code.


Ha ha Exactly and for me this looks like an interview question. I would never do that though, even if I did I would be caught in the review and asked to make a change.
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mohamed Sanaulla wrote:I would never do that though, even if I did I would be caught in the review and asked to make a change.

Hmmm. Oddly enough, I can see many cases where I might want it; particularly if I'd like to hand a client a "doctored" version of an existing class. A private nested class strikes me as the perfect way to do it (and used, I believe, by quite a lot of classes in the Java Collections Framework).

Winston
 
Mohamed Sanaulla
Bartender
Posts: 3185
34
Google App Engine Java Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Winston Gutkowski wrote:
Mohamed Sanaulla wrote:I would never do that though, even if I did I would be caught in the review and asked to make a change.

Hmmm. Oddly enough, I can see many cases where I might want it; particularly if I'd like to hand a client a "doctored" version of an existing class. A private nested class strikes me as the perfect way to do it (and used, I believe, by quite a lot of classes in the Java Collections Framework).

Winston

Agree, I have been using Anonymous Inner classes and found it to be useful. Using it to cover the absence of the closures in Java. But if the use of the nested classes is obscure then it would catch the eyes of the reviewers. Specifically in codebase which gets handled by lot of people down the years, so any unclear usage would simply increase the time required to understand the code.*

*talking from my minimal experience.
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Winston Gutkowski wrote:
Mohamed Sanaulla wrote:I would never do that though, even if I did I would be caught in the review and asked to make a change.

Hmmm. Oddly enough, I can see many cases where I might want it; particularly if I'd like to hand a client a "doctored" version of an existing class. A private nested class strikes me as the perfect way to do it


Sorry, I don't see the value in that.

(and used, I believe, by quite a lot of classes in the Java Collections Framework).


Do you have an example? I can't recall ever having seen it.
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mohamed Sanaulla wrote:
Agree, I have been using Anonymous Inner classes and found it to be useful. Using it to cover the absence of the closures in Java.


Sure, but anonymous inner class that's a subclass of the enclosing class? Maybe I'm just missing something or don't have a good imagination, but I can't envision a situation where that would make sense.
 
Mohamed Sanaulla
Bartender
Posts: 3185
34
Google App Engine Java Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jeff Verdegan wrote:
Mohamed Sanaulla wrote:
Agree, I have been using Anonymous Inner classes and found it to be useful. Using it to cover the absence of the closures in Java.


Sure, but anonymous inner class that's a subclass of the enclosing class? Maybe I'm just missing something or don't have a good imagination, but I can't envision a situation where that would make sense.

Not specifically Anonymous Inner class of the enclosing class, but using Anonymous Inner classes in General.
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mohamed Sanaulla wrote:
Jeff Verdegan wrote:
Mohamed Sanaulla wrote:
Agree, I have been using Anonymous Inner classes and found it to be useful. Using it to cover the absence of the closures in Java.


Sure, but anonymous inner class that's a subclass of the enclosing class? Maybe I'm just missing something or don't have a good imagination, but I can't envision a situation where that would make sense.

Not specifically Anonymous Inner class of the enclosing class, but using Anonymous Inner classes in General.


Ah, okay. I thought we were still talking about the OP's question of overriding a method from inside the same class where it's already implemented.
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jeff Verdegan wrote:Do you have an example? I can't recall ever having seen it.

Off the top of my head, one example is for returning sublists/submaps; they're also used for things like key and entry Sets. Collections also has a bunch of private static classes for its 'unmodifiable' and 'synchronized' Lists/Maps.

From subsequent posts, I'm not sure whether that's what you were referring to though; maybe we're at cross-purposes.

Winston
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Winston Gutkowski wrote:
Jeff Verdegan wrote:Do you have an example? I can't recall ever having seen it.

Off the top of my head, one example is for returning sublists/submaps; they're also used for things like key and entry Sets. Collections also has a bunch of private static classes for its 'unmodifiable' and 'synchronized' Lists/Maps.


Are you talking about nested classes that extend the class that contains them? AbstractList.java has a couple of package-private top-level classes for sublists defined in the same .java file, but no nested subclasses extending their enclosing class. As for Collections, that class isn't even instantiable, so the question of overriding is moot.

From subsequent posts, I'm not sure whether that's what you were referring to though; maybe we're at cross-purposes.


I think we may have been.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!