Results 1 to 7 of 7
- 02-20-2009, 06:43 AM #1
Member
- Join Date
- Feb 2009
- Posts
- 5
- Rep Power
- 0
How2assign all instance vars of a subclass var to be same as a superclass var?
I have a variable of a specific class type which is instantiated via a system-call (method). Unfortunately, I really need to override one of the class level methods. I am a relative beginner, so not sure of the easiest way to do this. I cannot simply alter the specific class API because it is a standard API. I thought the easiest way to do this would be to create a subclass with an overridden method, and then cast a subclass variable back to the superclass. For instance:
This *will* give me a variable "a" of class Animal, whose makeNoise() method has been overridden and is that of a Dog.Java Code:public class Animal { public void makeNoise() { // code to make noise } } public class Dog extends Animal{ @Override public void makeNoise() { // different code to make noise } } Dog d = new Dog(); Animal a = (Animal) d;
However, this is my problem: In my case I can't just instantiate the Dog variable d, because I am being handed this variable from the system and the only thing the system is going to hand me is a variable of class Animal, and there is no way to change it. I am not allowed to use:
So, is there another way to assign a subclass variable to be equal to a superclass variable, when you know they really are identical except for the one overridden method. Or is there a way to assign all the instance variables of a subclass variable to be the same as a superclass variable in one easy step? How would a Java expert search for an answer to this kind of question - I really couldn't figure out what the best search descriptor would be, and couldn't find anything helpful...Java Code:Animal c = getAnimalMethodFromSystem(); Dog d = c; // *this causes error....* Animal a = (Animal) d;
Thanks for any guidance,
Jim
- 02-20-2009, 07:16 AM #2
Senior Member
- Join Date
- Jan 2009
- Posts
- 671
- Rep Power
- 5
I don't understand your problem. Are you trying to force arbitrary animals to be Dogs? It sort of sounds like you want the arbitrary Animal to be itself, except that you want a dog sound to happen when makeNoise is called. If so, what you want to do is construct a Dog from the Animal:
Java Code:class Dog extends Animal { Animal c; public Dog(Animal c) { this.c = c; } ...animal method 1.. { return c....animal method 1... } ....animal method 2... { return c...animal method 2... } public void makeNoise() { ...do a dog noise... } }
- 02-20-2009, 07:27 AM #3
The term you need to use in your search is polymorphism.
There is no need for a cast when assigning a Dog reference to a variable of type Animal. Every Dog is-a Animal.
The instance method of the actual class of the object will be invoked, not the instance method of the type of the variable reference. Also, since you will never expect to have an object of class Animal but only objects of specific subtypes of Animal, this class should be abstract and contain an abstract method that enforces implementation by all concrete subclasses.
dbJava Code:public abstract class Animal { protected abstract void makeNoise(); public static void main(String... args) { Animal one = new Dog(); Animal two = new Cat(); System.out.print("Dog noise: "); one.makeNoise(); System.out.print("Cat noise: "); two.makeNoise(); } } class Dog extends Animal { public void makeNoise() { System.out.println("Woof!"); } } class Cat extends Animal { public void makeNoise() { System.out.println("Meow!"); } }
- 02-20-2009, 05:19 PM #4
Member
- Join Date
- Feb 2009
- Posts
- 5
- Rep Power
- 0
Thank you for the comments, but I don't think I have adequately explained the "complexity". I have a variable a of type Animal (which is given to me, as is, formt he system), and I want to override one method of type animal, but otherwise leave all the other instance variables and methods of that variable the same as when they are "handed" to me by the system. Since the variable is given to me, and its class is fixed and system-API specific, I can't change it.
RE:
By doing this, I have a Dog class which is identical to the Animal class except for: 1) one overridden method (which is what I wanted!), and 2) the Dog class has an instance variable which is also an Animal. From an object point of view, this doesn't make any sense that your variable of type Dog (which is an Animal) would also have an instance variable of type Animal. Moreover, when I go to use my variable of type Dog, and I want to reference "dog.someAnimalInstanceVar", I would have to use instead "dog.dummyAnimal.someAnimalInstanceVar", which I can't do.It sort of sounds like you want the arbitrary Animal to be itself, except that you want a dog sound to happen when makeNoise is called. If so, what you want to do is construct a Dog from the Animal:
class Dog extends Animal {
Animal dummyAnimal;
public Dog(Animal c) {
this.dummyAnimal = c;
}
@Override
public void makeNoise() {
...do a dog noise...
}
RE:
I cannot change Animal, it is a superclass part of the system API. I can only extend it, and by so doing then I have the problem casting back I explained above.since you will never expect to have an object of class Animal but only objects of specific subtypes of Animal, this class should be abstract and contain an abstract method that enforces implementation by all concrete subclasses
RE:
I realize that a cast is not necessary, but without the castThere is no need for a cast when assigning a Dog reference to a variable of type Animal. Every Dog is-a Animal.
I will get the Animal instance method makeANoise(), which I don't want.Java Code:Animal a = d; //instead of //Animal a = (Dog) d;
This seems like it ought to be easy, but I don't get it.
Thanks again,
Jim
- 02-20-2009, 05:54 PM #5
Senior Member
- Join Date
- Sep 2008
- Posts
- 564
- Rep Power
- 5
why does dog have an animal in it?
no you won't. this is the point of subclasses.I will get the Animal instance method makeANoise(), which I don't want.
- 02-20-2009, 08:53 PM #6
Senior Member
- Join Date
- Jan 2009
- Posts
- 671
- Rep Power
- 5
If you use the facade approach I pointed out above, you need to override ALL the methods, not just the makeNoise method. However, all the other overridden methods (or member variabel if they are public) will simply reference the equivalent dummyAnimal's methods.
If you can't change the Animal API, then I don't see that you have any choice but to make Dog be a facade. Don't worry whether it makes sense that Dog has-a Animal. A Dog facade is not an actual Dog.Java Code:public boolean doSomethingBesideNoise() { return dummyAnimal.doSomethingBesidesNoise(); }
- 02-22-2009, 06:04 AM #7
Member
- Join Date
- Feb 2009
- Posts
- 5
- Rep Power
- 0
Ahhhh, now I see what you mean, Toadaly!
I missed the point earlier. A very clever solution, Thanks.
I may have trouble implementing however, since the superclass I would be overriding has around 30 methods of its own plus over a 100 methods it inherits from its superclasses, plus numerous constants... But I will see if I can make it work.
Jim
Similar Threads
-
superclass and subclass
By mr idiot in forum New To JavaReplies: 19Last Post: 01-03-2009, 07:29 AM -
Packing vars
By Supamagier in forum Advanced JavaReplies: 6Last Post: 09-20-2008, 12:26 AM -
Parsing a superclass object to subclass object dynamicly
By Andrefs in forum Advanced JavaReplies: 1Last Post: 07-22-2008, 04:27 PM -
which class is superclass and subclass?
By java_fun2007 in forum New To JavaReplies: 0Last Post: 12-11-2007, 08:55 PM -
vars and if sentences in XSL-FO
By Alan in forum XMLReplies: 1Last Post: 05-31-2007, 02:24 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks