hansika motwani

Ranch Hand
+ Follow
since Jul 22, 2008
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 hansika motwani

now its working fine perfect..no probs .Thanks a lot..


class er
{
public static void main(String [] args)
{
b s = new b();
s.display(9,2);
}
}[/code]
1)is this method overloading ? There is a statement in java like method overloading has nothing to do with inheritance and polymorphism then why is this working here ?

2)
can we say the method overloading as compile time polymorphism (static binding)and method overriding as run time polymorphism(late binding)?

3) can we use method overloading even in inheritance then?

There is a rule in overriding that arguments must be the same and return type must be compatible .
RULE 1-->whatever the superclass takes as an argument the subclass overriding the method must use the same argument
RULE 2 -- > Whatever the superclass declares as return type .the overriding method must declare either the same type or a subclass type .remember a subclass object is guaranteed to be able to do anything its superclass declares so its safe to return a subclass where the superclass is expected

4) return type must be compatible ? what does this mean ? is compatible = same ?

In rule 1 does same argument means same type ? or same variable name?

5) to illustrate rule 2 i wrote one program like this

here in subclass i have overridden the method by using same argument type but with different return type which subclass intends ? iam getting compile time error but rule 2 says (this only) as per my view )...Correct me if iam wrong ? rule 2 says remember a subclass object is guaranteed to be able to do anything its superclass declares so its safe to return a subclass where the superclass is expected

6)

here i cant override a public method and make it private .Actually the compiler thinks at compile time that it is a public method ..if suddenly at run time the jvm slammed the door shut because the overriding version called at run time is private . then why am i getting compile time error rather than run time error because the compiler should'nt bother the jvm has to bother as i suppose ?'

please let me know how can i know these issues like compiler considers some issues and jvm bothers about few ..how would i know that ? Does it come by practise or is there any way to know about that

7) During method overloading you can vary the access levels in any direction ..what does this mean ?
8) During method overloading if only the return type is different its not a valid overload- the compiler will assume you are trying to override the method and even that wont be legal unless the return type is a subtype of the return type declared in a super class . what does this mean ?
does that mean we can put any return type in our sublass to make it a legal ?
stop knowing about personal details and involve in the subject .utilize your valuable time for java discussions rather about personal issues ..This is a good forum which is very rare for java lovers ..so please focus on java..Its my sincere appeal
SORRY FOR THE MISTAKE .I WRONGLY POSTED IT THREE TIMES..HOW TO delete ..how to use code tags ? convey it please

hansika motwani wrote:

hansika motwani wrote:1)class a
2){
3)int a =2,b=3;
4)void display()
5){
6)System.out.println(a);
7)}
8)}
9)class b extends a
10){
11)int a =4,b=5;

12)//void display()
13)//{
14)//System.out.print(b);
15)//}
16)}

17)class c
18){
19)public static void main(String args[])
20){
21)b obj = new b();
22)obj.display();
23)System.out.println(obj.a);
24)System.out.println(obj.b);
25)}
26)}

Q7)here since display method is not found by the jvm at run time in class b it will go and search from which it is extended and it finds and uses the method in class a
am i right? upto here ? now why again it uses the instance members of base class when display() method of base class is invoked then it has to use derived class instance members na since the reference variable type is b rather than a ?

Q8)once see the 1) answer of yours
you said
if you had used a obj=new b(); than obj.b will call a.a variable.
here correct me if iam wrong then obj.b will call a.b variable na ...

same first answer continuation --- you said obj.display() will display method of actual object not the reference type, here actual object is new b(), so b.display(), it is decided at runtime by jvm.

then if in display method i printed sop(a) and sop(b) then which instance members are invoked - derived class ones or base class ones ?
in my example i said sop(b) and the type is b so will b's instance variable will be invoked ? if the type is a then also b's instance vairable is getting invoked . why so ?

hansika motwani wrote:1)class a
2){
3)int a =2,b=3;
4)void display()
5){
6)System.out.println(a);
7)}
8)}
9)class b extends a
10){
11)int a =4,b=5;

12)//void display()
13)//{
14)//System.out.print(b);
15)//}
16)}

17)class c
18){
19)public static void main(String args[])
20){
21)b obj = new b();
22)obj.display();
23)System.out.println(obj.a);
24)System.out.println(obj.b);
25)}
26)}

Q7)here since display method is not found by the jvm at run time in class b it will go and search from which it is extended and it finds and uses the method in class a
am i right? upto here ? now why again it uses the instance members of base class when display() method of base class is invoked then it has to use derived class instance members na since the reference variable type is b rather than a ?

Q8)once see the 1) answer of yours
you said
if you had used a obj=new b(); than obj.b will call a.a variable.
here correct me if iam wrong then obj.b will call a.b variable na ...

same first answer continuation --- you said obj.display() will display method of actual object not the reference type, here actual object is new b(), so b.display(), it is decided at runtime by jvm.

then if in display method i printed sop(a) and sop(b) then which instance members are invoked - derived class ones or base class ones ?
in my example i said sop(b) and the type is b so will b's instance variable will be invoked ? if the type is a then also b's instance vairable is getting invoked . why so ?

