0

As I have learned, Object class is a top level class and it is a parent of all classes. If we do not know a type of class at compile time, we can assign it to a object of Object class.

I am trying to clone a object to object of a Object class. Also trying to get the object from HashMap which is already instantiated. I am having problem in doing this. Can you figure out it and explain the right ways of doing it? I have commented in which lines I get compile time error. My main doubts are:

  • If a parent class' object can be used for cloning, then it must work with Object class too as it is top level class.

  • And how to access object from map and use it for method call and cloning.

Code:

import java.util.HashMap;
import java.util.Map;


class Sample {
    public void call(){

}

}
class Question extends Sample implements Cloneable {
@Override
    public void call(){
    System.out.println("hello");
}
@Override
    public Object clone()throws CloneNotSupportedException{  
    return super.clone();  
    }  

public static void main(String args[]) throws CloneNotSupportedException{
    Map<Character,Object> map=new HashMap();
    Question s=new Question();
    Sample q=new Question();
    Sample cl=(Question)s.clone();
    Object ob=(Question)s.clone();//no compile time error
    map.put('o',s);
    s.call();//hello
    q.call();//hello
    cl.call();/hello
    ob.call();//Compile time error: cannot find  symbol call
    map.get('o').call();//Compile time error: cannot find  symbol call
    Object obj=(Question) (map.get('o')).clone();// Compile time error: clone has protected access in Object

}
}
6
  • 1
    possible duplicate of Java: help understanding the use of interfaces as a data type? [it's the same with subclasses] Commented Aug 22, 2015 at 11:28
  • just a piece of advice. The first character of a classname should always be an uppercase (convention): i.e. sample should be Sample Commented Aug 22, 2015 at 11:32
  • @bvdb Thanks . Updated. ! Commented Aug 22, 2015 at 11:35
  • @RC. Sorry I did not understand from the other thread. If you have time, can u elaborate a bit with respect to this program? Commented Aug 22, 2015 at 11:37
  • Don't use quote formatting for text that isn't quoted. Commented Aug 22, 2015 at 12:14

1 Answer 1

3

The following line can be simplified

Object ob=(Question)s.clone();//no compile time error
// the cast is unnecessary:
Object ob= s.clone();

But like you said, the ob will still contain a Question object. The problem is that once you start using this ob reference, java just knows it contains a value of Object, or a subclass of it. So for java ob could be a Number or a String or an Airplane.

Object ob = "airplane";

Once it gets to the line ob.call() it refuses. Because it's not sure that the ob object has a call method. For example, if it was a String or a Number it would be impossible.

For this reason you have to perform a simple cast first:

((Question)ob).call(); 
// or 
((Sample)ob).call();

Just let java know that it's an object with a call method.

The map#call issue has the same reasoning:

map.get('o').call();
//could be any of these
((Sample)map.get('o')).call();
((Question)map.get('o')).call();

But the last problem is more tricky. Actually a lot gets clear when you split up your statement in multiple lines:

Object obj=(Question) (map.get('o')).clone();
// would be the same like writing:
Object value = map.get('o');
Object value2 = value.clone();
Object obj = (Question) value2; // The (Question) cast is actually unnecessary.

The problem is in the value.clone() step. It is true that the Object class has a clone method, but it's marked as protected whereas the clone methods in your Question and Sample classes are public. So in short Object#clone is not accessible ; Sample#clone and Question#clone are accessible.

// so you want this:
Object value = map.get('o');
Object value2 = ((Question)value).clone(); // add a cast here
Object obj = value2;

If you prefer to do it all in 1 line:

Object obj=((Question) (map.get('o'))).clone();
Sign up to request clarification or add additional context in comments.

4 Comments

Wow, that was perfect.can you explain me about accessing the objects from HashMap?
Sorry actually (Question)ob.call(); too throws a compile time error.
I stand corrected, added braces ((Question)ob).call()
((Question)ob).call() is working well. thanks a ton (Y)

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.