• 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
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

Is inheritance is necessary to override?

 
Ranch Hand
Posts: 146
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
I had been to one interview. There i have been asked two questions.

1. Is inheritance is necessary to override?
2. Under what condition a class is made protected?

My answer was:
1. Inheritance is necessary to override.
2. When we need to restrict only a child class to access its super class's data, at time, it is a good idea to make a class protected.

But interviewers were not happy with my answers. I couldn't clear the interview.

Can anyone please help me where am i wrong and why?
 
Ranch Hand
Posts: 5575
Eclipse IDE Windows XP Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
you cant declare a top level class as protected. only default
(package-private) and public is allowed

hth
 
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Anu,

Regarding the first question, I have two quotations from the K&B
SCJP 6 Study Guide:

- "Instance methods can be overridden only if they are inherited by
the subclass."

- "If a method can't be inherited, you cannot override it. Remember that
overriding implies that you're reimplementing a method you inherited!"

So your answer to that question seems to be correct.

 
Anu satya
Ranch Hand
Posts: 146
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you Nick Puketza and Seetharaman.

According to Nick, i understand that Overriding and Inheritance are related to each other like try-catch block...so, we can't use either of the two. We have to use both. Am i correct? I am bit confussed.

Is it possible to override a constructor?

According to Seetharaman,

What if a class is not the top most class, under what circumstances we can make it protected? is my answer for this is correct? else is there any other answer with respect to design aspects?

Please help me....

One more request..... Nick, can you please send me the link to K&B SCJP 6 Study Guide ? I am planning to take up the exam.

Please
 
Nick Puketza
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Anu,

According to Nick, i understand that Overriding and Inheritance are related to each other like try-catch block...so, we can't use either of the two. We have to use both. Am i correct? I am bit confussed.



First, I think I might have confused you. If so, I'm sorry about that! You can use inheritance
without overriding. For example, you might create an application with two classes, a
superclass and a subclass, in which the subclass inherits all of the superclass methods,
and does not override any of them. (In the subclass you might declare several new
methods, too.)

However, I believe (based on my earlier post) that you cannot override a method unless
it is inherited.

Is it possible to override a constructor?



Constructors cannot be overridden.

One more request..... Nick, can you please send me the link to K&B SCJP 6 Study Guide ? I am planning to take up the exam.



Here is an Amazon link:

http://www.amazon.com/o/ASIN/0071591060/ca0cc-20


Good luck, Anu!
- Nick






 
Marshal
Posts: 75708
354
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Remember only instance methods can be overridden. Not static methods, nor fields.
 
Anu satya
Ranch Hand
Posts: 146
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator



Good luck, Anu!
- Nick



Thank you very much Nick for your answers and help....
 
I'm so happy! And I wish to make this tiny ad happy too:
Free, earth friendly heat - from the CodeRanch trailboss
https://www.kickstarter.com/projects/paulwheaton/free-heat
reply
    Bookmark Topic Watch Topic
  • New Topic