Javatpoint Logo
Javatpoint Logo

Bounded Types in Java

Java is a versatile and powerful programming language known for its strong type system. One of the key features that enhances type safety and promotes code reusability is bounded types. Bounded types allow developers to impose constraints on the types that can be used as generic parameters in classes, interfaces, and methods. By defining these constraints, developers can ensure that certain operations are supported and prevent errors at compile-time. In this article, we'll delve into bounded types in Java and explore how they can be leveraged to write more robust and flexible code.

Introduction to Bounded Types:

In Java, generic types are widely used to create reusable components. They allow developers to define classes, interfaces, and methods that can operate on a variety of different types without sacrificing type safety. However, in some cases, it is necessary to restrict the range of types that can be used as generic arguments. This is where bounded types come into play.

A bounded type parameter is a generic type that is restricted to a specific subset of types. It can be defined using the extends keyword followed by the upper bound. The upper bound can be a class or an interface, and it indicates that the generic type must be a subclass of the specified class or implement the specified interface.

Syntax:

The syntax for defining a bounded type parameter is as follows:

In the above example, T is the type parameter, and MyClassType is the upper bound. It means that any type T used as a parameter must either be MyClassType or its subclass.

Bounded Type Examples:

To better understand bounded types, let's consider a few examples. Suppose we have a class hierarchy representing different types of shapes:

Now, let's define a generic class called ShapeContainer that can hold objects of various shapes:

In this example, the ShapeContainer class has a bounded type parameter T with an upper bound of Shape. It ensures that only shapes or their subclasses can be used as the generic argument.

By using bounded types, we can enforce compile-time type safety and ensure that only shape-related operations can be performed on objects stored in ShapeContainer. For instance, if we try to add a string or any other unrelated type, the compiler will generate an error.

Bounded Type with Multiple Bounds:

Java also supports bounded types with multiple bounds, where a type parameter must satisfy multiple constraints. Multiple bounds are specified using the & symbol, followed by the interface or class names.

Consider the following example of a generic method that compares two objects:

In this example, the generic method compare has a bounded type parameter T with two bounds: Comparable<T> and MyInterface. It ensures that the type parameter T must implement the Comparable interface and extend the MyInterface interface.

By using bounded types with multiple bounds, we can leverage the capabilities of multiple interfaces or classes to provide more flexible and powerful functionality.

Here's a complete Java program that demonstrates the use of bounded types with input and output:

BoundedTypeExample.java

Output:

Drawing a circle
Drawing a rectangle

Bounded types in Java provide a powerful mechanism to enforce constraints on generic types, ensuring type safety and promoting code reuse. By specifying upper bounds, developers can restrict the range of types that can be used as generic parameters, leading to more robust and maintainable code. Whether it's restricting types within a class hierarchy or imposing constraints from multiple interfaces, bounded types enable developers to write more expressive and reliable code.

By understanding and utilizing bounded types effectively, Java developers can harness the full potential of the language's type system and build high-quality software systems.







Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA