Pangu
Pangu

Reputation: 3819

What does it mean in JAVA when you create an object of some type, but declared as another type?

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?

Upvotes: 3

Views: 430

Answers (5)

Ruchira Gayan Ranaweera
Ruchira Gayan Ranaweera

Reputation: 35557

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)

Upvotes: 1

Peter Walser
Peter Walser

Reputation: 15706

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.
}

Upvotes: 2

Alexander Suraphel
Alexander Suraphel

Reputation: 10613

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.

Upvotes: 1

ioreskovic
ioreskovic

Reputation: 5699

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 :)

Upvotes: 3

Rahul
Rahul

Reputation: 45060

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.

Upvotes: 3

Related Questions