aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Reference variable casting Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Reference variable casting" Watch "Reference variable casting" New topic
Author

Reference variable casting

Sridevi Baskaran
Greenhorn

Joined: Jun 29, 2009
Posts: 7
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();
}
}
Joshua Ebarvia
Ranch Hand

Joined: Sep 25, 2007
Posts: 70
Just to make your code easier to read....



Lovin' java
Joshua Ebarvia
Ranch Hand

Joined: Sep 25, 2007
Posts: 70
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
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 19063
    
  40

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


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Mo Jay
Ranch Hand

Joined: Feb 16, 2009
Posts: 83
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

Joined: Jun 29, 2009
Posts: 7
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 .......
Jason Irwin
Ranch Hand

Joined: Jun 09, 2009
Posts: 327
I suggest you start a new thread for a new question.


SCJP6
Sridevi Baskaran
Greenhorn

Joined: Jun 29, 2009
Posts: 7
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

Joined: Jun 09, 2009
Posts: 327
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?
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Reference variable casting