• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

JAVA- Object Refernce

 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 250
1
Eclipse IDE Chrome
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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:

 
Ranch Hand
Posts: 449
Scala IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Marshal
Posts: 79233
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Now I am super curious what sports would be like if we allowed drugs and tiny ads.
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic