Results 1 to 8 of 8
  1. #1
    goatjugsoup is offline Member
    Join Date
    May 2012
    Posts
    15
    Rep Power
    0

    Default Passing instance of derived class as base class

    Sorry, I didn't know how to describe it other than that in the title.

    Say if you had a base class and then you had several derived classes i.e. Animal > Cat, Dog, Fish and you had a function that would create an instance of one of the derived classes and return it based on some input could you go about the handling of the return variable like this?

    public animal doStuff(someInput)
    {
    //some code that either creates a cat, dog or fish, does somethings to it and then returns it
    }

    Just wondering if the data that is stored from the derived class would be kept or lost through doing it that way or not. As it is now in my head I think it would need to be converted to the specific child class type on the otherside.

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: Passing instance of derived class as base class

    Yes that could be done. If it were a static method, it would smell suspiciously like the Factory Pattern.

  3. #3
    awinston is offline Student
    Join Date
    Jul 2012
    Location
    United States
    Posts
    328
    Rep Power
    3

    Default Re: Passing instance of derived class as base class

    If you store the return of doStuff into a variable of type Animal, then it will only have access to the members of the Animal class. For example:

    Java Code:
    public class Animal
    {
      public void run()
      {
         ...
      }
    
    }
    
    public class Dog extends Animal
    {
      public void bark()
      {
         ...
      }
    
    }
    
    public class AnimalTest
    {
      public static void main(String[] args)
      {
        AnimalTest tester = new AnimalTest();
        Animal myAnimal = tester.returnDog();
        myAnimal.run();                                 //<---- okay because run is a method of the Animal class.
        myAnimal.bark();                               //<---- not okay because bark is not a method of the Animal class, even though myAnimal technically references a Dog
      }
    
      public Animal returnDog()
      {
         Animal dg= new Dog();
         return dg;
      }
    
    }
    However, if you then cast myAnimal into type Dog and store it into a variable of type Dog, you can now use the methods of type Dog. For example:

    Java Code:
    Dog myDog = (Dog)myAnimal;
    myDog.bark();                                        //<----now okay
    "Success is not final, failure is not fatal: it is the courage to continue that counts." - Winston Churchill

  4. #4
    goatjugsoup is offline Member
    Join Date
    May 2012
    Posts
    15
    Rep Power
    0

    Default Re: Passing instance of derived class as base class

    Thanks for the replies. Just to be 100% sure, returning dog from a function with return type animal then making sure to cast to type dog where the function was called will retain any dog variable data?

  5. #5
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: Passing instance of derived class as base class

    On the other hand...

    Java Code:
    public class TestAnimal {
       public static void main(String[] args) {
          Animal animal = Animal.getInstance(0);
          animal.speak();
          animal.move();
          
          animal = Animal.getInstance(1);
          animal.speak();
          animal.move();
       }
    }
    
    abstract class Animal {
       public abstract void move();
       public abstract void speak();
       
       public static Animal getInstance(int index) {
          switch (index) {
          case 0:
             return new Dog();
          case 1:
             return new Cat();
          default:
             String text = "index \"" + index +"\" does not correspond to a valid Animal";
             throw new IllegalArgumentException(text );
          }
       }
    }
    
    class Dog extends Animal {
    
       @Override
       public void move() {
          System.out.println("run");
       }
    
       @Override
       public void speak() {
          System.out.println("bark");
       }
       
    }
    
    class Cat extends Animal {
    
       @Override
       public void move() {
          System.out.println("Pounce");
       }
    
       @Override
       public void speak() {
          System.out.println("Meow");
       }
       
    }

  6. #6
    awinston is offline Student
    Join Date
    Jul 2012
    Location
    United States
    Posts
    328
    Rep Power
    3

    Default Re: Passing instance of derived class as base class

    [Previous Answer Under Reconsideration]
    Last edited by awinston; 08-01-2012 at 04:51 AM.
    "Success is not final, failure is not fatal: it is the courage to continue that counts." - Winston Churchill

  7. #7
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: Passing instance of derived class as base class

    Quote Originally Posted by awinston View Post
    Yes, as long as the Dog object is referenced by a variable of type Dog.
    No -- You're ignoring the magic of polymorphism!
    To reiterate, this is a situation that may be solved using polymorphism or generic classes. Anything to avoid too much casting which can give a program a bad smell.
    Last edited by Fubarable; 08-01-2012 at 04:57 AM.

  8. #8
    goatjugsoup is offline Member
    Join Date
    May 2012
    Posts
    15
    Rep Power
    0

    Default Re: Passing instance of derived class as base class

    alright, thanks again appreciate the replies, think i got it now

Similar Threads

  1. Replies: 7
    Last Post: 06-18-2012, 09:31 PM
  2. Replies: 1
    Last Post: 05-08-2012, 09:40 PM
  3. Help with passing String[] from GUI class to GUI class
    By X75TIGER75X in forum New To Java
    Replies: 9
    Last Post: 05-01-2012, 06:55 PM
  4. Replies: 8
    Last Post: 04-14-2010, 05:49 AM
  5. passing text from a class to a gui class
    By rob in forum AWT / Swing
    Replies: 2
    Last Post: 02-13-2009, 11:04 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
  •