Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

how to add more methods to class methods  RSS feed

 
Meir Yan
Ranch Hand
Posts: 599
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello all
I don�t know how exactly explain my problem but I will try .
I have base class that has some methods that I use on the jsp page.
my base class looks like this :

now I will like this methods to have more methods for example toBoolean()
so if I will do :

it will return me true or false if age is less then 0 so false
and if the name string len is 0 also flase
and other wise true ;

how can I implement this functions on my methods?
hope I made my self clear
thanks
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16028
87
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
First of all, the code of your base class is unnecessarily elaborate. You could simply have written it like this:


To answer your question: Do you understand how a line of code like this works?:

base.getAge().toBoolean()

It calls getAge() on the object that the variable base points to. That method returns a String object. Then, the method toBoolean() is called on the String object. But it doesn't compile, because class String does not have a method toBoolean().

If you want this to work, then your getName() method must return something else than a String object; it must return an object of a type that has a toBoolean() method.
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66207
151
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The getAge() method returns an int, which has no toBoolean() method (or any methods at all since it is a primitive).

You cannot add methods to a primitive or even any class without extending it.

If you want to add this kind of functionality you will need to create a wrapper class around the type you want to enhance. Personally, I'd just do the comparison and not complicate matters.
 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Maybe you should rethink the whole class.

1. Set your class to public (I'm not sure sure why it as default access)
2. Set a name and an age as private members
3. Correct getName and getAge to return private members.
4. Add a setName and setAge to set those members, or an appropriate constructor.
5. No need to set them static
6. Add a isValid method to do the necessary check of the private members
 
Meir Yan
Ranch Hand
Posts: 599
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
yeah I was thinking about to extend it some how .. but I don�t know how in java .
im sorry but just have to add some methods . ( work demands ) , any way .. im looking the best way to do it
 
Meir Yan
Ranch Hand
Posts: 599
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ok thanks for the fast reply , my stuation is that the method acctelly getting parameter my example only simplyfy this
the method retures value that is in one the age and the other the name but I also need to extand that method to return my more info
for example if the age is less them 0 or the name string is less then 0 so it will return me false
also I cant change much the class it is big class that allot of code are relay on it , I can only extend it .. but I don�t know how�
the functions looks like this :
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16028
87
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
//here I like to add some Boolean check if the name string len is less then 0 it will return me false

The length of a string is never less than 0. You can get the length of a String by calling the method length() on the string.

//here I like to add some Boolean check if the age is less then 0 it will return me false

The expression you're looking for here is: age >= 0
(this evaluates to false if age is less than zero).
[ September 04, 2006: Message edited by: Jesper Young ]
 
Meir Yan
Ranch Hand
Posts: 599
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hello and thanks for the reply
i only gave simple example about the age and name it is only example for other things similar that handle real data
It have to static, because of the application im working on this is afact i cant change it .
i need some how to extend the given class to what i said in the other post.
i wander if it can be done .
 
Srinivas Kalvala
Ranch Hand
Posts: 257
Firefox Browser Hibernate Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,

First a method can return only one object or one primitive value.

In your case , seems you want to pass String value when the length is > 0 and true when the length is ==0 and want to perform toBoolean() on the object.

You can do in one of the following ways:

1) Make the method to give true or false depending on the string length. ( the whole purpose of the method gone).

2) Let it return one object which got Boolean() methods on return object.

3) Let the base class have two Boolean methods() and make the "name" as instance variable inspite of local variable and call the boolean method first in your jsp and depening upon the result call the getXXX() method.

try it until you get a way
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!