1)what happens if i do this=null?2)how to swap 2 variables without using third temp variable in java?3)constructors can not be inherited,why?

Questions by lherwadkar

Showing Answers 1 - 17 of 17 Answers

madan

  • Apr 3rd, 2006
 

it shows compile time error when we make this=null i.e we can not assign value to final variable

  Was this answer useful?  Yes

rameshr

  • Apr 3rd, 2006
 

consider the  two variables and its values are a=20,b=10

a=a+b; //a=30

b=a-b;//b=20

a=a-b;//a=10

  Was this answer useful?  Yes

dipl. ing. Dan

  • Apr 3rd, 2006
 

3. Constructors are inherited. If you have a constructor in your superclass, it will be inherited in your subclass.

Try this:

public class InheritMe {
 
 public InheritMe() {
  System.out.println("I made it into InheritMe Constructor!");
 }

}

Now extend your subclass to inherit this superclass. 

"public class MySubclass extends InheritMe { ... } "

  Was this answer useful?  Yes

Ranjith

  • Apr 5th, 2006
 

I didn't get what you want to say about your code?? Your code does not inherited constructor, rather it is inheriting the super class

Constructors can not be inherited.

If you want make use of super class constructors, u can call that by using super keyword.

  Was this answer useful?  Yes

Nagarjuna S

  • Apr 7th, 2006
 

For swapping try this, which is a very good method.

a=a^b;// a = a xor b

b=a^b;// b = a xor b

a=a^b;// a = a xor b

Lunaticrr

  • Apr 11th, 2006
 

Could you explain what a=a^b;// a = a xor bb=a^b;// b = a xor ba=a^b;// a = a xor bis all about as opposed to a=a+b;b=a-b;a=a-b;I get the bottom one but not the top one. What does xor mean? What does ^ do?

  Was this answer useful?  Yes

SATISH

  • Apr 14th, 2006
 

Use this technique

if the 2 variables are a and b

b = b-a ;

a = a+b;

b = a-b;

print a and b;

if we take a = 40 and b= 50

b = b-a = 50 - 40 = 10;

a = a+b = 40 + 10 = 50;//(here b is 10)

b = a-b = 50 - 10 = 40; //(here a = 50)

then a = 50 and b = 40;

if we take a = 60 and b= 30

b = b-a = 30 - 60 = -30;

a = a+b = 60 + (-30) = 30;//(here b is -30)

b = a-b = 30 - (-30) = 60; //(here a = 30)

then a = 30 and b = 60;

  Was this answer useful?  Yes

Jhansi

  • May 24th, 2006
 

For a better understanding on Constructors, plz go through the article given below "http://www.javaworld.com/jw-10-2000/jw-1013-constructors.html".

  Was this answer useful?  Yes

The answer given above by Mr. Satish
------------------------------------------------------------------------------------------
a= ((a+b)-(a-b))/2
b= ((a+b)+(a-b))/2

------------------------------------------------------------------------------------------
will not work when one of the numbers is negative!!!

vinny

  Was this answer useful?  Yes

Give your answer:

If you think the above answer is not correct, Please select a reason and add your answer below.

 

Related Answered Questions

 

Related Open Questions