Results 1 to 3 of 3
  1. #1
    gabriels is offline Member
    Join Date
    Feb 2011
    Posts
    4
    Rep Power
    0

    Default A question about inheritance / polymorphism

    Hi, i tried the following code:

    XML Code:
    <pre>
    
    public class Parent {
    
    }
    
    public class FirstChild extends Parent {
    
    }
    
    public class SecondChild extends Parent {
    
    }
    
    public class Other {
    
        public void doSomething(Parent obj) {
            // some code
        }
    
        public void doSomething(FirstChild obj) {
            // some code
        }
    
        public void soSomething(SecondChild obj) {
            // some code
        }
    
    }
    
    public class Another {
    
        public static void main(String args[]) {
    
            other = new Other();
    
            Parent first = new FirstChild();
            Parent second = new SeconChild();
    
            other.doSomething(first);
            other.doSomething(second);
    
        }
    
    }
    
    </pre>
    In the last two lines doSomething(Parent) is called, but i want it to act like a virtual function, to activate the right overloaded function depending on the parameter run-time type. I know i can define doSomething() in Parent, FirstChild and SecondChild classes and it should work, but it's important that it will be in Other class. Is it possible?

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    14,048
    Blog Entries
    7
    Rep Power
    23

    Default

    Quote Originally Posted by gabriels View Post
    Hi, i tried the following code:
    Java Code:
    public class Another {
    
        public static void main(String args[]) {
    
            other = new Other();
    
            Parent first = new FirstChild();
            Parent second = new SeconChild();
    
            other.doSomething(first);
            other.doSomething(second);
    
        }
    
    }
    In the last two lines doSomething(Parent) is called, but i want it to act like a virtual function, to activate the right overloaded function depending on the parameter run-time type. I know i can define doSomething() in Parent, FirstChild and SecondChild classes and it should work, but it's important that it will be in Other class. Is it possible?
    Overloading is a compiler issue; all that the compiler knows is that your variables first and second are Parent objects (it doesn't think much further) so it calls the method that takes a Parent parameter. Maybe the Visitor pattern (also called the Double Dispatch pattern) can be of help, i.e. your other class is the Visitor and calls a Parent object that calls the correct method in your Visitor again according to its real type.

    kind regards,

    Jos
    The only person who got everything done by Friday was Robinson Crusoe.

  3. #3
    gabriels is offline Member
    Join Date
    Feb 2011
    Posts
    4
    Rep Power
    0

Similar Threads

  1. Still trying to grasp Polymorphism and Inheritance
    By tashimoto in forum New To Java
    Replies: 15
    Last Post: 12-06-2010, 09:35 PM
  2. Replies: 45
    Last Post: 05-25-2010, 08:32 PM
  3. question about polymorphism
    By becky in forum New To Java
    Replies: 4
    Last Post: 02-11-2009, 11:59 PM
  4. inheritance and polymorphism
    By tester in forum Eclipse
    Replies: 1
    Last Post: 12-21-2008, 05:58 AM
  5. Relation between Polymorphism and Inheritance
    By janakiram.attuluri in forum Advanced Java
    Replies: 1
    Last Post: 12-27-2007, 12:32 AM

Posting Permissions

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