• Post Reply Bookmark Topic Watch Topic
  • New Topic

problems with interface java  RSS feed

 
raj david
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,

I am having problems in the public class Lab, public void methodA() method. How can I use local variables string to reference objects. I have used // to show you where I am having problems, lines 33-40. I want to assign Man String to "Hello". Thank you

 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16060
88
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The code you posted is not complete - for example, the classes Dog and table are missing. (By the way, do you really want class Cat to extend class Dog? Inheritance implies an is a relationship: what you're saying is that a Cat is a Dog, which sounds quite strange...).

It's not very clear to me what you're asking. You can assign literal values to String variables:

Or you can assign the result of some expression, or a method call, to a variable:

Can you explain more clearly what you're having doubts about?
 
raj david
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Actually Instance variable should have read...

// Instance variables, mammal is the interface
private mammal test1;

that was a typo error.

At the moment the output shows

"Hello" referencing to test1.

What I am trying to acheive is to assign Man to "Hello". If code test1.example(); this works fine, example sends message to test1. Man is the local variable in the methodA() method.





 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16060
88
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm sorry, but it's still difficult what you want to achieve exactly - maybe because you're not so familiar with the Java terminology…

At the moment, the example() method in the interface and in the class Cat doesn't return anything (it's a void method). You could change it to return a String:

See Defining Methods in Sun's excellent Java tutorials.
 
ravindra patil
Ranch Hand
Posts: 234
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
HI ... please use code tags,

anyways i dindnt understand

String Man = "Man" + test; // this is wrong

what is "test " here , is it what you have defined as provate table test1 ??

you are trying something like
String s1= "man" +test1;

which will print hashcode values defined a variable in your class table and access it here using getter method
 
Campbell Ritchie
Marshal
Posts: 56584
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you have a String, followed by the catenation operator (+), whatever follows is converted to a String.
If it is an Object, it will use its toString method; if it is a primitive you will get 123 or 123.45 or similar or true/false or the representation of a character.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!