-3

why do I have to write like this

animal a = new cat();
cat b = (cat)a;
b.makesound();
a.makesound();

but not like this

animal a = new cat();
animal b = new cat();
a.makesound();
b.makesound();

they give the same outputs

This is the entire code.

public class DownCasting 
{
    public static void main(String args[])
    {
        animal a = new cat();
        cat b = (cat)a;
        b.makesound();
        a.makesound();
    }
}


class animal
{
    public void makesound()
    {
        System.out.println('w');
    }
}


class cat extends animal
{
    public void makesound()
    {
        System.out.println("meow");
    }
}

so downcasting just give an object second name? or it has other uses?

Seyed Ali Roshan
  • 1,476
  • 1
  • 18
  • 37
  • 2
    Unless `makesound` is a property of `Animal`, you don't. We'd need to see more code, really. – Makoto Mar 13 '17 at 21:31
  • Please don't try to put code in a comment; as you can see it's totally unreadable. [edit] your question if you want to improve it. – azurefrog Mar 13 '17 at 21:33
  • @user7639356: if you [edit] to make that part of the question, it becomes more readable – serv-inc Mar 13 '17 at 21:33
  • 2
    @JAAAY You can't, actually, since [you cannot override static methods](http://stackoverflow.com/questions/2223386/why-doesnt-java-allow-overriding-of-static-methods). – azurefrog Mar 13 '17 at 21:40

3 Answers3

0

In the second variant, you create two cats.

In the first, you assign the additional name b to the already existing animal a.

so downcasting just give an object second name? or it has other uses?

You might want to have a list of animals, f.ex. a dog:

class dog extends animal
{
    public void makesound()
    {
        System.out.println("woof");
    }
}

could be combined in the same list with cats.

serv-inc
  • 35,772
  • 9
  • 166
  • 188
0

Casting is the process of forcefully making a variable behave as a variable of another type. If a class shares an IS-A or inheritance relationship with another class or interface, their variables can be cast to each other’s type.

public class DownCasting 
{
    public static void main(String args[])
    {
        animal a = new cat();
        cat b = (cat)a;
        b.makesound();
        a.makesound();
    }
}


class animal
{
    public void makesound()
    {
        System.out.println('w');
    }
}


class cat extends animal
{
    public void makesound()
    {
        System.out.println("meow");
    }
}

In the line animal a = new cat(); you are using a variable of type Animal to refer an object of type Cat, and a.makesound(); will print "meow" because with inheritance, the instance methods bind at runtime.

However, if you try to do cat b = a; (without casting) the Java Compiler will try to use a variable of type Cat to reference an object of type Animal, and that is not possible (you cannot use a variable of a sub-class to reference (execute) methods from a super-class), so it is necessary cast the variable to indicate to the java compiler that it can be sure that despite a is a variable of type Animal, is actually a Cat.

Let me give you an example to make this more clear:

I have your two classes with some modifications:

class animal
{
    public void makesound()
    {
        System.out.println('w');
    }

    void sleep(){
        System.out.println("ZZZ");
    }
}


class cat extends animal
{
    public void makesound()
    {
        System.out.println("meow");
    }
    void play(){
        System.out.println("I´m playing");
    }
}

both classes now have a method that the other class does not have.

Now let's use it:

public class DownCasting 
{
    public static void main(String args[])
    {
        animal a = new cat();
    }
}

If I do a.play(); it will give me a compilation error because Java searchs for the method play() inside the Animal Class and cannot find it... Why java does that? as I already said: "With inheritance, the instance methods bind at runtime", so if I want to call that method I have to cast the a variable... ((Cat)a).play();

But what happen if you try to do it the other way around?

Cat c = new Animal(); //This line will never compile...

So, I can cast it like this in order to make it compile:

Cat c = (Cat)new Animal();
c.play();

BUT, in runtime it will throw a java.lang.ClassCastException why?, because you cannot use a variable of a sub-class to reference (execute) methods from a super-class.

To sum up, remember that the type of the object reference variable and the type of the object being referred to may be different.

But there are rules on how different these can be.

To this topic I will recommend you OCA Java SE 7 Programmer I Certification Guide: Prepare for the 1ZO-803 exam specifically the chapter 6.

Dazak
  • 1,011
  • 2
  • 9
  • 17
-1

why do I have to write like this

to answer this question you need to know first what can make a sound? the cat or the animal?

you do this :

animal a = new cat();
a.makesound();

when the method makesound is declared in animal

if not then you need to cast

ΦXocę 웃 Пepeúpa ツ
  • 47,427
  • 17
  • 69
  • 97