JAVA EXAMPLE PROGRAMS

JAVA EXAMPLE PROGRAMS

Publish Your Article Here

Program: How implement bounded types (extend superclass) 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 extending same super class? You can restrict the generics type parameter to a certain group of objects which extends same super class. You can achieve this my specifying extends <super-class> at class definitions, look at the example, it gives you more comments to understand.


package com.java2novice.generics;

public class MyBoundedClassEx {

	public static void main(String a[]){
		//Creating object of sub class C and 
		//passing it to BoundEx as a type parameter.
		BoundEx<C> bec = new BoundEx<C>(new C());
		bec.doRunTest();
		//Creating object of sub class B and 
		//passing it to BoundEx as a type parameter.
		BoundEx<B> beb = new BoundEx<B>(new B());
		beb.doRunTest();
		//similarly passing super class A
		BoundEx<A> bea = new BoundEx<A>(new A());
		bea.doRunTest();
		//If you uncomment below code it will throw compiler error
		//becasue we restricted to only of type A and its sub classes.
		//BoundEx<String> bes = new BoundEx<String>(new String());
		//bea.doRunTest();
	}
}
/**
 * This class only accepts type parametes as any class
 * which extends class A or class A itself.
 * Passing any other type will cause compiler time error
 */
class BoundEx<T extends A>{
	
	private T objRef;
	
	public BoundEx(T obj){
		this.objRef = obj;
	}
	
	public void doRunTest(){
		this.objRef.printClass();
	}
}

class A{
	public void printClass(){
		System.out.println("I am in super class A");
	}
}

class B extends A{
	public void printClass(){
		System.out.println("I am in sub class B");
	}
}

class C extends A{
	public void printClass(){
		System.out.println("I am in sub class C");
	}
}

Output:
I am in sub class C
I am in sub class B
I am in super class A
<< 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
What is abstract class or abstract method?
We cannot create instance for an abstract class. We can able to create instance for its subclass only. By specifying abstract keyword just before class, we can make a class as abstract class.

public abstract class MyAbstractClass{

}

Abstract class may or may not contains abstract methods. Abstract method is just method signature, it does not containes any implementation. Its subclass must provide implementation for abstract methods. Abstract methods are looks like as given below:

public abstract int getLength();
Famous Quotations
Discipline is just choosing between what you want now and what you want most.
-- Unknown Author

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 © 2022 by Nataraja Gootooru. All Rights Reserved.