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 1094035
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T23:54:58+00:00 2026-05-16T23:54:58+00:00

Consider the following program: import java.util.List; import java.util.ArrayList; public class TypeTest { public static

  • 0

Consider the following program:

import java.util.List;
import java.util.ArrayList;

public class TypeTest {

    public static class TypeTestA extends TypeTest {

    }

    public static class TypeTestB extends TypeTest {

    }

    public static final class Printer {
        public void print(TypeTest t) {
            System.out.println("T");
        }

        public void print(TypeTestA t) {
            System.out.println("A");
        }

        public void print(TypeTestB t) {
            System.out.println("B");
        }

        public <T extends TypeTest> void print(List<T> t) {
            for (T tt : t) {
                print(normalize(tt.getClass(), tt));
            }
        }

        private static <T> T normalize(Class<T> clz, Object o) {
            return clz.cast(o);
        }

    }
    public static void main(String[] args) {
        Printer printer = new Printer();
        TypeTest t1 = new TypeTest();
        printer.print(t1);
        TypeTestA t2 = new TypeTestA();
        printer.print(t2);
        TypeTestB t3 = new TypeTestB();
        printer.print(t3);
        System.out.println("....................");
        List<TypeTestB> tb1 = new ArrayList<TypeTestB>();
        tb1.add(t3);
        printer.print(tb1);
    }
}

The main method now prints:

T  
A  
B  
....................  
T  

What should I do to make it print the followings?

T  
A  
B  
....................  
B  

I’d like to avoid writing a loop such as the following for each of the type that can be printed:

   public void printTypeTestB(List<TypeTestB> t) {
        for (TypeTestB tt : t) {
            print(tt);
        }
    }
  • 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-16T23:54:58+00:00Added an answer on May 16, 2026 at 11:54 pm

    The root of your problem is that Java method overloads are resolved at compile time based on the declared type of the method argument expressions. Your program seems to be trying to use runtime dispatching to different method overloads. That simply doesn’t work in Java.

    The fact that you are using generics in your example is a bit of a red herring. You would have the same problem if you replaced the type parameter <T> with TypeTest.

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

Sidebar

Ask A Question

Stats

  • Questions 534k
  • Answers 534k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer SELECT t0.* FROM sometable AS t0 LEFT JOIN sometable AS… May 17, 2026 at 12:47 am
  • Editorial Team
    Editorial Team added an answer Is it using jsonp? You might be able to return… May 17, 2026 at 12:47 am
  • Editorial Team
    Editorial Team added an answer It can, if you tell it to keep the files… May 17, 2026 at 12:47 am

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Related Questions

Consider the following program #include <iostream> #include<cstdlib> using namespace std; class E { public:
Consider the following program. #include<iostream> using namespace std; class base { public: int _bval;
Consider the following code: class Program { static void Main(string[] args) { A a
Consider the following code: class Program { static void Main(string[] args) { new Program().Run(args);
Consider the following code. public interface IFoo { } public class Bar { public
Consider following program: static void Main (string[] args) { int i; uint ui; i
consider the following program: namespace NS2 { class base { }; template<typename T> int
Consider the following code parts: this is Timing.h: class Timing { public: //creates a
Consider the following code snippet import qualified Foreign.Concurrent import Foreign.Ptr (nullPtr) main :: IO
Consider the following program. How is the behaviour that it displays (namely that exceptions

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.