Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6934107
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T11:55:05+00:00 2026-05-27T11:55:05+00:00

I have the following design: I have an Abstract class Instance , I have

  • 0

I have the following design:
I have an Abstract class Instance,
I have a class Library that extends Instance and
I have a class File that also extends Instance

I’ve created my own linked list implementation and it’s defined as follows:

public class List<T extends Instance> implements Iterable {
    //some other code here

     public Iterator iterator(){
         return new ListIterator(this);

}

now I’ve created a class

public class ListIterator<T extends Instance> implements Iterator<T> {
    private List thisList;
    private Node current;

    public ListIterator(List l){
        thisList=l;
        current=thisList.head.next;
    }
    @Override
    public boolean hasNext() {
        if(current==null)
            return false;
        return false;
    }

    @Override
    public T next() {
        Node temp=current;
        current=current.next;
        return temp.data;
    }
}

Where Node is

public class Node<T extends Instance> {
    public Node<T> next;
    public Node<T> prev;
    public T data;

    public Node(T data,Node prev, Node next){
        this.data=data;
        this.prev=prev;
        this.next=next;
    }   
}

so my problem is as follows: the line return temp.data rises an error:

Type mismatch – cannot convert from Instance to T.

What is wrong with this code?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T11:55:05+00:00Added an answer on May 27, 2026 at 11:55 am

    I’d say that Node.data is a reference to an Instance object? If that is the case, the compiler can’t automatically change an Instance to a T, because even though T is an Instance object (T extends Instance), any given Instance might not be a T.

    The Java Generics tutorial explains it: http://docs.oracle.com/javase/tutorial/extra/generics/subtype.html

    Also, in your List<T> class, you should be specifying Iterator and ListIterator as generic using Iterator<T> and ListIterator<T>, or else the compiler won’t be able to handle the generics properly. Your Node reference also needs to be generic: Node<T>

    Hence you should be using

    private Node<T> current;
    

    and

    public T next() {
        Node<T> temp=current;
        current=current.next;
        return temp.data;
    }
    

    The compiler will usually warn you when you’re using a raw type for a generic class.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following classes: abstract class DTO{ } class SubscriptionDTO extends DTO {
a question about class design. Currently I have the following structure: abstract Base Repository
I have the following code for factory design pattern implementation. class Pen{ public: virtual
I have the following code: public abstract class RepositoryBase<T, TId> : IRepository<T, TId> where
I have the following design in one of my projects: template<typename C> class B
I have the following class hierarchy [BsonKnownTypes(typeof(MoveCommand))] public abstract class Command : ICommand {
In Java, it is very easy to code the following design: public abstract class
I currently have the following: public abstract class CharacterClass { public abstract Attribute FirstAttributeBonus
I have the following actions: abstract class AAction {} class BlueAction: AAction { void
Say I have the following abstract class: class AbstractClass { public: AbstractClass() {} virtual

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.