• 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 variable casting

 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi friends ,

In the following code , in the cast class , i have explicitly type-casted dog reference to Animal type . But still when i invoke , a1.makenoise () , Dog class is invoked , not the Animal class . Can anyone explain the reason please ??

Thanks in advance .........

class Animal
{
void makenoise()
{
System.out.println("in animal : makenoise ");
}
}
class Dog extends Animal
{
void makenoise()
{
super.makenoise();
System.out.println("In dog : makenoise ");
}
void playdead ()
{
System.out.println ("in dog : playdead ");
}
}
public class cast
{
public static void main ( String [] args )
{
Dog d = new Dog ();
Animal a1 = d ;
d.makenoise();
a1.makenoise();
}
}
 
Ranch Hand
Posts: 70
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Just to make your code easier to read....

 
Joshua Ebarvia
Ranch Hand
Posts: 70
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It is calling the method by the Dog since polymorphism plays here. The object type method is being invoked not the reference type.
In your example, the dog masquerading as an animal, when calling the overriden method will surely call the dog version since it is its object type. The reference type animal only restricts the methods and variables, that can be invoked.


Thanks
 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

In the following code , in the cast class , i have explicitly type-casted dog reference to Animal type . But still when i invoke , a1.makenoise () , Dog class is invoked , not the Animal class . Can anyone explain the reason please ??



A dog IS-A animal meaning if you expect an animal, and someone gives you a dog, it should be okay.

Furthermore, the dog doesn't lose its attributes just because you don't know what type of animal you are holding. If it is a dog, and you expect your animal to make noise, it will bark.

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

i have explicitly type-casted dog reference to Animal type .



I don't see where is the type-casting here in this code.

 
Sridevi Baskaran
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi friends ,

I cannot understand the concept of left shifting and right shifting which involves negative numbers ...
For eg .,

int x = -1<<4

For positive numbers , we have the formula .

I will be very thankful to you if anyone could explain me the concept involving negative numbers in detail ........

Thanks in advance .......
 
Ranch Hand
Posts: 327
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I suggest you start a new thread for a new question.
 
Sridevi Baskaran
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi friends ,

In Kathy Sierra book , it is given that methods marked as final cannot be overridden . But the below code compiles fine and gives the output as clidlet . I am confused . Please let me know the solution to this .

class clidder
{
private final void flipper ( )
{
System.out.println("clidder");
}
}
public class clidlet extends clidder
{
private final void flipper ( )
{
System.out.println("Clidlet");
}
public static void main ( String [] args )
{
new clidlet ( ).flipper ( );
}
}

Thanks in advance ....
 
Jason Irwin
Ranch Hand
Posts: 327
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please use code tags - it makes your code much easier to read.

And please stop using the same thread for different questions - start a new thread (after searching to see if someone else has already asked the same question).

The reason it works is that it is not an override, it's just the definition of another method with the same name. You cannot override private methods. The private method in the superclass is not visible to the subclass - how could it possibly be an override?
reply
    Bookmark Topic Watch Topic
  • New Topic