• Post Reply Bookmark Topic Watch Topic
  • New Topic

JAVA- Object Refernce  RSS feed

 
Divyansh Sharma
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

Question:

As far as I have learned that to create and object of a class we have to make a reference to it, and for that we create reference variable
For Ex:



Main Question:

Can we create a reference variable of a string; can we create object, call a method with the same-line?

as



Thanks In Advance
 
Joel Christophel
Ranch Hand
Posts: 250
1
Chrome Eclipse IDE
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Divyansh Sharma wrote:


This line is legal as long as your Employee class has a method getStrength() that returns a String.
 
Divyansh Sharma
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Joel,

Main Concern is, Can String act as ref variable for the object
String EmpName = new Employee("LordShiva", "immortal").getStrength();



----------------------
Joel Christophel wrote:
Divyansh Sharma wrote:


This line is legal as long as your Employee class has a method getStrength() that returns a String.
 
Joel Christophel
Ranch Hand
Posts: 250
1
Chrome Eclipse IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Divyansh Sharma wrote:Hi Joel,
Main Concern is, Can String act as ref variable for the object
String EmpName = new Employee("LordShiva", "immortal").getStrength();


Strings variables can only refer to Strings. Employee variables can only refer to Employees.

So you couldn't do the following:

 
Muhammad Khojaye
Ranch Hand
Posts: 449
IntelliJ IDE Java Scala
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can we create a reference variable of a string; can we create object, call a method with the same-line?

You cannot create an object that refer to different class, however, if the class is related via Inheritence, it is possible. For example, consider below code,

The above code is fine because ArrayList class implements List interface.

Also check out Polymorphism for more understanding. I hope it helps
 
Campbell Ritchie
Marshal
Posts: 56584
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think there is a lot of misunderstanding in this thread.
You are passing a String to the constructor. That works as long as you have a constructor taking a String parameter. (By the way: delete any constructors with no parameters; they will simply get in the way)Now, you can create an Employee object, and the compiler will insist you always pass a String for name and a String for strength. But there is nothing to check whether you can writeNow you have an Employee object which you can assign to an Employee reference. If you get the String from getStrength, you have a String object which you can assign to any available String reference.
Forget about inheritance for the time being.
As you doubtless already know, you declare a reference and you can then assign something of the same (or at least compatible) type to it. You can't assign emp to a String reference and you can't assign the return value of getStrength to an Employee reference. Java® is a type‑safe language and strongly typed, so the compiler will complain bitterly if you try to do that.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!