Results 1 to 9 of 9
  1. #1
    Blasz is offline Member
    Join Date
    Aug 2010
    Posts
    16
    Rep Power
    0

    Default Checking subclass of parameter?

    So I have this parameter for a method which is a superclass of two sub-classes and I want to check which subclass the parameter is. Is there a method for this?

  2. #2
    wsaryada is offline Senior Member
    Join Date
    Jun 2007
    Location
    Bali, Indonesia
    Posts
    760
    Rep Power
    8

    Default

    Hi,

    You can use the instanceof operator to check the type of your object.

  3. #3
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,458
    Rep Power
    20

    Default

    But if you need to do that you have a fundamental design flaw.

    What's your problem with providing two overloads of the method, one for each subclass type parameter?

    db

  4. #4
    Blasz is offline Member
    Join Date
    Aug 2010
    Posts
    16
    Rep Power
    0

    Default

    Cheers,

    I have 2 more questions, as I said before my superclass is the parameter and I want to call a method from a specific subclass if the parameter is from that subclass.

    Atm, I'm doing,

    Java Code:
    public void function (Superclass x) {
        if(x instanceOf Subclass1) {
            (Subclass1)x.subclass1method;
        }
    }
    Will that work and is it the most efficient way of what I want to do?

    2nd question is, I have an ArrayList of the superclass, will I be able to add the two subclasses to this list? If so, in the above function would I have to cast the parameter to the subclass of which it is in before I add it to the arraylist or is there no need?

    Sorry if my questions are not clear.

  5. #5
    Blasz is offline Member
    Join Date
    Aug 2010
    Posts
    16
    Rep Power
    0

    Default

    Quote Originally Posted by Darryl.Burke View Post
    But if you need to do that you have a fundamental design flaw.

    What's your problem with providing two overloads of the method, one for each subclass type parameter?

    db
    I was given the interface, I can't change the public functions.

  6. #6
    wsaryada is offline Senior Member
    Join Date
    Jun 2007
    Location
    Bali, Indonesia
    Posts
    760
    Rep Power
    8

    Default

    Did you mean that you can't change the interface given to you? What about extends the interface and add the behavior that you want to implements so that all the class that implements the interface will have the same contract.

  7. #7
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,458
    Rep Power
    20

    Default

    Quote Originally Posted by Blasz View Post
    I was given the interface, I can't change the public functions.
    If fulfilling an interface contract depends on type information not present in the interface, that's still a design flaw.

    db

  8. #8
    indyjoel is offline Member
    Join Date
    Nov 2010
    Posts
    4
    Rep Power
    0

    Default

    Quote Originally Posted by Darryl.Burke View Post
    But if you need to do that you have a fundamental design flaw.

    What's your problem with providing two overloads of the method, one for each subclass type parameter?

    db
    Darryl,

    I am facing the same issue and my instructor was NOT impressed with the use of instanceof but I cannot see (and I have researched and searched this) another way, even though I know it exists.

    I am not sure what you mean about "providing two overloads of the method, one for each subclass type parameter"

    I understand that this is the best way but am not sure how to implement this

    Apologies but I am just starting my Java journey and this has confused the you know what out of me

  9. #9
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,585
    Rep Power
    12

    Default

    (it would be better to start your own thread for your own question - perhaps with a link to this one to give an example of what your professor was not impressed with)

    Java Code:
    class Thing {}
    class RedThing extends Thing {}
    class GreenThing extends Thing {}
    
    public class Eg {
        /*
        void doSomething(Thing thing) {
            if(thing instanceof RedThing) {
                System.out.println("Stop!");
            } else {
                System.out.println("Go");
            }
        }
        */
    
        void doSomething(RedThing thing) {
            System.out.println("Stop!");
        }
    
        void doSomething(GreenThing thing) {
            System.out.println("Go");
        }
    
        public static void main(String[] args) {
            Eg eg = new Eg();
    
            eg.doSomething(new RedThing());
            eg.doSomething(new GreenThing());
    
            //Thing thing = new RedThing();
            //eg.doSomething(thing);
        }
    }

    The commented lines at the end of main() won't compile. If you want a behaviour to depend on what a thing is consider making that behaviour the behaviour (method) of the thing.

    Java Code:
    abstract class Thing {
        abstract void doSomething();
    }
    
    class RedThing extends Thing {
        void doSomething() {
            System.out.println("Stop!");
        }
    }
    
    class GreenThing extends Thing {
        void doSomething() {
            System.out.println("Go");
        }
    }
    
    public class Eg {
        public static void main(String[] args) {
            new RedThing().doSomething();
            new GreenThing().doSomething();
    
            Thing thing = new RedThing();
            thing.doSomething();
        }
    }

Similar Threads

  1. Subclass help
    By amystauff in forum New To Java
    Replies: 1
    Last Post: 05-30-2010, 05:36 AM
  2. Object is a Subclass of
    By AndrewM16921 in forum New To Java
    Replies: 3
    Last Post: 02-10-2010, 10:42 AM
  3. Subclass name to be a parameter?
    By Peetahzee in forum New To Java
    Replies: 6
    Last Post: 12-12-2009, 04:51 PM
  4. Subclass definition
    By Java Tip in forum java.lang
    Replies: 0
    Last Post: 04-23-2008, 09:03 PM
  5. subclass vs inner class
    By bugger in forum New To Java
    Replies: 1
    Last Post: 01-13-2008, 08:31 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •