Results 1 to 3 of 3
  1. #1
    robw is offline Member
    Join Date
    Feb 2017
    Posts
    1
    Rep Power
    0

    Default Subject different from the reference

    Hello everyone,
    I do not understand why in the second example the following link
    https://www.tutorialspoint.com/java/java_overriding.htm

    It uses the expression:
    XML Code:
    Animal b = new Dog (); // Animal reference but Dog object


    Why not just write Dog b = new Dog (); ??

  2. #2
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    13,541
    Rep Power
    26

    Default Re: Subject different from the reference

    Because you may be interested in any Animal, not specifically a Dog.

    SO you might have a method like:
    Java Code:
    public void doSomethingWithAnimal(Animal a) {
        a.move();
    }
    You could pass an Animal object into this, or a Dog object. Either way it will call the correct method.

    That link is just trying to show you how overriding works.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  3. #3
    SurfMan's Avatar
    SurfMan is offline Godlike
    Join Date
    Nov 2012
    Location
    The Netherlands
    Posts
    1,991
    Rep Power
    8

    Default Re: Subject different from the reference

    This is especially useful in the cases where you have lots of classes that share the same base class, and a function where it doesn't matter what the implementation is. (This concept is used a lot in Collections too: I want a List, but the implementation can differ).

    I have used the classes from the tutorial here, but I added "abstract" to the Animal class. This is quite logical, since you can never have an Animal. It's ALWAYS a subclass of Animal that you instantiate, never an Animal itself.

    For example, I built a zoo. You can see I can add many animals to the list. I declared the List to contain only objects of the Animal type, Consequently, I can only execute the Animal methods on them, and not the individual methods (unless I cast them, but that's a whole different story :))
    Java Code:
    public abstract class Animal {
       public abstract void move();
       public abstract void makeSound();
    }
    
    class Dog extends Animal {
       public void move() {
          System.out.println("Dogs can walk and run");
       }
    
       public void makeSound() {
          System.out.println("Bark!");
       }
    }
    
    class Cat extends Animal {
       public void move() {
          System.out.println("Cat can walk, climb and run");
       }
    
       public void makeSound() {
          System.out.println("Miauw!");
       }
    }
    
    class Goldfish extends Animal {
       public void move() {
          System.out.println("Fish swim!");
       }
    
       public void makeSound() {
          //do nothing here
       }
    
    }
    
    
    List<Animal> zoo = new ArrayList();
    
    Animal dog = new Dog();
    Animal cat = new Cat();
    Animal fish = new Goldfish();
    
    zoo.add(dog);
    zoo.add(cat);
    zoo.add(fish);
    
    for( Animal an: zoo) {
       an.move();
       an.makeSound();
    }
    "It's not fixed until you stop calling the problem weird and you understand what was wrong." - gimbal2 2013

Similar Threads

  1. Replies: 0
    Last Post: 10-07-2011, 07:50 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
  •