• Post Reply Bookmark Topic Watch Topic
  • New Topic

Can we override method in same class?  RSS feed

 
Shiva Sankhar
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
public class A {

private void add(){ } // overriden method

public void add(){ } // overriding method
}

1. Can we override add() method?
 
Darryl Burke
Bartender
Posts: 5167
11
Java Netbeans IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What happened when you tried?
 
Ravishanker kumar
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is not possible.
Anyway, what you want to achieve by overriding in same class?
 
Shiva Sankhar
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I got this question in one of my interviews.I told them Overriding is not possible in same class.But Overloading is possible.
Is it right?

 
Seetharaman Venkatasamy
Ranch Hand
Posts: 5575
Eclipse IDE Java Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
what is the use of duplicate methods[redundancy] ?
 
Brij Garg
Ranch Hand
Posts: 234
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

I got this question in one of my interviews.I told them Overriding is not possible in same class.But Overloading is possible.
Is it right?

You are correct.
 
Juni Panda
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Even i too faced the same question in one of the job interview. He was not convinced with the answer "not possible". That is why I was searching over the net for the answer and found this thread.
Can any java expert answer this.
 
Campbell Ritchie
Marshal
Posts: 56545
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch

The answer is, it says so in the Java Language Specification. It says “not override-equivalent”. that is the important thing here; you mustn’t have two methods which are override-equivalent.
 
Randall Twede
Ranch Hand
Posts: 4696
8
Java Scala
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i saw a question about this the other day. the person wanted to know why he was getting a compiler error
 
Saral Saxena
Ranch Hand
Posts: 203
Eclipse IDE Java Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Juni Panda wrote:Even i too faced the same question in one of the job interview. He was not convinced with the answer "not possible". That is why I was searching over the net for the answer and found this thread.
Can any java expert answer this.


So wats the final answers..regarding...Can we override method in same class?..??
 
Paul Clapham
Sheriff
Posts: 22829
43
Eclipse IDE Firefox Browser MySQL Database
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, apparently there isn't going to be a final answer. Numerous people have already said "No" as the answer, but other people haven't read that, or don't understand it, or for whatever reason have disregarded it. At this point if you don't like that answer, it's your responsibility to explain why you don't like it, or why you don't understand it. Just re-asking the question as if you had never read any of the thread is pointless.
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Saral Saxena wrote:
Juni Panda wrote:Even i too faced the same question in one of the job interview. He was not convinced with the answer "not possible". That is why I was searching over the net for the answer and found this thread.
Can any java expert answer this.


So wats the final answers..regarding...Can we override method in same class?..??


What happened when you tried it?
 
Saral Saxena
Ranch Hand
Posts: 203
Eclipse IDE Java Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jeff Verdegan wrote:
Saral Saxena wrote:
Juni Panda wrote:Even i too faced the same question in one of the job interview. He was not convinced with the answer "not possible". That is why I was searching over the net for the answer and found this thread.
Can any java expert answer this.


So wats the final answers..regarding...Can we override method in same class?..??


What happened when you tried it?


I haven't tried this ever, since it is against the basic principles of overriding..!!
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Saral Saxena wrote:
Jeff Verdegan wrote:
Saral Saxena wrote:
Juni Panda wrote:Even i too faced the same question in one of the job interview. He was not convinced with the answer "not possible". That is why I was searching over the net for the answer and found this thread.
Can any java expert answer this.


So wats the final answers..regarding...Can we override method in same class?..??


What happened when you tried it?


I haven't tried this ever, since it is against the basic principles of overriding..!!


Well then, after reading over this thread closely and carefully, if you still don't know the answer to your question, then you should try it. And then if you still have further questions, post what you did, what you observed, and what specific questions you still have.
 
Juni Panda
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The answer is definitely NO.. When I was asked in the interview, I wondered what sort of question is this. But the person who took my interview was not satisfied with my answer that it is "not possible" in the same class, and will give duplicate error.
See Campbell Ritchie's update , where Java Language Specification link is there, that says,

An instance method m1, declared in class C, overrides another instance method m2, declared in class A iff all of the following are true: //////////// So it's clear that C should definitely be a subclass to override a method of A , Hence not possible in the same class.

-> C is a subclass of A.

-> The signature of m1 is a subsignature of the signature of m2.

Either:

m2 is public, protected, or declared with default access in the same package as C, or

m1 overrides a method m3 (m3 distinct from m1, m3 distinct from m2), such that m3 overrides m2.
 
Arunkumar Chinnadurai
Ranch Hand
Posts: 56
Android MyEclipse IDE Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all,

No , not possible to override same class methods,
we can override inherited class methods only,

ok tell me the answer for following quest ?

1) what is the use of this override[you are saying same class method override] ?
2) which scenario are you going to use this..?
 
Dinesh Sing
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No, overriding is not possible inside the same class. If done, the coompiler will show a duplicate method compilation error.
 
Arunkumar Chinnadurai
Ranch Hand
Posts: 56
Android MyEclipse IDE Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Saral Saxena wrote:
Juni Panda wrote:Even i too faced the same question in one of the job interview. He was not convinced with the answer "not possible". That is why I was searching over the net for the answer and found this thread.
Can any java expert answer this.


So wats the final answers..regarding...Can we override method in same class?..??



No, we cannot override methods in same class, because cannot able call methods,
 
Rinkit Shah
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Arunkumar Chinnadurai wrote:
Saral Saxena wrote:
Juni Panda wrote:Even i too faced the same question in one of the job interview. He was not convinced with the answer "not possible". That is why I was searching over the net for the answer and found this thread.
Can any java expert answer this.


So wats the final answers..regarding...Can we override method in same class?..??



No, we cannot override methods in same class, because cannot able call methods,


yes we cannot override method in same class because it really makes no sense in overriding where same functionality can be added in the previous method and moreover we won't be able to call the methods as Arunkumar said.
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Great.

We have 5 consecutive answers from 4 different posters all saying the same thing and all handing Saral the answer rather than letting him to a bit of research himself.

Folks, in the future, please ReadTheAnswers(⇐click) and LetThemDoTheirOwnHomework(⇐click) (or in this case, their own research).

Thanks!
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!