sethu chiyan

Greenhorn
+ Follow
since Apr 05, 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 sethu chiyan

Hi Harsha,

// code here

default: x += x < 0 ? 2 : -2;// Evaluation explained below


In First time Loop

Current x value is -5 as v know

x+= (x<0) ? 2 : -2
x += (-5<0) ? 2: -2
x+=2;
so x = -3 in 1rst iteration


In second time Loop

Current x value is -3 as v know

x+= (x<0) ? 2 : -2
x += (-3<0) ? 2: -2
x+=2;
so x = -1 in 2nd iteration

In Third time Loop

Current x value is -1 as v know

x+= (x<0) ? 2 : -2
x += (-1<0) ? 2: -2
x+=2;
so x = 1 in 2nd iteration

Then as v know it goes like this
System.out.print("1");
System.out.print("4");
System.out.print("3");
System.out.print("3");

So the O/P is 1433.

Hope this will help u.

Pl do contact me if u r still in trouble,

with regards,
Chiyan.
Hi,


class Vertibrate
{
public void move()
{
System.out.println("Move");
}

}

class Mammal extends Vertibrate
{
public void move()
{
super.move();
System.out.println("Walks");
}
}

class Dog extends Mammal
{
public void move()
{
super.move();
System.out.println("Walks on paws");
}
}

public class Mobile
{
public static void main(String[] args)

{
Dog _dog = new Dog();
_dog.move();
}
}

I have tried in this way, the o/p is:
Move
Walks
Walks on paws

Anyway I know this is not the exact answer what you expect,but i know we cannot use super.super.move();

Still am waiting for Gi's response.. pl guide me 2.

with regards,
Chiyan
Hi,

Nithya U r xactly right,

Vidya, The answer is nothing, because it takes Default simply as label not as default.

with regards,
Chiyan
Hi, I have replied for some similar kind of question in another topic.

class Base {}
class Sub extends Base {}
class Sub2 extends Base {}
public class Mobile
{
public static void main(String argv[]){
Base b=new Base();
Sub s=(Sub) b; //Line no 10


}
}

Yes! its a Runtime error, coz of not following hierarchy reflection. you can't assign the obj of base class to the derived class object. But u can assign the obj of derived class to the base class object.

Example code for ur prg: No Compiler or runtime ERROR.

Sub _sub = new Sub();
b = _sub;
Sub2 _sub2 = new Sub2();
b = _sub2;


The point is Derived class may have some additional functionality of Base class, So u cant assign the base class object to derived class object.

The compiler would have shown the error if you had not type cast the b to (Sub). the copiler only checks the r.side operand sink with l.side operand in the assignment operator.

This cast incompatability will come to know only at the runtime.

with regards,
Chiyan
Hi,

Can u exaplain bit about getInstance,
when do we need to use???

Calendar _calendar = Calendar.getInstance();// y do v use here

explain with more information.

Thz,
Chiyan
Hi,
Apologize for repeating the whole prg.
class Phone
{
String device = "Phone.device";
void showDevice()
{
System.out.print("Phone.showDevice," + device + " ");
}
Phone()
{
showDevice();
}
}
class Mobile extends Phone
{
String device = "Mobile.device";
void showDevice()
{
/*super();Compiler objects: Coz the Constructor call should be the first statement of the constructor*/
System.out.print("Mobile.showDevice," + device + " ");
}
Mobile()
{
super();// Complier's automatic constructor calls from here
showDevice();
}
public static void main(String[] args)
{
Mobile n = new Mobile();
n.showDevice();
}
}

The O/P of the prg is

Mobile.showDevice,null Mobile.showDevice,Mobile.device Mobile.showDevice,Mobile.device

Explanation:
while creating the instance for class Mobile(), its constructor is called. From the Mobile Constructor it invokes parent class's Phone constructor either u define super(); explicitly or implicitly.

As Srinivas said:

"null" is getting printed because the call to the constructor is not completed hence the member variables are not initilized.

The rest of the O/P is same as explained above.

If u want to call a overriden method from overriding method ,u can use

void showDevice()
{
super.showDevice();// way of using super inside the method
}

with regards,
Chiyan
Hi da Rama,

if(((x>3)&&(y<2)|doStuff()))

1. I don't know if its a type, should it be || instead of |. Would it still return a boolean with |.?
ANS: Yes! Its a type, u can use |. Yes it will return boolean with |
2. Assuming it was a type and it actually meant ||, k&b says:

No, u no need to assume, keep as it is

if (x>3) is false, no point in looking at the rest of the expression. 'cos during runtime it is evaluated as

Yes! exactly.

3. which means, it solves the first operation from left to right and the result of it, it operates with the next one.

can someone shed some light on this?

S, it solvz first operation from left to right and the result, with that result proceed to next operator and the other operand. this is the way how it work.


With regards,
Sethu.
Hi Prasad n Raghu,
Note the ERROR:
class Mobile extends Phone
{
String device = "Mobile.device";
void showDevice()
{
super();//Compiler objects
//Coz the Constructor call
//should be the first statement of the constructor
System.out.print("Mobile.showDevice," + device + " ");
}
Mobile()
{
super();// Complier's automatic constructor calls from here
showDevice();
}

public static void main(String[] args)
{
Mobile n = new Mobile();
n.showDevice();
}

}
With regards,
Chiyan
class Mytest
{
Mytest a;
Mysubtest b;
a= new Mytest();
b= new Mysubtest();
a=b; //Compiler can assure itself a legal conversion.
b=a; //giving compile error (Yes!) coz Compiler follows the hierarchy reflection. u can't do it wise versa
// remaining code;
}
class Mysubtest extends Mytest
{}
Hi Gayathri,

As far as I know you can copy the reference variable of child class to parent class's.Child class reference variable can b assigned to any class reference variable of higher in the hierarchy.

About ERR: Compiler follows the hierarchy reflection and it can assure itself a legal conversion. So u cant assign wise versa.