LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 11-10-2007, 09:20 AM   #1
manolakis
Member
 
Registered: Nov 2006
Distribution: xubuntu
Posts: 464

Rep: Reputation: 37
protected clone Java


Hi all!!!
I am facing a problem with a the java.lang.object class.
I am actually trying to pass as a parameter in a method a Class and then clone() that object/class.
An example could be:
Quote:
public AObject doSmth(AObject obj)
{
...
AObject orig = (AObject) obj.clone();
return orig.clone();
}

public class AObject extends Object{}
You can notice that AObject class extends Object class. My problem is the following error:
Quote:
clone() has protected access in java.lang.Object
AObject orig = (AObject) obj.clone();
Any ideas how to overcome this problem?
Thanks
 
Old 11-10-2007, 11:20 AM   #2
Alien_Hominid
Senior Member
 
Registered: Oct 2005
Location: Lithuania
Distribution: Hybrid
Posts: 2,247

Rep: Reputation: 53
Your class should implement cloneable interface.
 
Old 11-10-2007, 11:44 AM   #3
ilikejam
Senior Member
 
Registered: Aug 2003
Location: Glasgow
Distribution: Fedora / Solaris
Posts: 3,109

Rep: Reputation: 97
Hi.

Do you get the error at runtime, or compiletime? The following compiles OK for me...
Code:
public class AObject extends Object{
    
    public AObject doSmth(AObject obj) throws CloneNotSupportedException
    {
	AObject orig = (AObject)obj.clone();
	return orig;
    }
    
}
Dave

Last edited by ilikejam; 11-10-2007 at 11:45 AM.
 
Old 11-10-2007, 12:33 PM   #4
jay73
LQ Guru
 
Registered: Nov 2006
Location: Belgium
Distribution: Ubuntu 11.04, Debian testing
Posts: 5,019

Rep: Reputation: 133Reputation: 133
Put this in your AObject class

public Object clone() throws CloneNotSupportedException(){return super.clone();}

You also need to make sure that it implements Cloneable.
 
Old 11-10-2007, 01:19 PM   #5
Alien_Hominid
Senior Member
 
Registered: Oct 2005
Location: Lithuania
Distribution: Hybrid
Posts: 2,247

Rep: Reputation: 53
Quote:
Originally Posted by ilikejam View Post
Hi.

Do you get the error at runtime, or compiletime? The following compiles OK for me...
Code:
public class AObject extends Object{
    
    public AObject doSmth(AObject obj) throws CloneNotSupportedException
    {
	AObject orig = (AObject)obj.clone();
	return orig;
    }
    
}
Dave
You'll get CloneNotSupportedException, when you try to clone.
 
Old 11-10-2007, 01:36 PM   #6
manolakis
Member
 
Registered: Nov 2006
Distribution: xubuntu
Posts: 464

Original Poster
Rep: Reputation: 37
Hi again and thanks everyone for your replies
The case is that AObject it is not my Class. I am trying to use a Java package. That was an example of my code. AObject is a class in that package that I want to return from a method. I tried to implement Cloneable in my Driver Class of the package but again I got the same error.
Any other ideas?

Thanks
 
Old 11-10-2007, 03:42 PM   #7
manolakis
Member
 
Registered: Nov 2006
Distribution: xubuntu
Posts: 464

Original Poster
Rep: Reputation: 37
Will it easy for anyone to post how to use getClass() to compare two objects? Thanks
 
Old 11-10-2007, 04:09 PM   #8
jay73
LQ Guru
 
Registered: Nov 2006
Location: Belgium
Distribution: Ubuntu 11.04, Debian testing
Posts: 5,019

Rep: Reputation: 133Reputation: 133
class A{}

public class CheckClass {

public static void main (String [] args){

Long lng = new Long(10);
A a = new A();

System.out.println(lng.getClass()==Long.class);
System.out.println(a.getClass()==A.class);

}

}

Both return true.
 
Old 11-10-2007, 05:48 PM   #9
manolakis
Member
 
Registered: Nov 2006
Distribution: xubuntu
Posts: 464

Original Poster
Rep: Reputation: 37
Many thanks
 
Old 11-11-2007, 04:30 AM   #10
Alien_Hominid
Senior Member
 
Registered: Oct 2005
Location: Lithuania
Distribution: Hybrid
Posts: 2,247

Rep: Reputation: 53
Quote:
Originally Posted by manolakis View Post
Hi again and thanks everyone for your replies
The case is that AObject it is not my Class. I am trying to use a Java package. That was an example of my code. AObject is a class in that package that I want to return from a method. I tried to implement Cloneable in my Driver Class of the package but again I got the same error.
Any other ideas?

Thanks
There are several problems:

1) You can't access protected method from separate class => solution: extend AObject class and override clone() method, get AObject clone using super.clone()

2) AObject should implement cloneable interface. If it doesn't, you can't clone it.
 
Old 12-20-2007, 08:04 AM   #11
manolakis
Member
 
Registered: Nov 2006
Distribution: xubuntu
Posts: 464

Original Poster
Rep: Reputation: 37
Getting the subclasses of the class

Hi there

Does anybody know how to get the subclasses of a class, in particular in the following example:
Quote:
public class B extends A{...}

public class A{...}

public class C
{
public static void main(String [] args)
{
A a = new A();
Class b_class = a.getItsSubClass();
}
}
I would be really happy if anyone knows how to implement a method like getItsSubClass().

Thanks for your time
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Quick Java Questioin: protected access sleight Programming 2 02-18-2007 05:55 AM
protected wmas time112852 Linux - Software 4 12-09-2005 04:42 PM
Protected PDF Schopy Linux - Software 3 04-13-2005 06:03 PM
protected folders tommytomato Linux - Security 6 09-21-2004 11:23 PM
ripping a protected cd mexifries Linux - Software 7 08-28-2003 03:14 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 12:48 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration