Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why do you need to list two object types when you instantiate an object?

Tags:

java

object

If I have JungleCat as a subclass of Cat (JungleCat extends Cat), and then I say:

JungleCat cat1 = new JungleCat();
Cat cat2 = new Cat();
Cat cat3 = new JungleCat();
JungleCat cat4 = new Cat(); //this one is illegal, right?
JungleCat cat5;

I'm wondering what are the object types of cat1, cat2, cat3, cat4, and cat5? I'm also wondering why there's redundancy in instantiating an object: why do you need to list two object types when you instantiate an object.

I'm sorry if this is a really basic question, but it's the sort of thing where I just want to know once and for all and with a good answer with good reasoning, which I know I can expect here (and not Yahoo Answers, etc) :P

like image 287
bob Avatar asked Dec 14 '12 05:12

bob


People also ask

How do we instantiate an object?

Instantiating a ClassThe new operator requires a single, postfix argument: a call to a constructor. The name of the constructor provides the name of the class to instantiate. The constructor initializes the new object. The new operator returns a reference to the object it created.

Why do we initialize objects in Java?

Initializing an object means storing data into the object. Let's see a simple example where we are going to initialize the object through a reference variable. We can also create multiple objects and store information in it through reference variable.

Can an object have two classes?

The only way to pass the same object to two functions of different classes is if one class inherits from the other, and the Object is an instance of the inherited class.

How do you instantiate a class object in Java?

Java provides the new keyword to instantiate a class. We can also instantiate the above class as follows if we defining a reference variable. We observe that when we use the new keyword followed by the class name, it creates an instance or object of that class.


1 Answers

In the below statement: -

JungleCat cat1 = new JungleCat();

You can break it up in two parts: -

JungleCat cat1;  // This creates a reference of type `JungleCat`
cat1 = new JungleCat();   // This creates an object of type `JungleCat`.

Now, you are making the cat1 reference, point to the JungleCat object. References are nothing but a link to the object that is created, so that you can access them.

You can also create object just like this: -

new JungleCat();   // Will create an unnamed object

But, in the above case, you will only be able to use the methods and properties at the place where you have instantiated. But, later on, since you have no reference to access that object, you can't access it's properties either.


Now, let's move ahead to the 2nd statement: -

Cat cat = new JungleCat();

Here, as you can guess, you have reference of type Cat - Super Class, and an object of type JungleCat. This is what we call Polymorphism.

So, basically, you can create a reference of any super type, and make it point to an object of any subtype. This is pretty simple to understand - "Since a JungleCat is a Cat only. So, you can always have a Cat reference point to JungleCat".

This is not true the other way round. For e.g.: -

JungleCat ref = new Cat();

Now this is not valid. Because a Cat is not neccessarily a JungleCat. It can be any other Cat. So, you can't have your JungleCat reference point to a Cat object.


Now here's your actual concern: -

I'm wondering what are the object types of cat1, cat2, cat3, cat4, and cat5

Well, cat1, cat2.. are not objects, but are references pointing to some objects. And you can infer from the above explanation, the reference type of each of them.

Object types are the type used on the RHS of the object creation statement. The type used with new keyword, is the type of the Object. You can have different types of reference pointing to the same object type.

So, you can have both cat1, and cat2 references pointing to the same object type.

like image 180
Rohit Jain Avatar answered Oct 24 '22 20:10

Rohit Jain