JAVA EXAMPLE PROGRAMS

JAVA EXAMPLE PROGRAMS

Checkout for Promo Codes

Program: How implement bounded types (implements an interface) with generics?


As of now we have seen examples for only one type parameter. What happens in case we want to access group of objects comes from same family, means implementing same interface? You can restrict the generics type parameter to a certain group of objects which implements same interface. You can achieve this my specifying extends <interface-name> at class definitions, look at the example, it gives you more comments to understand. You can also specify multiple interfaces at the definision. you can do this by specifying mulitple interfaces seperated by "&". You can also specify class which implements an interface and the interface together. For example:

class MyClass<T extends TestClass & TestInterface> {

Look at example for more understanding.


package com.java2novice.generics;

public class MyBoundedInterface {

	public static void main(String a[]){
		
		//Creating object of implementation class X called Y and 
		//passing it to BoundExmp as a type parameter.
		BoundExmp<Y> bey = new BoundExmp<Y>(new Y());
		bey.doRunTest();
		//Creating object of implementation class X called Z and 
		//passing it to BoundExmp as a type parameter.
		BoundExmp<Z> bez = new BoundExmp<Z>(new Z());
		bez.doRunTest();
		//If you uncomment below code it will throw compiler error
		//becasue we restricted to only of type X  implementation classes.
		//BoundExmp<String> bes = new BoundExmp<String>(new String());
		//bea.doRunTest();
	}
}

class BoundExmp<T extends X>{
	
	private T objRef;
	
	public BoundExmp(T obj){
		this.objRef = obj;
	}
	
	public void doRunTest(){
		this.objRef.printClass();
	}
}

interface X{
	public void printClass();
}

class Y implements X{
	public void printClass(){
		System.out.println("I am in class Y");
	}
}

class Z implements X{
	public void printClass(){
		System.out.println("I am in class Z");
	}
}

Output:
I am in class Y
I am in class Z
<< Previous Program | Next Program >>

Java Generics Sample Code Examples

  1. Write a simple generics class example.
  2. Write a simple generics class example with two type parameters.
  3. How implement bounded types (extend superclass) with generics?
  4. How implement bounded types (implements an interface) with generics?
  5. What is generics wildcard arguments? Give an example.
Knowledge Centre
doPost Vs doGet methods
doGet() method is used to get information, while doPost() method is used for posting information. doGet() requests can't send large amount of information and is limited to 240-255 characters. However, doPost()requests passes all of its data, of unlimited length. A doGet() request is appended to the request URL in a query string and this allows the exchange is visible to the client, whereas a doPost() request passes directly over the socket connection as part of its HTTP request body and the exchange are invisible to the client.
Famous Quotations
If you don’t make mistakes, you’re not working on hard enough problems. And that’s a big mistake.
-- Frank Wilczek

About Author

I'm Nataraja Gootooru, programmer by profession and passionate about technologies. All examples given here are as simple as possible to help beginners. The source code is compiled and tested in my dev environment.

If you come across any mistakes or bugs, please email me to [email protected].

Most Visited Pages

Other Interesting Sites

Reference: Java™ Platform Standard Ed. 7 - API Specification | Java™ Platform Standard Ed. 8 - API Specification | Java is registered trademark of Oracle.
Privacy Policy | Copyright © 2020 by Nataraja Gootooru. All Rights Reserved.