9 Replies - 409 Views - Last Post: Yesterday, 02:37 AM Rate Topic: -----

#1 idlewild   User is online

  • New D.I.C Head

Reputation: 3
  • Posts: 35
  • Joined: 19-June 20

The methods of an Object

Posted 28 June 2020 - 12:25 AM

Hello,

When I create an object as such

Object obj=new Object();

there are only 9 methods available, it shows in Eclipse

but in the books they say there are eleven. ( wait X 3, notify, notifyAll, getClass, equals, toString, hashCode, finalize, clone )

finalize() and clone() methods do not exist for the variable obj

obj only has wait X 3, notify, notifyAll, getClass, equals, toString, hashCode methods available to it.

why can not I finalize() or clone a regular object Object ?

Thank you !

Is This A Good Question/Topic? 0
  • +

Replies To: The methods of an Object

#2 g00se   User is offline

  • D.I.C Lover
  • member icon

Reputation: 3698
  • View blog
  • Posts: 16,952
  • Joined: 20-September 08

Re: The methods of an Object

Posted 28 June 2020 - 04:33 AM

Quote

why can not I finalize() or clone a regular object Object ?

Because those are protected methods. You do that in Object subclasses
Was This Post Helpful? 1
  • +
  • -

#3 cfoley   User is offline

  • Cabbage
  • member icon

Reputation: 2410
  • View blog
  • Posts: 5,050
  • Joined: 11-December 07

Re: The methods of an Object

Posted 28 June 2020 - 04:46 AM

Eclipse probably shows the overloaded wait() method once instead of three times.

Clone() and finalize() methods do exist for all Java objects. However, they are protected methods which means they are only available within the same package (java.lang) and to subclasses. Critically, because Object is in a package startingjava., you cannot create classes in the same package which means you would have to create a subclass to access those methods. This is probably what your book means.

The documentation for clone() also states this, meaning that to take advantage of the built in clone() mechanism, you would have to also implement Cloneable.

Quote

if the class of this object does not implement the interface Cloneable, then a CloneNotSupportedException is thrown.


You can find more information in the documentation.
https://docs.oracle....ang/Object.html
Was This Post Helpful? 1
  • +
  • -

#4 idlewild   User is online

  • New D.I.C Head

Reputation: 3
  • Posts: 35
  • Joined: 19-June 20

Re: The methods of an Object

Posted 28 June 2020 - 08:41 AM

Thank you all for your responses

so i understand i will have to override(?) the clone() method by implementing the Cloneable interface - of the object created.

but how does one finalize the following object?
Object obj=new Object();

Was This Post Helpful? 0
  • +
  • -

#5 idlewild   User is online

  • New D.I.C Head

Reputation: 3
  • Posts: 35
  • Joined: 19-June 20

Re: The methods of an Object

Posted 01 July 2020 - 01:05 AM

sorry to bump but it stays a Mystery to me
Was This Post Helpful? 0
  • +
  • -

#6 g00se   User is offline

  • D.I.C Lover
  • member icon

Reputation: 3698
  • View blog
  • Posts: 16,952
  • Joined: 20-September 08

Re: The methods of an Object

Posted 01 July 2020 - 04:42 AM

You wouldn't want to do that - see https://docs.oracle....html#finalize()

But if you did it, again you'd have to override it in a subclass
Was This Post Helpful? 1
  • +
  • -

#7 idlewild   User is online

  • New D.I.C Head

Reputation: 3
  • Posts: 35
  • Joined: 19-June 20

Re: The methods of an Object

Posted 02 July 2020 - 07:08 PM

View Postg00se, on 01 July 2020 - 04:42 AM, said:

You wouldn't want to do that - see https://docs.oracle....html#finalize()

But if you did it, again you'd have to override it in a subclass

isn't it strange that you cannot do clone and finalize on the mother of all Objects?
Was This Post Helpful? 0
  • +
  • -

#8 ndc85430   User is offline

  • I think you'll find it's "Dr"
  • member icon

Reputation: 1027
  • View blog
  • Posts: 3,951
  • Joined: 13-June 14

Re: The methods of an Object

Posted 02 July 2020 - 09:18 PM

It's more strange that you'd want to use Object at all rather than a more specific one. Also, why are you wanting to call things like finalize anyway? Most of the time you're not going to need to.
Was This Post Helpful? 1
  • +
  • -

#9 idlewild   User is online

  • New D.I.C Head

Reputation: 3
  • Posts: 35
  • Joined: 19-June 20

Re: The methods of an Object

Posted 02 July 2020 - 11:55 PM

View Postndc85430, on 02 July 2020 - 09:18 PM, said:

It's more strange that you'd want to use Object at all rather than a more specific one. Also, why are you wanting to call things like finalize anyway? Most of the time you're not going to need to.

thank you for your response and one last question regarding the topic

what if

Object o=new String("abc");


and I'd wanna finalize the Object o before a downcast?

Thank you !
Was This Post Helpful? 0
  • +
  • -

#10 g00se   User is offline

  • D.I.C Lover
  • member icon

Reputation: 3698
  • View blog
  • Posts: 16,952
  • Joined: 20-September 08

Re: The methods of an Object

Posted Yesterday, 02:37 AM

I've already posted a link to the API documentation for Object.finalize - please read it. The point is, you don't call or implement that method anymore. It was a design dead end
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1