Hi,

if you can't use inheritance or a interface, you have to use method
invokation.

public class Untitled1 {

    public static void main(String[] args)
    {
        Untitled1 test1 = new Untitled1();
    }

    public Untitled1() {

        Object o = new A();
        invokeDoShow(o);

        o = new B();
        invokeDoShow(o);

        o = new C();
        invokeDoShow(o);
    }

    private void invokeDoShow(Object o)
    {
        try {
            // search for the method with no params
            java.lang.reflect.Method m = o.getClass().getMethod("doShow",
new Class[] {});

            if (m != null)
                try {
                    // invokes the method with no params from the given
object
                          m.invoke(o, new Object[] {});
                } catch (Exception e)
                {
                    /* IllegalAccessException - if the underlying method is
inaccessible.
                       IllegalArgumentException - if the number of actual
and formal parameters differ, or if an unwrapping conversion fails.
                       InvocationTargetException - if the underlying method
throws an exception.
                       NullPointerException - if the specified object is
null and the method is an instance method.
                       ExceptionInInitializerError - if the initialization
provoked by this method fails. */
                    e.printStackTrace();
                }
        } catch (NoSuchMethodException e) {
            e.printStackTrace();
        } catch (SecurityException e) {
            e.printStackTrace();
        }
    }
}

    public class A{
        public void doShow() {
            System.out.println("A.doShow()");
        }
    }

    public class B{
        public void doShow() {
            System.out.println("B.doShow()");
        }
    }

    public class C{
        public void doShow() {
            System.out.println("C.doShow()");
        }
    }
}

Hope this helps

Joerg Meister

> -----Urspr�ngliche Nachricht-----
> Von: A mailing list about Java Server Pages specification and reference
> [mailto:[EMAIL PROTECTED]]Im Auftrag von Shawn Zhu
> Gesendet: Donnerstag, 23. November 2000 00:17
> An: [EMAIL PROTECTED]
> Betreff: Re: finally, something interesting!
>
>
> Yep Namratha, you got 2 of the 3 ways I know.
> in summary,
> I can sort of achieve what I asked in the following:
> 1. interface
> 2. class inheritance
> 3. use of "InstanceOf"
>
> I've also thought about using Class and Method classes
> Like Venkatesh had mentioned, however, that does not
> work out.  Because we'll be looking for a method doShow()
> when we passed in Object which does not have such an method.
> It fails at compile time as well.
>
> After the above said, now lets say if you were only give
> Class A, B, And C, i.e. those class all have been created, like
> those prepackaged classes, you can't change their implementations.
> Now is it still possible to have a ShowAll method?
>
> > -----Original Message-----
> > From: Namratha Reddy [mailto:[EMAIL PROTECTED]]
> > Sent: Wednesday, November 22, 2000 1:54 PM
> > To: [EMAIL PROTECTED]
> > Subject: Re: finally, something interesting!
> >
> >
> > You could accomplish this in one of many ways:
> > 1. Create an interface with the signature of the 'doShow'
> > method and make the
> > classes A, B, and C implement this interface.
> >
> > 2. Use inheritance: Create a class X and have classes A, B,
> > and C extend that
> > class.
> > and the doShow methods are defined as follows:
> >   in A:
> >   doShow()
> >   {
> >     System.out.println("Doshow method in Class A");
> >   }
> >
> >   in B:
> >   doShow()
> >   {
> >     System.out.println("Doshow method in Class B");
> >   }
> >
> >   in C:
> >   doShow()
> >   {
> >     System.out.println("Doshow method in Class C");
> >   }
> >
> > Then in the show method:
> >   public void show(X var)
> >   {
> >     var.doShow();
> >   }
> >
> > and in the calling method if you have the following:
> >   A var1 = new A();
> >   B var2 = new B();
> >   C var3 = new C();
> >
> >   show(var1);
> >   show(var2);
> >   show(var3);
> >
> > The output will be as follows:
> >   Doshow method in Class A
> >   Doshow method in Class B
> >   Doshow method in Class C
> >
> > Hope this helps.
> > > Scope: Java
> > > Q:
> > > if you have three class A, B, and C all have the same
> > method doShow();
> > > We only know that all inherit from Class Object (like all
> > other classes do).
> > >
> > > Is it possible to have a public function that takes in an
> > Class Object that
> > > may belong
> > > to A, B, or C, and call doShow()?
> > >
> > > Something conception like:
> > >
> > > public void show(Object o) {
> > >         o.doShow();
> > > }
> > >
> > > Of course the above code won't compile.  One way to hack it
> > is to use
> > > "InstanceOf" (one of the answer) but it's a unclean hack.
> > >
> > > Anyone has any better ideas?  I thought about going from
> > Object.getClass(),
> > > but it does
> > > not seem to be the way.
> > >
> > >
> > ==============================================================
> > =============
> > > To unsubscribe: mailto [EMAIL PROTECTED] with body:
> > "signoff JSP-
> > INTEREST".
> > > For digest: mailto [EMAIL PROTECTED] with body: "set
> > JSP-INTEREST DIGEST".
> > > Some relevant FAQs on JSP/Servlets can be found at:
> > >
> > >  http://java.sun.com/products/jsp/faq.html
> > >  http://www.esperanto.org.nz/jsp/jspfaq.html
> > >  http://www.jguru.com/jguru/faq/faqpage.jsp?name=JSP
> > >  http://www.jguru.com/jguru/faq/faqpage.jsp?name=Servlets
> >
> > Namratha Reddy
> > Staff Consultant
> > High Gear Inc.
> >
> > Phone: (262)814-1660 ext.522
> > E-Mail: [EMAIL PROTECTED]
> > www.high-gear.com
> >
> > ==============================================================
> > =============
> > To unsubscribe: mailto [EMAIL PROTECTED] with body:
> > "signoff JSP-INTEREST".
> > For digest: mailto [EMAIL PROTECTED] with body: "set
> > JSP-INTEREST DIGEST".
> > Some relevant FAQs on JSP/Servlets can be found at:
> >
> >  http://java.sun.com/products/jsp/faq.html
> >  http://www.esperanto.org.nz/jsp/jspfaq.html
> >  http://www.jguru.com/jguru/faq/faqpage.jsp?name=JSP
> >  http://www.jguru.com/jguru/faq/faqpage.jsp?name=Servlets
> >
>
> ==================================================================
> =========
> To unsubscribe: mailto [EMAIL PROTECTED] with body: "signoff
> JSP-INTEREST".
> For digest: mailto [EMAIL PROTECTED] with body: "set
> JSP-INTEREST DIGEST".
> Some relevant FAQs on JSP/Servlets can be found at:
>
>  http://java.sun.com/products/jsp/faq.html
>  http://www.esperanto.org.nz/jsp/jspfaq.html
>  http://www.jguru.com/jguru/faq/faqpage.jsp?name=JSP
>  http://www.jguru.com/jguru/faq/faqpage.jsp?name=Servlets
> >

===========================================================================
To unsubscribe: mailto [EMAIL PROTECTED] with body: "signoff JSP-INTEREST".
For digest: mailto [EMAIL PROTECTED] with body: "set JSP-INTEREST DIGEST".
Some relevant FAQs on JSP/Servlets can be found at:

 http://java.sun.com/products/jsp/faq.html
 http://www.esperanto.org.nz/jsp/jspfaq.html
 http://www.jguru.com/jguru/faq/faqpage.jsp?name=JSP
 http://www.jguru.com/jguru/faq/faqpage.jsp?name=Servlets

Reply via email to