3

I'm really confused about this, and for some reason, I don't seem to get it. Maybe I'm overthinking it or just plain idiot.

But anyway, here's a sample code:

AkitaDog balto = new Dog();

I really don't get want that means, at least not in a sense where I can explain it to someone else.

If you provide me with this code, then I understand:

AkitaDog balto = new AkitaDog();

But in the first code, is it basically saying you have an AkitaDog type named balto, but you're making a Dog object out of it?

What does this also mean in terms of the methods balto inherits from Dog?

Can someone please give some simple examples to help understand it?

halfer
  • 19,824
  • 17
  • 99
  • 186
Pangu
  • 3,721
  • 11
  • 53
  • 120
  • 1
    See http://docs.oracle.com/javase/tutorial/java/concepts/inheritance.html –  Nov 25 '13 at 08:16
  • Have you read about `polymorphism`, `inheritance` and `object references in java` ? – vikingsteve Nov 25 '13 at 08:20
  • 1
    I believe you meant to say something different. You probably meant to say `Dog balto = AkitaDog()` because I *assume* that in your case `AkitaDog` is a class which descends from `Dog` (in other words, `AkitaDog` is a `Dog` with more properties than just a `Dog`). From your example, it seems like you meant it the other way around, that `Dog` is somehow an `AkitaDog` with its particular properties. – stackular Nov 25 '13 at 08:24
  • possible duplicate of [What does it mean to "program to an interface"?](http://stackoverflow.com/questions/383947/what-does-it-mean-to-program-to-an-interface) – Raedwald Nov 25 '13 at 08:26
  • 1
    @stackular you are right....that's part of why i was confused :) – Pangu Nov 25 '13 at 08:28

5 Answers5

3

This concept is called Subtype Polymorphism.

Subtyping (also subtype polymorphism or inclusion polymorphism) is a form of type polymorphism in which a subtype is a datatype that is related to another datatype (the supertype) by some notion of substitutability, meaning that program elements, typically subroutines or functions, written to operate on elements of the supertype can also operate on elements of the subtype.

You can have a look at this example for more clarity on it.

enter image description here

The type "bird" has three subtypes "duck", "cuckoo" and "ostrich". Conceptually, each of these is a variety of the basic "bird" that inherits many "bird" characteristics but has some specific differences.

Rahul
  • 44,383
  • 11
  • 84
  • 103
  • Good answer. The other answers all mention inheritance. This is not wrong, but the key concept here is the sub-typing in accordance with the variable's polymorphism. – Seelenvirtuose Nov 25 '13 at 08:26
  • So does that mean if you do this: bird birdie = new cuckoo(), then birdie is a bird type and inherits all the properties that means it a cuckoo? – Pangu Nov 25 '13 at 08:37
3

Think of it as this way.

You have a FRUIT. And you also have APPLE and ORANGE.

Now, imagine someone asks you for a fruit. Since a FRUIT is an abstract term and can mean many things, you have to give him something he can actually eat, an actual fruit, in your case, an apple or orange.

That's why you can write:

Fruit appleFruit = new Apple();

Since apple IS A KIND OF fruit.

Similar principle is applied with Interfaces/Abstract classes and concrete classes implementing them :)

ioreskovic
  • 5,531
  • 5
  • 39
  • 70
  • So then that means appleFruit of type Fruit inherits all the properties of Apple....or the other way around??......:) – Pangu Nov 25 '13 at 08:32
  • Yes, any `Apple` will inherit the set of properties from `Fruit`. Also, it can change them, and it can expand them. – ioreskovic Nov 25 '13 at 08:39
  • thank you so much...I think i get it now....so that means when I did this: AkitaDog balto = new Dog().......it's actually wrong?...it should be the other way around? – Pangu Nov 25 '13 at 08:42
  • Exactly. If someone asks you for an apple, you cannot give him an abstract fruit :) – ioreskovic Nov 25 '13 at 09:28
2
class Plant { }
class Fruit extends Plant { }
class Banana extends Fruit { }

Banana is a Fruit, and thus also a Plant.

Any of these assignments will work:

Banana banana = new Banana();
Fruit fruit = new Banana();
Plant plant = new Banana();

The variable type is how you look at the object. Some specifics will be hidden if the variable type is not the exact type, but this allows to generalize in a way we do not need to know the exact type:

class FruitBowl {
   add(Fruit fruit); // allows to add Banana, Kiwi, Apple etc.
}
Peter Walser
  • 15,208
  • 4
  • 51
  • 78
1

You must learn about inheritance and polymorphism in Java.

What does this also mean in terms of the methods balto inherits from Dog.

It means balto acquires the attributes of Dog. Then balto is a type of(one representation for a Dog)

Ruchira Gayan Ranaweera
  • 34,993
  • 17
  • 75
  • 115
1

Variables in Java are just references to objects in memory. A Dog variable references an object of type Dog etc.. But in cases of inheritance references of a base type can hold references to objects of a derived class. This can happen without a problem because all public methods from the base class are also available in an object of a derived class.

Alexander Suraphel
  • 10,103
  • 10
  • 55
  • 90