• Post Reply Bookmark Topic Watch Topic
  • New Topic

OOP  RSS feed

 
Karan Jawa
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What is the difference between?
Object o=new Person();
and Person p=new Person();
 
Campbell Ritchie
Marshal
Posts: 56533
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In one case you are declaring it as Object; you can only use methods in the Object class, and you can replace it with a different type.
In the other case you are declaring it as Person and you can use any methods of the Person class too, but you can only replace the reference with another reference to something which is a Person object.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!