Back

Explore Courses Blog Tutorials Interview Questions
0 votes
4 views
in Java by (7k points)

How do you make a class be able to implicitly convert to another class? How do you accomplish this in Java?

1 Answer

0 votes
by (13.1k points)

You cannot overload operators with java. Add a method to your class of choice to perform the conversion. For example:

public class Blammy

{

    public static final Blammy blammyFromHooty(final Hooty hoot)

    {

        Blammy returnValue;

        // convert from Hooty to Blammy.

        return returnValue;

    }

}

  • Only built-ib types have implicit conversions.

  • Explicit conversion is called a cast. For example, int q = 15; double x = (double) q;

  • There can never be implicit conversions of your custom types

  • Extends and implements are not conversions.

  • All implicit conversions are for primitive types.

Want to learn Java? Check out the core Java certification from Intellipaat.

Related questions

0 votes
1 answer
0 votes
1 answer
asked Mar 17, 2021 in Java by Jake (7k points)
0 votes
1 answer
asked Feb 18, 2021 in Java by dante07 (13.1k points)
0 votes
1 answer
asked Feb 16, 2021 in Java by Jake (7k points)
0 votes
1 answer

Browse Categories

...