aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Tricky Questions(Can anybody guide me) Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Tricky Questions(Can anybody guide me)" Watch "Tricky Questions(Can anybody guide me)" New topic
Author

Tricky Questions(Can anybody guide me)

Prasad Ballari
Ranch Hand

Joined: Sep 23, 2000
Posts: 149
Q1. what is secret behind this?.

Object ob=new int[]{1,3,5};
int[] x=(int[])ob;

Prasad

[This message has been edited by Prasad Ballari (edited October 02, 2000).]
Alagu Seenivasan
Greenhorn

Joined: Sep 30, 2000
Posts: 18
"An object of a sub-class can be assigned to a super class's instance variable". Since "Object" is the root class, any class descending from "Object" can be assigned to "Object".
another example
--------------

Vivek Nambiar
Ranch Hand

Joined: Sep 25, 2000
Posts: 63
int[] x=(int[])ob; which means that an explicit cast is being performed here as an array is one of the subclass of an Object class. And that any subclass cannot be assigned a value of its superclass.
[This message has been edited by Vivek Nambiar (edited October 02, 2000).]
Prasad Ballari
Ranch Hand

Joined: Sep 23, 2000
Posts: 149
Seenivasan and Vivek.
Thank u very much.One more thing.Can any object can be assigned to 'Object'class.But how it performs internally because lot of extra features are present in the subclass that are not present in superclass, Once we cast to the superclass, can we still access the features of subclass with reference to superclass?.
Prasad
mohit joshi
Ranch Hand

Joined: Sep 23, 2000
Posts: 243
Trying to invoke methods of subclass on Object variable will throw runtime exception. You will have to cast it back to an appropriate class to invoke functions.
[This message has been edited by mohit joshi (edited October 02, 2000).]
Prasad Ballari
Ranch Hand

Joined: Sep 23, 2000
Posts: 149
mohit,
I think u might be correct, but i tried with the following code,there eventhough i have not casted it to subclass its calling subclass method,but when i tried to call a variable x,its calling superclass variable not subclass. Why? whats difference it makes for methods & variables while calling,I am calling in the same way.

class A extends Object{
int x;
public void someMethod(){ x++ };
//methods to access x goes here
}
class B exterds A{
int x;

public void someMethod(){ x=10;x++ };
//methods to access x goes here
}
//At some point a created object like this
B tb=new B();
A ta=tb;
ta.someMethod(); //here its calling Subclass B's method
int y=ta.x; //here its calling Superclass A's variable

Prasad

[This message has been edited by Prasad Ballari (edited October 02, 2000).]
Vivek Nambiar
Ranch Hand

Joined: Sep 25, 2000
Posts: 63
A simple rule here.
Whenever u r assigning a subcalss to a superclass and call the its method, in ur case ta.someMethod(), it always call the subclass method.
Manish Singhal
Ranch Hand

Joined: Sep 21, 2000
Posts: 104
Prasad,
Here in your code...or in any similar code, the values of variables are decided at compile time and the methods are called at runtime so since there was no object of subclass till the execution of program begins, hence the value of the variable of superclass will be printed and the method of subclass will be called.
Anyone, Please correct me if I am wrong.
Manish </b?>
Manish Singhal
Ranch Hand

Joined: Sep 21, 2000
Posts: 104
Please explain -----------
Object ob=new int[]{1,3,5};
Manish
Ray Hsieh
Ranch Hand

Joined: Sep 20, 2000
Posts: 59
Hi Manish,
You'll wanna check one very short chapter about Array in JLS (Java Language Spec). It will explain *everything*, including this:
java.io.Serializable s = new int[] {1, 2, 3};
Cheers
Originally posted by Manish Singhal:
Please explain -----------
Object ob=new int[]{1,3,5};
Manish

Prasad Ballari
Ranch Hand

Joined: Sep 23, 2000
Posts: 149
Manish,
I hope ur answer correct.I am satisfied technically.Really thank very much.
Prasad
Ahmed Nasir
Ranch Hand

Joined: Sep 25, 2000
Posts: 57
I guess the rule to remember is that variables would be called from the super class and method would be called from subclass
when ever a call is made to a variable which is a superclass variable but contains a subclass object..right?..
I am a learner, please fix the above definitions if required.
Thanks
Nasir
Ahmed Nasir
Ranch Hand

Joined: Sep 25, 2000
Posts: 57
Just a little correction to my prev. post. when ever the call is made THROUGH a superclass variable which contains a subclass object.
Thanks
Nasir
Prasad Ballari
Ranch Hand

Joined: Sep 23, 2000
Posts: 149
Nasir
I can put explanation may be like this,The concept of this referancing is called a "Dynamic method Dispatch" by which a call to an overridden function is resolved at run time, rather than at compile time,hence only the variable are refered from Superclass during compile time but methods depend on different types of objects being referred from Super class variable.
If u still not clear.then pls let know, i may help u through one more example
Prasad
------------------

[This message has been edited by Prasad Ballari (edited October 03, 2000).]
Manish Singhal
Ranch Hand

Joined: Sep 21, 2000
Posts: 104

Nasir U are absolutely correct and dear Prasad we cannot name this concept as Dynamic Method Dispatch, becasue in that case Method Overriding concept comes into picture.
Am I wrong ???
Manish

[This message has been edited by Manish Singhal (edited October 03, 2000).]
Prasad Ballari
Ranch Hand

Joined: Sep 23, 2000
Posts: 149
manish.
Thanks for correcting me.Will you pls explain me the concept of Dynamic Method Dispatch.

Awiting for r reply.
Thankx
Prasad
------------------
Manish Singhal
Ranch Hand

Joined: Sep 21, 2000
Posts: 104
Prasad
Dynamic Method Dispatch is the mechanism in which a call to an overridden function is resolved at run time, rather than compile time.
class A{
void method(){
//something
}
}
class B extends A{
void method(){
//something
}
}
class C extends A{
void method(){
//something
}
}

class Dispatching(
public static void main(String [] args){
A a =new A();
B b = new B();
C c = new C();
A r; // reference of A
r =a;
r.callme();

r =b;
r.callme();

r =c;
r.callme();
}
}
I hope its fine now.
Manish
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Tricky Questions(Can anybody guide me)
 
Similar Threads
Confusion in overriding
India Australia cricket
A question on conversion
Why The Output Is Same...
finalize help plzzz