• Post Reply Bookmark Topic Watch Topic
  • New Topic

Creating a class with name "Object"  RSS feed

 
karthik Suryanarayanan
Ranch Hand
Posts: 121
Java Opera Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.



 
Raymond Tong
Ranch Hand
Posts: 255
2
IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 436
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 64
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Ninad Kuchekar
Ranch Hand
Posts: 64
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Kartik,

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

-Ninad
 
karthik Suryanarayanan
Ranch Hand
Posts: 121
Java Opera Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 66306
152
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Hauke Ingmar Schmidt
Rancher
Posts: 436
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 121
Java Opera Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Oops. Sorry. I'll do it better the next time when i post any code. Thank you Ranchers.
 
karthik Suryanarayanan
Ranch Hand
Posts: 121
Java Opera Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 21135
87
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!