deepu Bhalotia

Ranch Hand
+ Follow
since Apr 19, 2005
Merit badge: grant badges
For More
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 deepu Bhalotia

I know the Output...
My Question is ...

which overloaded method will be called by
m1(a)....m1(b), m1(c)..statements...


Thanks
Deepak
http://www.danchisholm.net/july21/mybook/chapter11/exam1.html

class SuperA {String s1="SuperA";}
class SuperB {String s1="SuperB";}
class A extends SuperA {
String s1="A";
class B extends SuperB { // 1
String s1="B";
void m1() {
System.out.print(this.s1 + ","); // 2
System.out.print(super.s1 + ","); // 3
System.out.print(A.this.s1 + ","); // 4
System.out.print(A.super.s1); // 5
}
}
public static void main (String[] args) {
new A().new B().m1(); // 6
}}

Can you explain me about line number-6...
class A {String s1 = "A";}
class B extends A {String s1 = "B";}
class C extends B {String s1 = "C";}
class D {
static void m1(A x) {System.out.print(x.s1);}
static void m1(B x) {System.out.print(x.s1);}
static void m1(C x) {System.out.print(x.s1);}
public static void main(String[] args) {
A a; B b; C c; a = b = c = new C();
m1(a); //1
m1(b); //2
m1(c); //3
}}


// Hi Guys,
Can you pleas explain me .... in line 1,2 and 3 which method will be called.

Thanks,
Deepak

what is reference type of the Object created in Line 1...


Deepak
[ May 11, 2005: Message edited by: Mark Spritzler ]
Answer is

1. B
2. D
3. E
4. F


Thanks,
Deepak
can you please make it more clearer...

This question is teasing me for a long time...

Thanks,
Deepak
This question has been given in http://www.danchisholm.net/july21/mybook/chapter11/exam1ans.html

Question 2
Suppose that the superclass constructor invocation, "super(argumentListopt);", appears explicitly in a subclass constructor. If a compile-time error is to be avoided then the arguments for the superclass constructor invocation, "super(argumentListopt);", can not refer to which of the following?

a. Static variables declared in this class or any superclass.
b. Instance variables declared in this class or any superclass.
c. Static methods declared in this class or any superclass.
d. Instance methods declared in this class or any superclass.
e. The keyword this.
f. The keyword super.


Can anybody explain it with an Example....

Thanks,
Deepak

(Hopefully fixed the link...)
[ May 10, 2005: Message edited by: Barry Gaunt ]
class ColorException extends Exception {}

class WhiteException extends ColorException {}

class White
{
void m1() throws ColorException {throw new WhiteException();}
void m2() throws WhiteException {}

public static void main (String[] args)
{
White white = new White();
int a,b,d,f; a = b = d = f = 0;

try
{
white.m1();
a++;
}
catch (ColorException e)
{
b++;
}

try
{
white.m2();
d++;
}
catch (WhiteException e)
{
f++;
}
System.out.print(a+","+b+","+d+","+f);
}
}

I think the Answer willbe 0101 But the Given answer is 0,1,1,0.

Can anybody explain please.....

With Regards,
Deepak
Timmy
Can you tell me the Hierarchy...
class A {
public static void main (String[] args) {
Object error = new Error();
Object runtimeException = new RuntimeException();
System.out.print((error instanceof Exception) + ",");
System.out.print(runtimeException instanceof Exception);
}}

Hi
Can you Explain me about Statement....
Object error = new Error();

Here Error class is a Direct Subclass of Throwable.....So how can Object be the Superclass of Error...


Thanks in Advance....
Deepak
class Animal
{
}
class Horse extends Animal
{
}
public class UseAnimals
{
public void doStuff(Animal a)
{
System.out.println("In the Animal version");
}
public void doStuff(Horse h)
{
System.out.println("In the Horse version");
}
public static void main (String [] args)
{
UseAnimals ua = new UseAnimals();
Animal animalObj = new Animal();
Horse horseObj = new Horse();
ua.doStuff(animalObj);
ua.doStuff(horseObj);


Animal animalRefToHorse = new Horse();
ua.doStuff(animalRefToHorse);
}
}

OUTPUT:
In the Animal version
In the Horse version
In the Animal version

Hi,
This is the Perfect Example of Overloading.. In Overloading the Compiler Check for the Reference type.


Animal animalRefToHorse = new Horse();
ua.doStuff(animalRefToHorse);

Here the Reference is of Animal. So the doStuff method that takes animal object as a Arguments gets executed...

Animal animalObj = new Animal();

Here the Reference is of Animal. So Animal version....

Horse horseObj = new Horse();
Here the Reference is of Horse. So Horse version gets executed...


Hope this will clarify your doubt...

Deepak
[ May 10, 2005: Message edited by: deepu Bhalotia ]
In Overloading Overloaded method in Subclass can throw any Exception.

I think this code is totally valid and should n't throw any compile time error.

Can you just provide me.. What is the Error Message....


Deepak
You mean to say
Both Wrapper class object should be same and the Value. Other wise it will return false...
I have created instance of Two Classes.

1. StringBuffer s1=new StringBuffer("Java Ranch");
2. String s2=new String("Java Ranch");

when i use the below given statement, Should it give a compile time error or should return false or true..?

s2.equals(s1)

Deepak