File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

overriding and marker interface questions

 
Praveen Sharma
Ranch Hand
Posts: 129
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Pie
Posts: 15150
31
Android IntelliJ IDE Java Scala Spring
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Praveen Sharma
Ranch Hand
Posts: 129
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 607
Firefox Browser Java Spring
  • 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic