Home > OS >  Why does my child class object use the parent classes variable but not methods when upcasting?
Why does my child class object use the parent classes variable but not methods when upcasting?

Time:12-25

Why does "beagle" use the "Dog" execution of speak() but uses the Animal classes legs variable (seems inconsistent)? And why do the constructors get run twice each if I only create 2 objects? (seen in output below)

This was an upcasting/downcasting example originally so I'm trying to change the type of the object aka Animal dog = new Dog(); instead of Dog dog = new Dog();

And in Animal dog = new Dog(), what does "Animal" represent and what does Dog() represent? (I believe one is the Object Type)

// Online Java Compiler
// Use this editor to write, compile and run your Java code online

class Animal {
    int legs;

    Animal(){
        //this.legs = 5;
        System.out.println("-- Animal Constructor called --");
    }

    void speak(){
        System.out.println("Speak!");
    }
}

class Dog extends Animal {

    int legs;

    Dog(){
        this.legs = 4;
        System.out.println("-- Dog Constructor called --");
    }
    void speak(){
        System.out.println("Bow wow!");
    }
}

class Main {
    public static void main(String[] args) {

        Animal beagle = new Dog();
        Dog chew_wawa = new Dog();
//        Animal animal = new Animal();


        System.out.println(beagle.legs);
        System.out.println(chew_wawa.legs);
//        System.out.println(animal.legs);


        beagle.speak();
        chew_wawa.speak();
//        animal.speak();
    }

}

Why is the output for this code:

-- Animal Constructor called --
-- Dog Constructor called --
-- Animal Constructor called --
-- Dog Constructor called --
0
4
Bow wow!
Bow wow!

Process finished with exit code 0

CodePudding user response:

When you create an object of a subclass (such as Dog) and assign it to a superclass reference (such as Animal), you are upcasting the object. Upcasting allows you to treat an object of a subclass as if it were an object of the superclass. In this case, when you call the speak() method on the beagle object, it will use the implementation of the speak() method in the Dog class, because that is the actual type of the object. However, when you access the legs variable of the beagle object, it will use the value of the legs variable defined in the Animal class, because that is the type of the reference variable.

 public static void main(String[] args) {
    // Upcasting: creating a Dog object and assigning it to an Animal reference
    Animal beagle = new Dog();

    // Downcasting: casting the Animal reference back to a Dog reference
    Dog chew_wawa = (Dog)beagle;
    
    // Continue with the print statements...
}

CodePudding user response:

  1. Why do the constructors get run twice each if I only create 2 objects ?

Java adds super(); on the first line of every constructor which calls the parent class constructor - in your case Animal(). So whenever, you create an object for a class, it's parent class constructor will be called first and then the child class constructor.

  1. Why does "beagle" use the "Dog" execution of speak() ?

In java, object creation happens in heap memory. In the case of Animal animal = new Dog(); animal is a reference type and new Dog(); is the object to which memory is allocated in heap. beagle uses the Dog version of speak() because the animal refers to an Dog object and hence Dog version is invoked. This concept is referred as enter image description here

  1. But uses the Animal classes legs variable (seems inconsistent)?

animal reference type refers a Dog() object, which has both parent and child version of the variable (legs). Which version to use, depends on the reference type (in your case animal). This concept is called variable hiding

  • Related