• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

overriding in java

 
Ranch Hand
Posts: 87
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Team ,

I know the concept of overriding from super class but still I am not clear as why there is a need for overriding ,cann't we write the complete functionality in subclass only,

Please reply with any example,

Thanks,
Uju
 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

ujwwala tem wrote:
I know the concept of overriding from super class but still I am not clear as why there is a need for overriding ,cann't we write the complete functionality in subclass only,



What do you mean by in the subclass only? Do you mean in a brand new method, with new interface, and functionality?

One of the purposes of inheritance (which includes allowing you to create a subclass), is to create new functionality with the superclass' interface. Effectively allowing you to create something that IS-A superclass. This is so that you don't have to change any code that expects an instance of the superclass. Can you explain to us how to accomplish this by "write the complete functionality in subclass only"?

Henry
 
ujwwala tem
Ranch Hand
Posts: 87
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

Thank for the reply,

My query is like what is the need of overriding ,This has been asked to me in interview,

As in overriding,the method from superclass gets overriden so whet is the need of that,can n't we write the new method in subclass only,I know as it will add special features according to subclass but then interviewr told as better to write in subclass only..

Please explain with real time scenario,

Thanks

 
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Interview questions are different from normal questions. How did you answer it?
 
ujwwala tem
Ranch Hand
Posts: 87
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi ,
I answered as it will inherit the superclass feature and add its own functionality in subclass but still I m not clear as why to override

Thanks
 
Campbell Ritchie
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please explain more about what “inherit the superclass feature” means.
 
Sheriff
Posts: 17644
300
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Overriding methods is related to the object-oriented concepts of inheritance, which you already seem to know about, and polymorphism. Do you understand what polymorphism is about? Here's one way it has been explained. There are a number of other ways that polymorphism in Java has been explained, too.
 
ujwwala tem
Ranch Hand
Posts: 87
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Team,

So wht I understood is a overriding is based on he inheritance and polymorphysm so it is inappropriate to just define the method in subclass only instead we can override it a we want,

Thanks,
Uju
 
Henry Wong
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

ujwwala tem wrote:
I know the concept of overriding from super class but still I am not clear as why there is a need for overriding ,cann't we write the complete functionality in subclass only,



ujwwala tem wrote:
So wht I understood is a overriding is based on he inheritance and polymorphysm so it is inappropriate to just define the method in subclass only instead we can override it a we want,



So, you started this topic saying that overriding doesn't serve a purpose (why there is a need at all)? And now you are saying that you should always override (as it is inappropriate otherwise)? Which do you think is correct?

If I had to speculate on what the interviewer was thinking, I would say that he wanted your opinion, and the ability to back up those opinions. And quite frankly, if you answered with either one of those two options, it would probably come off badly. You need to understand what is inheritance (and polymorphism); why it is useful, and why it is not. In other words, the answer to this question should have been your view of the subject, and lead it to examples of your works; and discuss why you thought inheritance was appropriate in one case and not the other.

Henry
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic