wood burning stoves*
The moose likes Java in General and the fly likes overriding and marker interface questions Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "overriding and marker interface questions" Watch "overriding and marker interface questions" New topic
Author

overriding and marker interface questions

Praveen Sharma
Ranch Hand

Joined: Jul 31, 2008
Posts: 129
Recently I attended an interview and could not answer these two questions satisfactorily:

1. The access level in the overriding method can't be more restrictive than the overridden method's. Why ?
2. A real time example of marker interface?

I would be glad if someone could answer these so that I can build my knowledge.


SCJP 80% SCWCD 89%
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14157
    
  19

What do you think yourself the answers to these questions would be? Some hints to get you started:

Question 1: Suppose that you have a superclass with a public method, and a subclass in which that method is overridden, but which is protected. What could go wrong? Could code that shouldn't be able to call the protected method suddenly call it? How?

Question 2: What is a marker interface? Are there examples of marker interfaces in the standard Java library? What are they used for?


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
Praveen Sharma
Ranch Hand

Joined: Jul 31, 2008
Posts: 129
1) Finding it tough to think

2) I know of Cloneable and Serializable. These give information to the JVM to treat the classes (marked with the marker interface) in a certain way. But the interviewer was more interested in some real time example where I can use marker interface.
Saifuddin Merchant
Ranch Hand

Joined: Feb 08, 2009
Posts: 605

The OP seems to have given this some thought, so I going to give my thoughts on the question,

OP --> 1. The access level in the overriding method can't be more restrictive than the overridden method's. Why ?
Jesper --> Question 1: Suppose that you have a superclass with a public method, and a subclass in which that method is overridden, but which is protected. What could go wrong? Could code that shouldn't be able to call the protected method suddenly call it? How?

Jesper has almost answered your question with his hint.

However due to polymorphism the method A() in the Child class which is protected gets called. This should not be allowed.

Got to rush, answer to Question 2 soon

Cheers - Sam.
Twisters - The new age Java Quiz || My Blog
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: overriding and marker interface questions