PUNIT ..excellent explanation ..every thing went well except the first answer of yours
After i did set the classpath as said by you .it did compile and i got my class file in f:
everything was ok but when i tried to run it as java kanth
it is saying exception in thread main java.lang.noclassdeffound error: kanth

but if i again type this in command line as set classpath=.;
and typed java kanth (once) i can see partial output now with an exception

and again set classpath as set classpath=%classpath%;e:\
and now javac kanth.java
successfully compiled
and now java kanth

I CAN SEE THE OUTPUT NOW ( HURRAH!) WHATS THE LOGIC behind it first it is not showing now its showing ( whats the logic???)

what should i do to see the output first itself??
1)class a
2){
3)int a =2,b=3;
4)void display()
5){
6)System.out.println(a);
7)}
8)}
9)class b extends a
10){
11)int a =4,b=5;

12)//void display()
13)//{
14)//System.out.print(b);
15)//}
16)}

17)class c
18){
19)public static void main(String args[])
20){
21)b obj = new b();
22)obj.display();
23)System.out.println(obj.a);
24)System.out.println(obj.b);
25)}
26)}

Q7)here since display method is not found by the jvm at run time in class b it will go and search from which it is extended and it finds and uses the method in class a
am i right? upto here ? now why again it uses the instance members of base class when display() method of base class is invoked then it has to use derived class instance members na since the reference variable type is b rather than a ?

Q8)once see the 1) answer of yours
you said
if you had used a obj=new b(); than obj.b will call a.a variable.
here correct me if iam wrong then obj.b will call a.b variable na ...

same first answer continuation --- you said obj.display() will display method of actual object not the reference type, here actual object is new b(), so b.display(), it is decided at runtime by jvm.

then if in display method i printed sop(a) and sop(b) then which instance members are invoked - derived class ones or base class ones ?
in my example i said sop(b) and the type is b so will b's instance variable will be invoked ? if the type is a then also b's instance vairable is getting invoked . why so ?
Ranchers help me in overriding issues !



1)here in this program when i say obj . display () then the derived class display is being overridden and it gets invoked ..here when iam saying obj . display () then display () method uses base class a (instance members )or derived class a (instance members ) ?

2)similarly there is a statement in java that instance members are not overridden
but it is not applying here when i say obj.a ,obj.b then it is displaying 4and 5 rather than 2 and 3 ...

but we know that instance members are not overridden ? then why it happens like this? but here derived class instance members are overridden ???


3) when i say a obj = new b(); then base class instance members come into picture ..is it due to type of obj ? Does that mean that instance members are not overridden. applies only basing on their type ?? and instance methods are overridden irrespective of their type ? like obj.display() invokes derived class methods without bothering about their type?

4) JVM starts walking up the inheritance tree starting at the classtype you invoked the method on ? But what happens if jvm doesnt find a match
What does these words mean ? can you explain it briefly?

5) when i say a obj = new b(); then the jvm will search for the method in b's class or first in a's class .confused about these things answer if possible as if the compiler and jvm thinks ...I mean to say that will the jvm will go and search which class first in inheritance?

6) what s the meanig of return key ? what does it indicate? how to use that in our program?

provide me an apt link atleast if you dont find time to answer my queries ?
Thanks punit.Thanksalot .I will let you know if i have any queries related to the current topic
iam in f:

// saved under the name of sri.java


package packag.pack.sri.poi.kjdsf;
public class sri
{

public sri()
{
System.out.println("no");
}

}
then i typed f:\> javac -d . sri.java at the command line and obtained the folder under the directory structure of

f:\packag\pack\sri\poi\kjdsf>sri.class


//saved under the name of kanth.java

import packag.pack.sri.poi.kjdsf.sri;//fully qualified name

class kanth
{
kanth()
{
System.out.println("yes ");
}
public static void main(String args[])
{
kanth o = new kanth();
sri obj = new sri();

}

}




then i typed in f:\> javac kanth.java and java kanth.java and obtained
ouput as yes and no .

Q1) now i cutted the folder packag under f: which is f:\packag and pasted it in e: so it became e:\packag\pack\sri\poi\kjdsf\sri.class>

since i moved the directory structure in another drive e: so i did set the classpath as
i typed the command when iam in f: f:\> set classpath=%classpath%;e:\packag

then i tried to compile javac kanth.java.
here iam getting error !!! package does not exist


can ranchers help me out of this ..how to set the classpath ?
is there any different way to set the classpath ?
please suggest me basing on this example ..

Q2)2. Secondly, if you have the package in D: drive as "d:\p\a.class"
where "p" is the package name. Then from command line we can execute the a.class using the fully qualified name of the class which is "p.a"

F:\>java -cp D:\ p.a

or you can set D:\ in the classpath environment variable and say

F:\>java p.a
Is this method right???

This is not working for me though i use cp option

can any one say how to use cp option for the above example
and how to set the classpath for the above example
------------------------------------

