Jaishanker Vijayakumar

Greenhorn
+ Follow
since May 18, 2000
Cows and Likes
Cows
Total received
0
In last 30 days
0
Total given
0
Likes
Total received
0
Received in last 30 days
0
Total given
0
Given in last 30 days
0
Forums and Threads
Scavenger Hunt
expand Ranch Hand Scavenger Hunt
expand Greenhorn Scavenger Hunt

Recent posts by Jaishanker Vijayakumar

This is great thanks for your elaborate reply.
Adding to the explanation...
The condition where it will throw an ClassCastException is when
18: d2 = (Derived2)b;
Since b refers to "Derived1".

Originally posted by Jim Yingst:
Er... it looks like the given answer "b" is just plain wrong. Their explanation makes no sense. Don't worry about it - "d" is correct.[/QUOTE
I am not able to give myself an logical explanation to it. Please can you explain it to me why it should work.

21 years ago

Originally posted by Hema:
Take the lines,


I understand that but try this code it will not give you any errors.
Thanks for the detailed explanation.
The code in this executes with errors but the answer differs. This question is from gEs : Java(TM) 2.0.
What happens when the following code executes?
1:class Base{
2:// legal code
3:}
4:class Derived1 extends Base{
5: // legal code
6:}
7:class Derived2 extends Base{
8:// legal code
9:}
10 ublic class Test
11:{
12: static public void main(String [] args)
13: {
14: Base b = new Derived1 ();
15: Derived1 d1 = new Derived1();
16: Derived2 d2 = new Derived2();
17: b = (Base) d1;
18: b = (Base) d2;
}
}
A. Compile time error at line 14
B. Run time error at line 18
C. Compile time error at line 17
D. NO ERRORS

Question Help.
B. Run time error at line 18
b is of type Base and points to d1 and d2 is neither so at run
time it fails
I saw this question in a mock exam. I ran the code and it ran with any errors but the exam answer differs, can someone please explain to me the logic behind it. Or is the answer wrong.
What happens when the following code executes?
1:class Base{
2:// legal code
3:}
4:class Derived1 extends Base{
5: // legal code
6:}
7:class Derived2 extends Base{
8:// legal code
9:}
10 ublic class Test
11:{
12: static public void main(String [] args)
13: {
14: Base b = new Derived1 ();
15: Derived1 d1 = new Derived1();
16: Derived2 d2 = new Derived2();
17: b = (Base) d1;
18: b = (Base) d2;
}
}
A. Compile time error at line 14
B. Run time error at line 18
C. Compile time error at line 17
D. NO ERRORS

Question Help.
B. Run time error at line 18
b is of type Base and points to d1 and d2 is neither so at run
time it fails
21 years ago
I am not able to compile this code.
class Shape{
String Shape = "Shape";
int i;
Shape(){
System.out.println("Constructor Shape");
}
void draw(){
System.out.println("Draw Shape");
}
//String getCircleString(){return "None";}
}
class Circle extends Shape{
String sc = "Circle";
int ic;
Circle(){
System.out.println("Constructor Circle");
}
void draw(){
int ic1=0 ;
System.out.println("Draw Circle"+ic1);
}
public String getCircleString(){
return sc;
}
}

public class Geometry{
String g = "Geometry";
public static void main(String[] arg){
Shape s1 = new Circle();
s1.draw();
System.out.println("Variable Shape 3 sc from accessor "+ s1.getCircleString());

}
}
It gives me an error on the Print statement at s1.getCircleString() is not defined in the Shape class. But since I created it as a Circle should it not identify the function defined there.
If I create this function in Shape also then the error does not occur.
Guys can some one explain to me why this is happening as it is happening.
Thanks in advance.
22 years ago