sam
sam

Reputation: 13

Defining a method for bigdecimal

I am trying to define my own method for BigDecimal. The method takes two BigDecimal as parameter and do something with them.

private boolean doSomething(BigDecimal left, BigDecimal right){
    do something...
}

And I call it by

doSomething(b1, b2);

But I think it should be handled by BigDecimal object, Is there any way to define it in BigDecimal class and call it like this?

b1.doSomething(b2);

Upvotes: 0

Views: 73

Answers (1)

Francis G
Francis G

Reputation: 1088

You can extends the class BigDecimal, then create your own function. It goes something like this. Hope this helps.

public class Test1{
    public static void main(String[] args){
        CustomBigDecimal d1 = new CustomBigDecimal(100);
        d1.somefunction();
    }
}

class CustomBigDecimal extends BigDecimal {
    public CustomBigDecimal(int val) {
        super(val);
    }
    public void somefunction(){
    }
}

Upvotes: 2

Related Questions