Results 1 to 8 of 8
- 08-01-2012, 03:53 AM #1
Member
- Join Date
- May 2012
- Posts
- 15
- Rep Power
- 0
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.
-
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.
- 08-01-2012, 04:29 AM #3
Student
- Join Date
- Jul 2012
- Location
- United States
- Posts
- 328
- Rep Power
- 1
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:
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: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; } }
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
- 08-01-2012, 04:38 AM #4
Member
- Join Date
- May 2012
- Posts
- 15
- Rep Power
- 0
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?
-
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"); } }
- 08-01-2012, 04:42 AM #6
Student
- Join Date
- Jul 2012
- Location
- United States
- Posts
- 328
- Rep Power
- 1
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
-
Re: Passing instance of derived class as base class
Last edited by Fubarable; 08-01-2012 at 04:57 AM.
- 08-01-2012, 05:15 AM #8
Member
- Join Date
- May 2012
- Posts
- 15
- Rep Power
- 0
Similar Threads
-
Accessing an array from a class and passing to another class through main()
By coderz in forum New To JavaReplies: 7Last Post: 06-18-2012, 09:31 PM -
using an instance of the super class in the constructor of a sub class
By marcosol in forum New To JavaReplies: 1Last Post: 05-08-2012, 09:40 PM -
Help with passing String[] from GUI class to GUI class
By X75TIGER75X in forum New To JavaReplies: 9Last Post: 05-01-2012, 06:55 PM -
Dynamic loading of a class (passing class definition over the network)
By eddie-w in forum Advanced JavaReplies: 8Last Post: 04-14-2010, 05:49 AM -
passing text from a class to a gui class
By rob in forum AWT / SwingReplies: 2Last Post: 02-13-2009, 11:04 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks