#Can someone explain me Superclass x = new Subclass() means?

1 messages · Page 1 of 1 (latest)

still walrus
#

Sorry for so much spam these days.

Okay , so I trying to understand upcasting and downcasting and I can't seem to click how come a parent class reference is able to access it's own methods and methods of the child which the child has overriden?

Like in this code ,

class Animal{
    void anotherMethod(){
        System.out.println("Hello in Animal");
    }
    void makeSound(){
        System.out.println("Make sound in Animal");
    }
}

class Deer extends Animal{
    public Deer(){
        System.out.println("Hello , in Constructor");
    }
    void color(){
        System.out.println("The color is Red");
    }
    void makeSound(){
        System.out.println("Deer noise");
    }
}

public class Hello{
    public static void main(String[] args) {
      Animal animal = new Deer(); // What does this line mean?
      animal.anotherMethod();
      animal.makeSound();
    }
}

If I have to explain , ig , a superclass is always to access methods of it self and it's subclass that the subclass has overriden

But where I am getting is how is the superclass accessing those methods?

thin juncoBOT
#

<@&987246399047479336> please have a look, thanks.

toxic cave
#

u need to differentiate a few things here

#

first of all, dont mix variables with objects

#

a variable refers to an object

#

its like a pointer

#

maybe think about it like a phone number of a person

#

while the actual object is the person

#

and the variable just their phone number

#

the actual objects are what u create with new

#

the variable is on the left

#

in fact, this here Animal animal = new Dog() are 3 steps

#
  1. create a variable called animal which is allowed to point to anything we consider an Animal
#
  1. create a new dog object
#
  1. let the animal variable point/refer to that dog object
#

pay attention to the strict differentiation of variable and object

#

animal is not an object, its the variable

#

next, u have to learn sth about inheritance

#

inheritance is an "is-relationship"

#

this means that a Dog is an Animal

#

(which intuitively makes sense)

#

when i said earlier that Animal animal is a variable allowed to point to anything we consider an Animal, this includes dogs and cats bc they are Animal after all

#

so what the code setup is a variable called animal and it currently points to a dog object

#

theoretically this means that animal.bark() is possible as the object this variable points to is a Dog after all, and dogs do bark

#

for various reasons - mostly to protect u from bugs, java doesn't allow it though

#

the idea here is "yeah, its a dog, i know, but perhaps tomorrow u change it to a cat, so lets be safe"

#

also, there are plenty of situations where it wouldnt be easy for the compiler to reall, know whats behind a variable

#

example:

#
Animal animal = pickRandomAnimalFromShelter();
#

maybe the method returns dogs on mondays, cats on tuesdays and when the stars align u get fish

#

so despite animal perhaps pointing to a Dog when u run it, its not possible for the compiler to guarantee its a dog before u actually run the code

#

to get out of this dilemma and provide a consistent ajd not-confusing experience, java designers decided that the compiler must not "look behind" a variable

#

and only allow usage of what that type provides, even if what's behind the variable can actually do more than just that

#

its important to understand that
Animal animal = new Dog() did in no way change the dog to a dog that can do less or one that isn't truly a dog anymore

#

the object is still a full dog

#

its just that viewing it through the lenses of the variable only shows u parts of it

#

this is also the point where it's worth mentioning that u can "restore" this type info via cast

#
Dog asDog = (Dog) animal;
#

this means "shutup java, trust me. there's a dog behind that variable. so let me do dog stuff with it"

#

and it will crash ur program hard if it's actually not a dog but a cat

#

but once again, the object never changed. it was a dog and still is a dog now

#

👍

lethal horizon
#

and at that point you probably want to look at instanceof as well

#

which lets you test if an object is of a given type:

if (animal instanceof Dog) {
    // binary operator returning true or false
}
still walrus
# toxic cave inheritance is an "is-relationship"

i read about something like , in java are various type of relationship , like Is-A , has-A etc..idk what are these called ?
Can you point it to me

Also , thanks for the explanation ,as far as I know , we created a reference to the Object of type Dog () and that reference is deciding what we can access , so we can access both methods common in dog and animal as well as methods in animal only

lethal horizon
still walrus
#

I think there was also something called association as well

toxic cave
#

this stuff isn't really standardized terminology. and most of them are just English words

magic shore
#

Read about sealed class too, to be exhaustive