Caroso
Caroso

Reputation: 111

How to overload a method in an object type

I have a simple method in a object car, which converts the power from the value in the object's horsepower field (hp) to kilowatts (kW). The code is the following:

MAP MEMBER FUNCTION engine_power
RETURN FLOAT IS
  v_kw FLOAT := 0.745699872;
BEGIN
  RETURN hp * v_kw;
END;

Now I want to overload this method to calculate the power, but the result will be INTEGER instead of FLOAT.

What is the syntax for overloading the method in the body of the object?

Upvotes: 0

Views: 138

Answers (1)

Alex Poole
Alex Poole

Reputation: 191335

You can only have one MAP method, but you can add another method with the integer. You need to add it in both the type and type body statements:

create type my_object as object (hp number,
  MAP MEMBER FUNCTION engine_power RETURN FLOAT,
  MEMBER FUNCTION engine_power RETURN INTEGER
);
/

Type MY_OBJECT compiled

create type body my_object as
  MAP MEMBER FUNCTION engine_power
  RETURN FLOAT IS
    v_kw FLOAT := 0.745699872;
  BEGIN
    RETURN hp * v_kw;
  END;

  MEMBER FUNCTION engine_power
  RETURN INTEGER IS
    v_kw FLOAT := 0.745699872;
  BEGIN
    RETURN TRUNC(hp * v_kw);
  END;
end;
/

Type body MY_OBJECT compiled

I'm not sure you really want either to be a MAP method, but depends how you're using the methods.

Upvotes: 1

Related Questions