File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes Is inheritance is necessary to override? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Is inheritance is necessary to override?" Watch "Is inheritance is necessary to override?" New topic
Author

Is inheritance is necessary to override?

Anu satya
Ranch Hand

Joined: Mar 17, 2005
Posts: 146
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?

With Regards,
Anu
Seetharaman Venkatasamy
Ranch Hand

Joined: Jan 28, 2008
Posts: 5575

you cant declare a top level class as protected. only default
(package-private) and public is allowed

hth
Nick Puketza
Greenhorn

Joined: Apr 01, 2010
Posts: 16
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

Joined: Mar 17, 2005
Posts: 146
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

Joined: Apr 01, 2010
Posts: 16
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

Joined: Oct 13, 2005
Posts: 36579
    
  16
Remember only instance methods can be overridden. Not static methods, nor fields.
Anu satya
Ranch Hand

Joined: Mar 17, 2005
Posts: 146


Good luck, Anu!
- Nick



Thank you very much Nick for your answers and help....
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Is inheritance is necessary to override?
 
Similar Threads
access modifier question
Urget Plzzzzz Help - Lot of confusion.
How many types of inheritance is supported by Java?
protected
Question about packages