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

  • Home
  • SEARCH
  • 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 3352466
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T02:02:01+00:00 2026-05-18T02:02:01+00:00

We have a class hierarchy similar to this one: public class TestDereference { private

  • 0

We have a class hierarchy similar to this one:

public class TestDereference {
 private static MainInterface mi = new MainInterfaceImpl();

 public static void main(String[] args) {
  System.out.println(mi.getSubInterface().getField());
 }
}

interface MainInterface {
 <T extends SubInterface> T getSubInterface();
}

interface SubInterface {
 Field getField();
}

class Field {
 @Override
 public String toString() {
  return "Hooray!";
 }
}

class SubInterfaceImpl implements SubInterface {
 Field f = new Field();

 public Field getField() {
  return f;
 }

}

class MainInterfaceImpl implements MainInterface {
 SubInterfaceImpl si = new SubInterfaceImpl();

 public <T extends SubInterface> T getSubInterface() {
  return (T) si;
 }
}

The interfaces actually have more than one implementation but that’s not the problem. Compiling this with the Eclipse compiler or Java 1.6 works just fine (as seen on ideone). But if I try to compile this with Java 1.5 (which is one of the requirements of our project) gives the following error:

TestDereference.java:12: test.SubInterface cannot be dereferenced
                System.out.println(mi.getSubInterface().getField());
                                                     ^

Also note that it doesn’t happen with JDK 1.6 using -target 1.5 either, only with JDK 1.5

The only cases when this error occurs that I found on the web are related to doing things like this:

double d = 2.0;
d.toString();

where it’s obvious what the problem is.

But it my case it should work, since it’s clear that getSubInterface() returns a SubInterface implementing class that definitely has the getField() method.

So is this a compiler bug? And what options do I have besides doing mi.<SubInterface>getSubInterface() every single time (which works, but since Eclipse doesn’t mark this as an error, most people on the team forget do it, and most of them use only JDK 1.6 so they don’t notice it when compiling with maven/cmd line either)?

  • 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-18T02:02:01+00:00Added an answer on May 18, 2026 at 2:02 am

    Check bug: https://bugs.java.com/bugdatabase/view_bug?bug_id=5003431

    Option one:

    SubInterface si = mi.getSubInterface();
    si.getField();
    

    Option two:

    mi.<SubInterface>getSubInterface().getField()
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a class hierarchy, this one: type TMatrix = class protected //... public
Here's the deal. I have a big class hierarchy and I have this one
I have a class hierarchy like this: @Entity @Table (name=call_distribution_policies) @Inheritance (strategy=InheritanceType.JOINED) public class
Let's have the following class hierarchy: public class ParentClass implements SomeInterface { } public
I have a class hierarchy as such: +-- VirtualNode | INode --+ +-- SiteNode
Suppose we have the following class hierarchy: class Base { ... }; class Derived1
I have class A: public class ClassA<T> Class B derives from A: public class
I'm trying to set up an inheritance hierarchy similar to the following: abstract class
I have a UIViewController subclass called TripViewController. This class has the following method: -
I need to create an object in one class hierarchy that wraps an object

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.