Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

inheritance for Object class

 
Manish Thapliyal
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please watch the following code, I have a query about it.

Basically there are two classes Test2 & Test3 in the two different packages.

package test1;
public class Test2
{protected int test=8;}


package test3;
import test1.*;
class Test3 extends test1.Test2
{
//static int test5=getTest();
static Test3 t=new Test3();
public static void main(String a[])
{
System.out.println(t.test);
t.toString();
}
}


Now class Test3 extend Test2.As Object is the root class then Test2 has java.lang.Object class as it's superclass.
So Test3 class indirectly inherits the Object Class.

No my query is that :
Object class has only two methods protected remaning are default(no access outside the package.)
Then is that means the class Test3 does not have access the other methods of Object class such as equals(),hashcode.
 
Marco Ehrentreich
best scout
Bartender
Posts: 1294
IntelliJ IDE Java Scala
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Manish,

the methods in class Object do NOT have default access! They are all public and protected (only clone and finalize)!

Perhaps you only looked at the "method summary" section in the API documentation. There are indeed no access modifiers but in the detailed method descriptions you will see that they're public

Marco
[ June 03, 2008: Message edited by: Marco Ehrentreich ]
 
Manish Thapliyal
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks marco for your reply
you were correct I have seen only the api method summary.

Actually i have reached to this question while searhing why the finallizer() & clone method are protected . why not public or private
Please explain.
 
Marco Ehrentreich
best scout
Bartender
Posts: 1294
IntelliJ IDE Java Scala
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The reason is very simple... The methods clone() and finalize() don't have a very useful default implementation in class Object so they are left protected.

If you need them you can override the methods in your subclass and by overriding you can even promote them to "public" access if you like.

Marco
 
Manish Thapliyal
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
AS I understood, finallize() is protected not public because minimum accesbility for overriding is the protected.

But why finnalize() is not private i am unable to understand?
 
Marco Ehrentreich
best scout
Bartender
Posts: 1294
IntelliJ IDE Java Scala
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You already gave the answer yourself

finalize() and clone() are not public because they are not very useful with their default behavior in class Object.

And they are protected and not private because they are expected to be overridden by subclasses!!! If they were private they couldn't be overridden, that's the point! As you said the minimum access modifier to allow overriding is "protected".

Marco
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic