Results 1 to 7 of 7
  1. #1
    gymshoe is offline Member
    Join Date
    Feb 2009
    Posts
    5
    Rep Power
    0

    Default 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:
    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;
    This *will* give me a variable "a" of class Animal, whose makeNoise() method has been overridden and is that of a Dog.

    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:

    Java Code:
    Animal c = getAnimalMethodFromSystem();
    Dog d = c;  // *this causes error....*
    Animal a = (Animal) d;
    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...

    Thanks for any guidance,
    Jim

  2. #2
    toadaly is offline Senior Member
    Join Date
    Jan 2009
    Posts
    671
    Rep Power
    6

    Default

    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...
      }
    }

  3. #3
    DarrylBurke's Avatar
    DarrylBurke is online now Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,237
    Rep Power
    19

    Default

    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.
    Java 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!");
       }
    }
    db

  4. #4
    gymshoe is offline Member
    Join Date
    Feb 2009
    Posts
    5
    Rep Power
    0

    Default

    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:
    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...
    }
    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.

    RE:
    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
    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.

    RE:
    There is no need for a cast when assigning a Dog reference to a variable of type Animal. Every Dog is-a Animal.
    I realize that a cast is not necessary, but without the cast
    Java Code:
    Animal a = d; //instead of 
    //Animal a = (Dog) d;
    I will get the Animal instance method makeANoise(), which I don't want.

    This seems like it ought to be easy, but I don't get it.
    Thanks again,
    Jim

  5. #5
    emceenugget is offline Senior Member
    Join Date
    Sep 2008
    Posts
    564
    Rep Power
    6

    Default

    why does dog have an animal in it?

    I will get the Animal instance method makeANoise(), which I don't want.
    no you won't. this is the point of subclasses.

  6. #6
    toadaly is offline Senior Member
    Join Date
    Jan 2009
    Posts
    671
    Rep Power
    6

    Default

    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.

    Java Code:
    public boolean doSomethingBesideNoise() {
      return dummyAnimal.doSomethingBesidesNoise();
    }
    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.

  7. #7
    gymshoe is offline Member
    Join Date
    Feb 2009
    Posts
    5
    Rep Power
    0

    Default

    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

  1. superclass and subclass
    By mr idiot in forum New To Java
    Replies: 19
    Last Post: 01-03-2009, 07:29 AM
  2. Packing vars
    By Supamagier in forum Advanced Java
    Replies: 6
    Last Post: 09-20-2008, 12:26 AM
  3. Replies: 1
    Last Post: 07-22-2008, 04:27 PM
  4. which class is superclass and subclass?
    By java_fun2007 in forum New To Java
    Replies: 0
    Last Post: 12-11-2007, 08:55 PM
  5. vars and if sentences in XSL-FO
    By Alan in forum XML
    Replies: 1
    Last Post: 05-31-2007, 02:24 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
  •