This week's book giveaway is in the Performance forum.
We're giving away four copies of The Java Performance Companion and have Charlie Hunt, Monica Beckwith, Poonam Parhar, & Bengt Rutisson on-line!
See this thread for details.
Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Is inheritance is necessary to override?

 
Anu satya
Ranch Hand
Posts: 146
  • Mark post as helpful
  • send pies
  • 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?
 
Seetharaman Venkatasamy
Ranch Hand
Posts: 5575
Eclipse IDE Java Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
you cant declare a top level class as protected. only default
(package-private) and public is allowed

hth
 
Nick Puketza
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • 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
  • 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
  • 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






 
Campbell Ritchie
Sheriff
Pie
Posts: 49396
62
  • Mark post as helpful
  • send pies
  • 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
  • Quote
  • Report post to moderator


Good luck, Anu!
- Nick



Thank you very much Nick for your answers and help....
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic