Extending Java Generic Classes

Categories: Java; Tagged with: ; @ September 5th, 2013 23:34

“You can subtype a generic class or interface by extending or implementing it. The relationship between the type parameters of one class or interface and the type parameters of another are determined by the extends and implements clauses.”

http://docs.oracle.com/javase/tutorial/java/generics/inheritance.html

package com.liguoliang.core.dao;

import java.util.List;

import com.liguoliang.core.modle.Animal;

public abstract class AbstractDAO <T extends Animal>{
	
	protected  T saveAnimal(T animal) {
		System.out.println("Save: " + animal.toString());
		return animal;
	}
	
	protected abstract List<T> getList();
}

Sub class should follow the contract made by the super class: T extends Animal

Otherwise, got the Error: Bound mismatch: The type ** is not a valid substitute for the bounded parameter of the type AbstractDAO

package com.liguoliang.core.dao;

import java.util.List;

import com.liguoliang.core.modle.Dog;

public class DogDAO<WhatEver extends Object> extends AbstractDAO<Dog> {
	
	public Dog saveDog(Dog dog) {
		return saveAnimal(dog);
	}

	@Override
	protected List<Dog> getList() {
		return null;
	}
	
	public WhatEver whatEver(WhatEver w) {
		return w;
	}
	
}

<->



// Proudly powered by Apache, PHP, MySQL, WordPress, Bootstrap, etc,.