Relationship of type variables in Java generic inheritance

Source: Internet
Author: User

This is an example of how a generic class should relate to a type variable in the process of inheritance.

First, several auxiliary classes are given:

Package Generic;public class Animal {    }


Package Generic;public class Person extends Animal {private string name;public person (String name) {super (); this.name = NA Me;} Public String GetName () {return name;} public void SetName (String name) {this.name = name;} @Overridepublic String toString () {return "person [name=" + name + "]";} Public Integer Printandreturn () {return 1;}}

Package Generic;public class Student extends person {private String studentnumber;public Student (string name, String stude Ntnumber) {super (name); this.studentnumber = Studentnumber;} Public String Getstudentnumber () {return studentnumber;} public void Setstudentnumber (String studentnumber) {this.studentnumber = Studentnumber;} @Overridepublic String toString () {return "Student [studentnumber=" + Studentnumber + ", name=" + getName () + "]";}}

Package Generic;public class Building {private string name;public Building (String name) {super (); this.name = name;}}

Package generic;/** * @version 1.00 2004-05-10 * @author Cay Horstmann */public class Pair<t extends person> {   p Rivate T first;   Private T second;   Public Pair () {first = null; second = null;}   Public Pair (t first, T second) {this.first = first;  This.second = second; } public   T GetFirst () {return first;}   Public T Getsecond () {return second;}   public void Setfirst (T newvalue) {first = newvalue;}   public void Setsecond (T newvalue) {second = newvalue;}   }


1. The type variable of a subclass must represent the same scope as the parent class or a subset of the parent class

Package Generic;public class Parison<t extends person> extends pair<t> {}

or

public class Parison<t extends student> extends pair<t> {}

2. When a parent class substitutes a type variable with a specific type, the concrete variable must be an element in the range represented by the type variable

Package Generic;public class Parison extends Pair<person> {}

or

Package Generic;public class Parison extends Pair<student> {}


Relationship of type variables in Java generic inheritance

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.