-4

I currently read a java book and I am stuck at an example including this line :

Bird titi = new Swan();

where Swan is a subclass of Bird.

I could explain this as follow:

when java executes this line, it stores memory for the value titi, and the memory stored can contain an information for an object of type Bird. next, the second part of the line call the Swan constructor without any argument, which initializes the titi value.

if I am right, I can't explain why a Swan instance can be stored in a Bird type because Swan, as a subclass, contains more information than Bird. so I think I am something wrong. where?

and, extra-question : in which case this type of statement is useful?

thank you

lolveley
  • 1,659
  • 2
  • 18
  • 34
  • 1
    https://docs.oracle.com/javase/tutorial/java/IandI/polymorphism.html – luk2302 Aug 11 '17 at 13:14
  • 1
    Remember that a "variable" in Java is actually just a **reference** - so you're not allocating a block of memory to fit a `Bird` then having to squeeze all of a `Swan`s data in, you're making a new fixed-size reference to somewhere else. The data at that "somewhere else" is then defined when you invoke the object construction, so here it's made to be a `Swan`. – hnefatl Aug 11 '17 at 13:15

4 Answers4

1

You may have a method that only takes an instance of Bird. Since Swan is a Bird, you can use an instance of Swan and treat it as Bird.

That's the beauty of polymorphism. It allows you to change out the implementation of the class' internals without breaking the rest of your code.

Chinmay jain
  • 979
  • 1
  • 9
  • 20
1

Where it is calling new Swan(), it is creating a new Swan object in memory.

When the Swan object is assigned to the Bird variable (possible because a Swan is a sub type of Bird) the Bird variable simply has a pointer to the Swan in memory. Because titi is a verb, this object can now be accessed / treated like a bird for static time type checking and functionality... though it is always a Swan, and can be cast to a Swan to extended Swan functionality.

ControlAltDel
  • 33,923
  • 10
  • 53
  • 80
  • Nitpick: "Bird variable simply has a pointer to the Swan in memory" not pointer but reference, which JVM can use to locate object in memory (it doesn't need to be its current memory address). – Pshemo Aug 11 '17 at 13:23
1
Bird titi = new Swan();

is actually known as: programming to superclass but sometimes programming to interfaces

means:

titi is a Bird, pointing to a Swan reference... note that this is only valid because of inheritance... which means in this case that Swan is a class that extends the Bird class

why: it allows modifications in the code... you can latter decide not to use a Swan but an Owl so

Bird titi = new Swan(); will be replaced with Bird titi = new Owl(); and everything will work fine

programming to interfaces is more elegant because you can do:

IFly titi = new Swan();

which is valid if the Swan implemtents the interface IFly, so it is:

titi is something that can fly, pointing to a Swan ref.

ΦXocę 웃 Пepeúpa ツ
  • 47,427
  • 17
  • 69
  • 97
0
Bird titi = new Swan(); 

It's all about Polymorphism.

A Parent class/Interface type can hold it's Child class's Object.

Here Bird is Parent class/Interface and Swan is Child.

The same example is below :

List list = new ArrayList();

Here List is Parent Interafce and ArrayList it's Child class.

Mehraj Malik
  • 14,872
  • 15
  • 58
  • 85