• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Reference varibale question

 
Ranch Hand
Posts: 486
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

A reference variable can refer to any object of the same type as the declared
reference, or�this is the big one�it can refer to any subtype of the
declared type!



Can anyone please explain me the meaning of above sentence in detail with example
Please......
 
Bartender
Posts: 6663
5
MyEclipse IDE Firefox Browser Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
if Horse extends Animal and Animal is a concrete class the following will compile

Animal a = new Animal(); //#1
Animal a = new Horse(); // #2

Animal() at #1 is of the same type and at #2 Horse() is the subtype. This is what it means.

Now please quote the source of this question.
 
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
quote:
--------------------------------------------------------------------------------
A reference variable can refer to any object of the same type as the declared
reference, or�this is the big one�it can refer to any subtype of the
declared type!
--------------------------------------------------------------------------------

Can anyone please explain me the meaning of above sentence in detail with example
Please......
--------------------
Hi

A reference variable is one through which an object in the heap will be accessed.

For example:

class Animal{
public void eat(){
System.out.println("Animal's eat");
}
}
class Cat extends Animal{
}


Animal animal = new Cat();

There are three things to be noted:
1. The declaration of the reference variable 'animal'.
2. The creation of a new Cat object in the heap.
3. Assignment of the address to the animal reference variable.

If you see the above statement in the example,
 
Dinesh Tahiliani
Ranch Hand
Posts: 486
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks all for your reply. I got now..
 
Krishnamoorthy Vuyala Muralidharan
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
quote:
--------------------------------------------------------------------------------
A reference variable can refer to any object of the same type as the declared
reference, or�this is the big one�it can refer to any subtype of the
declared type!
--------------------------------------------------------------------------------

Can anyone please explain me the meaning of above sentence in detail with example
Please......
--------------------
Hi

A reference variable is one through which an object in the heap can be accessed.

For example:

class Animal{
public void eat(){
System.out.println("Animal's eat");
}
}
class Cat extends Animal{
System.out.println("Cat's eat");
}

Animal animal = new Animal(); // correct
Animal animal = new Cat(); // correct

Animal animal = new Car(); // incorrect

There are three things to be noted:
1. The declaration of the reference variable 'animal'.
2. The creation of a new Cat object in the heap.
3. Assignment of the address to the animal reference variable.

If you see the above statement in the example,
'animal' reference variable can reference to its own type (Animal) and also reference to any of its subtype(for example, Cat object).

Hope this helps.

Kind regards
Kris
 
reply
    Bookmark Topic Watch Topic
  • New Topic