Win a copy of Micro Frontends in Action this week in the Server-Side JavaScript and NodeJS forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Bear Bibeault
  • Junilu Lacar
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • salvin francis
  • Frits Walraven
Bartenders:
  • Scott Selikoff
  • Piet Souris
  • Carey Brown

cannot override getClass

 
Greenhorn
Posts: 22
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi everyone,

We know that only overriden instance methods are dynamically invoked based on the real object's type.
So why getClass() have this behaviour without being overrided because You cannot override getClass (https://docs.oracle.com/javase/tutorial/java/IandI/objectclass.html).

Thank you in advance
 
Ranch Hand
Posts: 261
15
Android Angular Framework Spring AngularJS Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello, Alion:

Actually what you are seeing is not overridden behavior.  The 'getClass()' method is returning the Class object associated with the instance.  Although the returned value can vary infinitely, the behavior is actually no different.  If you were to call "getClass()" on something declared and constructed as just "Object o = new Object();", the instance 'o' would return you a Class instance, and that instance would have methods to tell the class' name, etc.  So this is a case of something being configured with different data than it would be otherwise.  If you were to declare something wildly different like a java.sql.Connection, it's 'getClass()' returned value whose getClassName() method would give a different string.  It need not even be an instance of the same Class class (I don't know: maybe you could extend the Class class and return something different? I've never tried as it's not something most users need to ever do).

The point is, the getter just gives you whatever has been set somehow.

As a beginner, perhaps this is mind-blowing, or perhaps it's a boring fact you just hadn't thought of it before.  But you can make your own "getter" that returns some object like, say Employee.  Employee could have fields in it representing address, salary, date of hire, date of birth, etc.  And you could use that one Employee class to represent anyone who has a job in the entire world if you make it general enough.  The same goes for Class.

Enjoy.
 
Alion Bada
Greenhorn
Posts: 22
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

L Foster wrote:Hello, Alion:

Actually what you are seeing is not overridden behavior.  The 'getClass()' method is returning the Class object associated with the instance.  Although the returned value can vary infinitely, the behavior is actually no different.  If you were to call "getClass()" on something declared and constructed as just "Object o = new Object();", the instance 'o' would return you a Class instance, and that instance would have methods to tell the class' name, etc.  So this is a case of something being configured with different data than it would be otherwise.  If you were to declare something wildly different like a java.sql.Connection, it's 'getClass()' returned value whose getClassName() method would give a different string.  It need not even be an instance of the same Class class (I don't know: maybe you could extend the Class class and return something different? I've never tried as it's not something most users need to ever do).

The point is, the getter just gives you whatever has been set somehow.

As a beginner, perhaps this is mind-blowing, or perhaps it's a boring fact you just hadn't thought of it before.  But you can make your own "getter" that returns some object like, say Employee.  Employee could have fields in it representing address, salary, date of hire, date of birth, etc.  And you could use that one Employee class to represent anyone who has a job in the entire world if you make it general enough.  The same goes for Class.

Enjoy.



Thank you very much for your answer even if I don't understand all you've said
 
L Foster
Ranch Hand
Posts: 261
15
Android Angular Framework Spring AngularJS Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Alion:
If you have not understood it all, then I have not answered well enough.  Sorry for that.  I will try one more time.  Maybe I should stick very closely to the words of your question.

We know that only overriden instance methods are dynamically invoked based on the real object's type.
So why getClass() have this behaviour without being overriden?  Because You cannot override getClass (https://docs.oracle.com/javase/tutorial/java/IandI/objectclass.html).



The "getClass()" method is not being overridden.  Instead, it is just returning a different value.  The behavior is not changed--only the value being returned is different.

 
Alion Bada
Greenhorn
Posts: 22
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

L Foster wrote:Alion:
If you have not understood it all, then I have not answered well enough.  Sorry for that.  I will try one more time.  Maybe I should stick very closely to the words of your question.

We know that only overriden instance methods are dynamically invoked based on the real object's type.
So why getClass() have this behaviour without being overriden?  Because You cannot override getClass (https://docs.oracle.com/javase/tutorial/java/IandI/objectclass.html).



The "getClass()" method is not being overridden.  Instead, it is just returning a different value.  The behavior is not changed--only the value being returned is different.



Now I understand lol
Thank you very much  !!
 
Alion Bada
Greenhorn
Posts: 22
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

L Foster wrote:Alion:
If you have not understood it all, then I have not answered well enough.  Sorry for that.  I will try one more time.  Maybe I should stick very closely to the words of your question.

We know that only overriden instance methods are dynamically invoked based on the real object's type.
So why getClass() have this behaviour without being overriden?  Because You cannot override getClass (https://docs.oracle.com/javase/tutorial/java/IandI/objectclass.html).



The "getClass()" method is not being overridden.  Instead, it is just returning a different value.  The behavior is not changed--only the value being returned is different.



I add this to make things more clearer :
Every class loaded in Java has a corresponding instance of java.lang.Class representing that class.
Instances of the class Class represent classes and interfaces in a running Java application.

it helped a lot :p
 
reply
    Bookmark Topic Watch Topic
  • New Topic