q3) saved under a.java
package p;
public class a
{
public static void main(String [] args )
{
System.out.println("asdf");
}
}
typed at the command prompt e:\> javac -d . a.java
then i got a.class under p folder as p/a.class
how to see the output when iam in e: itself

i typed like this e:cd p
then e:\p> java a
but i am unable to see the output ? what should be the command to see the output ?


q4)
I also went into f: and used cp option as
f:\ java -cp e:\p.a
IT IS NOT getting .iam unable to see the output ???

Q5) If i have a jar file in d: \jars . which consists of packages then how would i use that in different directory say e:
and access the package in d:\jars\package...how to set classpath for jars across drives and access the packages in that jar ?

what should be the commands for the above 5 questions ? dear ranchers what could be the solutions for these?
its from balagruswamys book ..
a primer edition
balaguruswamy (core java book)
If possible with explanation(even one line if time wont permit you to write elaborately (please explain so that i can get your way of thinking to the query )

TRUE OR FALSE (T/F) WRITE ANSWERS WITH QUESTION NUMBERS
1)It is an error to have a method with the same
signature in both the superclass and subclass

2)A constructor must always invoke its superclass constructor in its first statement

3)Subclasses of an abstract class that do not provide an implementation of an abstract method are also abstract

4)All methods in abstract class must be declared abstract

5)when the string objects are compared with == ,the result is true if the strings contain the same values

6)A string object cant be modified after it is created

7)A catch can have comma separated multiple arguments

8)It is an error to catch the same type of exception in two different blocks
assosciated with a particular try block
9)A final class may not have any abstract methods

10)java always provides a default constructor to a class

11)when present , package must be the first non comment statement in the file

12)I)The import statement is always the first non comment statement in a java program file
II)int 0xCAFE=2? EXPLAIN


13)I)objects are passed to a method by use of call - by - reference
II)IS -10>>>2=1073741821.EXPLAIN ?

----------------------------------------------------------------------------------------------------------------------------------------------

MULTIPLE CHOICE .CHOOSE A,B,C,D ...
OBSERVE CAREFULLY YOU MAY ALSO FIND MORE THAN ONE ANSWER
THEN WRITE THAT MANY OPTIONS

14)What will be the result of the expression 9|9
a)1b)18c)9d)none of the above

15)consider the following statements
int x=10,y=15;
x=(x<y)?(y+x) y-x);
what will be the value of x after executing the statements

16)which of the following operators are overloaded for string objects
a)-b)+c)+=d)&

17)which of the following are illegal loop constructs ?
a.while (int i >0)
{
i--;
other statements ;
}
b.for (int i=10,int j=0;i+j>5;i=i-2,j++)
{
body of statements
}

18)which of the following contol expressions are valid for an if statement
a.an integer expression
b.a boolean expression
c.either a or b
d.neither a nor b

19) In the following code snippet , which lines of the code contain error
1)int j=0;
2)while (j<10){
3)j++;
4)if (j==5)continue loop ;
5)System.out.println("j is "+j);
}
a.line 2 .b.line 3
c.line 4.d.line 5 e.none of the above

20)which keyword can protect a class in a package from accessibility by the classes outside the package ?
a.private b.protected c.final d.dont use any keyword(default)

21)we would like to make a member of a class visible in all subclasses regardless of what package they are in
which one of the following keywords would achieve this a.private b.protectedc.public d.private protected

22)class class a
{
public static void main(String args[])
{
classb b = classb();
}
classa (int x)
{}
}
class classb extends classa
{}
what will happen when we compile and run this code ?
a.compile and run successfully
b.error classa does not define a no argument constructor
c.error classb does not define a no argument constructor
d.error there is no code in the class classb
e.error there is nod code in the constructor classa (int x)


23)what does the following line of code do ?
TextField text = new TextField (10);
a.creates text object that can hold 10 rows of text
b.creates text object that can hold 10 columns of text
c.creates the object text and initializes it with the value 10
d.The code is illegal


24)what is java_g used for
a.using the jdb tool
b.executing a class with optimization turned off
c.To provide information about deprecated methods
d. none of the above

25)which of the following command lines options generates documentation for all classes and methods
a.-protected.
b.-public
c.-private
d.-verbose
e.-encoding


26)which of the following represents legal flow control statements
a.break;
b.break();
c.continue outer ;
d.continue (inner );
e.return ;
f.exit ();
ya ankit whats the cost of this book when you bought it? moreso,do you know any indian site which will courier me for that price.

ya ankit .though this is not the appropriate thread for posting this
i want to share this with you.why dont you maintain a single thread of ankit where all our queries can be posted to you. because i find only you to be more precise in posting queries .more so i congratulate you on your scjps finish.its amazing ankit.but still iam not satisfied with the lost 2 percent.but we are human beings na
ya ankit whats the cost of this book when you bought it? moreso,do you know any indian site which will courier me for that price.

ya ankit .though this is not the appropriate thread for posting this
i want to share this with you.why dont you maintain a single thread of ankit where all our queries can be posted to you. because i find only you to be more precise in posting queries .more so i congratulate you on your scjps finish.its amazing ankit.but still iam not satisfied with the lost 2 percent.but we are human beings na