O'Reilly logo

From Java to C#: A Developer's Guide by Heng Ngee Mok

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

22.1. Explaining operator overloading

In both Java and C#, the binary * operator has always been used to obtain the product of two numerical values, which are the two operands. The code below shows a class which represents a fraction with two int fields – numerator and denominator. I have added a constructor which takes in two ints to initialize these two fields:

 1: class Fraction{
 2:   public int numerator;
 3:   public int denominator;
 4:
 5:   // constructor
 6:   public Fraction (int numerator, int denominator){
 7:     this.numerator = numerator;
 8:     this.denominator = denominator;
 9:   }
10: }

When you want to perform a fraction multiplication, you can write a static method, called Multiply, which takes in two Fractions and which returns a resultant new ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required