1

For you, this might be very simple but I have no idea what the difference is.I just want to know the difference between these two codes. Suppose I have some codes as described below.

The first class is Animal which will be the Superclass

public class Animal {

    private String name;
    private int weight;
    private String sound;

    public void setName(String name){
        this.name = name;
    }

    public String getName(){
        return name;
    }

    public void setWeight(int weight){
        if(weight > 0){
            this.weight = weight;
        } else {
            System.out.println("Weight must be bigger than 0");
        }
    }

    public int getWeight(){
        return weight;
    }

    public void setSound(String sound){
        this.sound = sound;
    }

    public String getSound(){
        return sound;
    }
}

The second class is Dog which extends the class Animal

public class Dog extends Animal {

    public void digHole(){
        System.out.println("Dig a hole");
    }

    public Dog(){
        super();

        setSound("bark");
    }
}

The last class is the WorkWithAnimals which will print the output

public class WorkWithAnimals {

    public static void main(String args[]){

        Dog fido = new Dog();

        fido.setName("Dadu");
        System.out.println(fido.getName());

        fido.digHole();
        fido.setWeight(-1);
    }
}

My question is, what is the difference between Animal fido = new Dog() and Dog fido = new Dog() ?

Since Dog already extends Animal, why do we have to write the code like Animal fido = new Dog() ?

Both of them print the same result, don't they?

beresfordt
  • 5,088
  • 10
  • 35
  • 43
edgards
  • 99
  • 8

6 Answers6

2

The class "Animal" will not include digHole, so you should get a compile time error; thus:

Animal fido = new Dog();
fido.dighole(); //<--compile time error

whereas,

Dog fido = new Dog();
fido.dighole(); 

will be fine.

b degnan
  • 672
  • 1
  • 14
  • 31
  • Depends on how strict your compiler is. I have everything to be very restrictive. Give him a better solution or explanation. – b degnan Dec 11 '15 at 16:09
1
Animal fido = new Dog()

And

Dog fido = new Dog()

Are both referring the same object, Dog. OOP allows you create variable type of superclass that reference its child. It is purpose of abstraction. If you have no idea what that means, you should find a java book and read it.

OPK
  • 4,120
  • 6
  • 36
  • 66
1

imagine this :

public void sleep(Animal a){
    a.sleep();
}

with Animal as parameter type ,i can pass it a Cat,Dog or anything that extends Animal.

but if i wrote this:

public void sleep(Dog d){
      d.sleep();
  }

Now i'm restricted to Dog type

Also in the first case i don't have to know about how various subclasses of Animalimplement sleep().

Ramanlfc
  • 8,283
  • 1
  • 18
  • 24
0

If you define your instance fido like this:

 Animal fido = new Dog()

Then you can call on fido all methods that are defined on Animal. However you cannot make a call like this without casting:

fido.digHole(); // this will not compile
((Dog)fido).digHole(); // this will work
hotzst
  • 7,238
  • 9
  • 41
  • 64
0

The difference is related to how you are going to use the instances in the future. For example: If you want to add elements to a collection, you will most probably will use generics to avoid types casting.

Or for more complex uses, look for GoF Design Patterns (specially strategy) and why using an interface (in this case super class) improves code design for readability and flexibility.

Edson
  • 61
  • 4
0

This is the basis of OOP, can take this example as in real life where we have animals that case represented For the Animal class and we have a dog, the dog is an animal, has all attributes of a generic animal such as a monkey or kangaroo, but these three animals each have their way of doing things as much as there are things that only one of them does!

Ivan Vilanculo
  • 648
  • 1
  • 11
  • 25