In: Computer Science
Constructor:
-empty body
Class method 1: goodFriend
- return string value, "I like you!"
Class method 2: badFriend
- return string value, "I don't like you!"
Main Method:
- create new Friend object
- call goodFriend and badFriend using Friend object
/* class header */ {
/* constructor method header */ { }
public static void main (String [] args) { Friend f = /* new Friend object */
// call goodFriend using Friend object
// call badFriend using Friend object
}
public /* return type */ goodFriend() {
return "I like you!";
}
public /* return type */ badFriend() {
return "I don't like you!";
}
}
So In this program I will show you how to call the method using object as per given requirements
In this Program I use two diffrent Method which Returning some string value
1>public String goodFriend()
which is returning "I like you!" String
2>public String badFriend()
which is returning "I don't like you!" String
Code:
public class Friend {
public Friend()
{
//Default constructor invoked when the object is created....
}
public String goodFriend()
{
return "I like you!";
}
public String badFriend()
{
return "I don't like you!";
}
public static void main(String[] argv)
{
Friend f=new Friend();//this statement invoke constructor which is basically used to assign the memory to the recently created object
//f.goodFriend() this statment is returning a string value that's why I directly write it into the Print Statement
System.out.println(f.goodFriend());
// You can also use the following approach where you have to first store the returning value into the variable and then print it
String data=f.badFriend();
System.out.println(data);
}
}
Output
I hope you will undestand how to call the function and using Object
Do you feel needful and useful then please upvote me
Thank You