File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes Creating a class with name Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Reply Bookmark "Creating a class with name "Object"" Watch "Creating a class with name "Object"" New topic
Author

Creating a class with name "Object"

karthik Suryanarayanan
Ranch Hand

Joined: Oct 07, 2009
Posts: 94

I have tried Creating a class with name Object. I have created a constructor like this.


public class Object {

Object()
{
System.out.println("hello this is Abc");
}
}

1.)Now i write one class like this.

public class AbcTest {

AbcTest()
{
System.out.println("This is the main program");
}

public static void main(String[] args) {

new AbcTest();
}

}

The Output is
==========

This is the main program

2.) Now i add an extends Keyword to the above class and declare as

public class AbcTest extends Object {

AbcTest()
{
System.out.println("This is the main program");
}

public static void main(String[] args) {

new AbcTest();
}

}

The Output is
==========

This is Object class
This is the main program



I already know that even if we don't specify the extends Object , by default all the java classes are extending to Object class. So why there is difference in this output. We should get the 2nd output in Both Cases.
1.)I have learned Overriding Concepts. But can Classes be Overriden.? Is this Overriding of the Object Class.?
Else What is this Concept all about?

Please help me out Ranchers.




"Learning is a Culture where your Eagerness & Curiosity plays a major Role".
Raymond Tong
Ranch Hand

Joined: Aug 15, 2010
Posts: 156

If you have not explicitly extends your class with another class.
It is going to extends java.lang.Object
but not any custom Object defined by yourself.

Imagine you have a lot of class called Object in different packages, which one it would extends ? simply not all.

For your information, by default, all class by default import java.lang.*;

Moreover, I think it better not to name a class or method which is specified in standard Java API which lead to confusion.
Hauke Ingmar Schmidt
Rancher

Joined: Nov 18, 2008
Posts: 371
Hej, welcome!

It is a bad idea to create a class with a name that already exists. java.lang.Object is the very base class of the Java type system. Every class derives from it. It's constructor does not ouput anything.

When you create a class "public class AbcTest" you really write "public class AbcTest extends java.lang.Object". Thus the constructor of java.lang.Object and AbcTest are called sequentially and produce the output.

Classes in the java.lang.* package do not need to be imported explicit. So "public class AbcTest extends Object" will, if there is no class of Name "Object" in the same package as "AbcTest", use java.lang.Object.

IF there is a custom class called "Object" in the same package as "AbcTest" then this class will be used. So "public mypackage.AbcTest extends Object" means "public mypackage.AbcTest extends mypackage.Object" first; if there is no "Object" class in "mypackage", "java.lang.Object" will be used. (If you didn't use any package declarations then the default package is used - which is a bad idea also.)

Just rename your "Object" class to better see the difference.
Ninad Kuchekar
Ranch Hand

Joined: Jan 05, 2010
Posts: 64
Hi Kartik,

The difference between your class and the traditional "Object" class is that, firstly they are very different. Secondly, the compiler considers the fully qualified name(including namespace).

For e.g., If you a write a class Object in a package "copy.cat". If another class "class ABC extends Object" in this package exists it would consider your Java class and not the traditional one.
Unless, you write something like, "class ABC extends java.lang.Object".


-Ninad


Don't walk as if you rule the world, walk as if you don't care who rules it...
Ninad Kuchekar
Ranch Hand

Joined: Jan 05, 2010
Posts: 64
Hi Kartik,

Please write your code within the code tags, for better readability.

-Ninad
karthik Suryanarayanan
Ranch Hand

Joined: Oct 07, 2009
Posts: 94

I understand that writing classes with name Object is a bad practice,etc.I think i have enough knowledge on those things. But what i mean to say is ,is there any overriding concept here. If we do so how would the compiler identify it. I'm creating a java class called Object with my own code inside constructor.If i include that inside java.lang package ,how would the compiler React?

Raymond,
As you were telling, declaring those classes leads to confusion. How does the class compile then? If suppose there's a scenario like that , how would you describe the Solution? Thanks in Advance..


Ninad Kuchekar wrote:Hi Kartik,

Please write your code within the code tags, for better readability.

-Ninad


Ninad,
i think i have written clearly. I dont understand the point where you get confused inside my code.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 56168
    
  13

karthik Suryanarayanan wrote:If i include that inside java.lang package ,how would the compiler React?

Seems like that would be an easy enough question to answer with a little code of your own.

i think i have written clearly. I dont understand the point where you get confused inside my code.

Be sure to use code tags when posting code to the forums. Unformatted or unindented code is extremely hard to read and many people that might be able to help you will just move along to posts that are easier to read. Please click this link ⇒ UseCodeTags ⇐ for more information.

Properly indented and formatted code greatly increases the probability that your question will get quicker, better answers.


[Smart Questions] [JSP FAQ] [Books by Bear] [Bear's FrontMan] [About Bear]
Hauke Ingmar Schmidt
Rancher

Joined: Nov 18, 2008
Posts: 371
If you really want to dig into this, you may have a look at java.lang.ClassLoader, which is responsible for class loading. Another starting point are the endorsed dirs that can be used to override system classes
karthik Suryanarayanan
Ranch Hand

Joined: Oct 07, 2009
Posts: 94

Oops. Sorry. I'll do it better the next time when i post any code. Thank you Ranchers.
karthik Suryanarayanan
Ranch Hand

Joined: Oct 07, 2009
Posts: 94

Hauke Ingmar Schmidt , Thanks for your valuable input. I've started learning those. It's really quite interesting to learn the endorsed standard mechanism.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19216

Hauke Ingmar Schmidt wrote:If you really want to dig into this, you may have a look at java.lang.ClassLoader, which is responsible for class loading. Another starting point are the endorsed dirs that can be used to override system classes

Except that you are not allowed to change or override the system classes. If you do you are breaking the user agreement for the JDK.
 
I agree. Here's the link: http://ej-technologies/jprofiler - if it wasn't for jprofiler, we would need to run our stuff on 16 servers instead of 3.
 
subject: Creating a class with name "Object"
 
Similar Threads
Problem related to enums
Method Exception declaration when inheritance.
static method overriding in same class
inheritance and polymorphism across packages
Methods