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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T19:10:02+00:00 2026-05-29T19:10:02+00:00

Consider the following: public class GenericTest { static void print(int x) { System.out.println(Int: +

  • 0

Consider the following:

 public class GenericTest {
    static void print(int x) {
        System.out.println("Int: " + x);
    }
    static void print(String x) {
        System.out.println("String: " + x);
    }

    static void print(Object x) {
        System.out.println("Object: " + x);
    }

    static <T> void printWithClass(T t) {
        print(t);
    }
    public static void main(String argsp[]) {
        printWithClass("abc");
    }
}

It prints Object: abc.
Why doesn’t it print String: abc?

  • 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-29T19:10:13+00:00Added an answer on May 29, 2026 at 7:10 pm

    Java supports method overriding (dynamic type binding), but not what you are trying to achieve (overloading is static polymorphism and not dynamic).

    In order to achieve what you want to achieve in Java, you need double dispatch.

    Visitor Pattern should be your friend here.

    I have written you a code sample.

    public class Test {
    
        public static void main(String argsp[]) {
            PrintTypeImpl typeImpl = new PrintTypeImpl(new StringType(), new IntType(), new ObjectType());
            typeImpl.accept(new PrintVisitor());
        }
    
        static final class PrintVisitor implements TypeVisitor {
            public void visit(IntType x) {
                System.out.println("Int: ");
            }
    
            public void visit(StringType x) {
                System.out.println("String: ");
            }
    
            public void visit(ObjectType x) {
                System.out.println("Object: ");
            }
        }
    
        interface TypeVisitor {
            void visit(IntType i);
    
            void visit(StringType str);
    
            void visit(ObjectType obj);
        }
    
        interface PrintType {
            void accept(TypeVisitor visitor);
        }
    
        static class StringType implements PrintType {
            @Override
            public void accept(TypeVisitor visitor) {
                visitor.visit(this);
            }
        }
    
        static class ObjectType implements PrintType {
            @Override
            public void accept(TypeVisitor visitor) {
                visitor.visit(this);
            }
        }
    
        static class IntType implements PrintType {
            @Override
            public void accept(TypeVisitor visitor) {
                visitor.visit(this);
            }
        }
    
        static final class PrintTypeImpl implements PrintType {
    
            PrintType[] type;
    
            private PrintTypeImpl(PrintType... types) {
                type = types;
            }
    
            @Override
            public void accept(TypeVisitor visitor) {
                for (int i = 0; i < type.length; i++) {
                    type[i].accept(visitor);
                }
            }
        }
    
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Consider the following snippet: public class ReflectionTest { public static void main(String[] args) {
Consider the following class public class PlanetKrypton { public static void CallSuperManforHelp(string helpMessage, params
Consider the following code :- public class UsingWait1{ public static void main(String... aaa){ CalculateSeries
Consider following example : public class SomeBusinessLayerService : DataService<MyEntityContainer> { [WebInvoke] void DoSomething(string someParam)
Consider the following code: public class MyClass { public static string MyStaticMethod() { //string
Consider the following Java class: public class Foo { public static void doStuff() {
Consider the following code public class sqldetails extends JFrame implements ActionListener{ static String username=null;
Consider the following code: public class Foo { private static final Object LOCK =
Consider the following code: public partial class TeacherControlPanel : System.Web.UI.Page { protected string username
Consider the following: public static class FileSerializer { public static void SaveToFile<T>(this T obj,

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.