Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 6 of 6

Thread: Polymorphism issue

  1. #1
    Junior Member
    Join Date
    Oct 2010
    Posts
    21
    Thanks
    3
    Thanked 1 Time in 1 Post

    Default Polymorphism issue

    Hey there. I'm making a game that utilises two different modes which inherit from an abstract class. The basic aim of what I'm trying to achieve is to use polymorphism so that depending on the mode chosen by the user, the functionality offered to them is different. To do this I have a method in the abstract class which I want to overload (I believe that's what it should be, as the abstract class method has no parameter and the child method has one).

    However, my issue is that I cannot access this method only the one in the abstract class from the reference. I have no problem using overloading in the same class, it is just using it in a child class that is perplexing me. I have spent a good amount of time researching online and re-reading a Java book I have but neither produced fruitful results I'm afraid. If anyone could explain why this isn't working or guide me in the right direction I would appreciate it an awful lot.


  2. #2
    Super Moderator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,225
    Thanks
    176
    Thanked 817 Times in 760 Posts
    Blog Entries
    5

    Default Re: Polymorphism issue

    Not sure I understand the problem, but if you have an abstract class with abstract function, just implement that function in the child class. This allows you to abstract the actual algorithm.
    public abstract class Parent{
        public abstract int getValue();
    }
    public class One extends Parent{
        public int getValue(){
            return 1;
        }
    }
    public class Two extends Parent{
        public int getValue(){
            return 2;
        }
    }
    ///somewhere else in the program, the returned Object abstracts the actual implementation details. 
    public Parent getParent(int val){
        switch(val){
            case 1:
                return new One();
            case 2:
                return Two();
        }
    }

  3. #3
    Junior Member
    Join Date
    Oct 2010
    Posts
    21
    Thanks
    3
    Thanked 1 Time in 1 Post

    Default Re: Polymorphism issue

    Thank you for your reply. That very nearly was what I was trying to convey, I believe I may have been going about it wrong now. The following is what I wish to achieve:

    public abstract class Parent{
        public void setValue();
         value = "5";
     
    }
    public class One extends Parent{
        public void setValue(int inputvalue){
            value = inputvalue;
        }
    }

    As you see what I want to do is basically have a method in the parent class where it sets the value automatically (in my program I use a random number) whereas in the child class I want to overload the method so they can input the number their self.

  4. #4
    Junior Member
    Join Date
    Oct 2010
    Posts
    21
    Thanks
    3
    Thanked 1 Time in 1 Post

    Default Re: Polymorphism issue

    Thought I'd update you on a couple of changes I made. In my parent class I adjusted the method I wanted to overload in the child classes to abstract with no parameters. This meant I could simply override it in one class, in the other I override it and have another method named the same thing but with an additional parameter. Effectively, I believe I have achieved the same thing I started off trying it, though I'm not sure if it is the most elegant way.

    Here's what I got:

    public abstract class Parent{
        abstract void setValue();     
    }
    public class One extends Parent{
        public void setValue(int inputvalue){
            value = inputvalue;
        }
     
    @Override
    public void setValue(){
    throw new UnsupportedOperationException("You tried to set the secret number incorrectly");
       }
    }

    EDIT: I have just realised one error with overloading and trying to use Polymorphism is that I cannot access the overloaded method as it is in a child class. Would creating two abstract methods in the base class with the only difference being the amount of parameters be good practice or not? All other methods are the same so I am wondering whether it is redundant to use Polymorhpism or not now.
    Last edited by LDM91; November 28th, 2010 at 11:42 AM.

  5. #5
    Super Moderator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,225
    Thanks
    176
    Thanked 817 Times in 760 Posts
    Blog Entries
    5

    Default Re: Polymorphism issue

    You can't overload an abstract function in a child class and use it abstractly via the parent the way it seems you would like. If you want to abstract them using a reference of the parent type, you should define each as abstract in the parent. It seems to me you might want to have a concrete no parameter method in the parent, and then an abstract for the child to implement (you can hide the no-parameter method using the protected modifier if you wish). Its hard to say if polymorphism is the best candidate for this without an example better than the one I posted.

  6. The Following User Says Thank You to copeg For This Useful Post:

    LDM91 (November 28th, 2010)

  7. #6
    Junior Member
    Join Date
    Oct 2010
    Posts
    21
    Thanks
    3
    Thanked 1 Time in 1 Post

    Default Re: Polymorphism issue

    Thank you for very much! The concrete no parameter method and abstract method with the parameter works perfectly. Sorry if the problem was difficult to understand I didn't quite understand the concept at first.

Similar Threads

  1. JButton issue, please help
    By Khoatic in forum AWT / Java Swing
    Replies: 3
    Last Post: November 19th, 2010, 10:19 PM
  2. Port issue?
    By Brt93yoda in forum Java Theory & Questions
    Replies: 3
    Last Post: October 13th, 2010, 04:28 PM
  3. [SOLVED] Calendar Issue
    By aussiemcgr in forum What's Wrong With My Code?
    Replies: 1
    Last Post: October 11th, 2010, 01:19 PM
  4. i do not know how to solve this issue
    By javastupi in forum What's Wrong With My Code?
    Replies: 2
    Last Post: January 20th, 2010, 07:28 PM
  5. Issues with Tomcat 6.0
    By sanyog24681 in forum Java Servlet
    Replies: 0
    Last Post: October 21st, 2008, 07:55 AM