Sri Yamujala

Greenhorn
+ Follow
since Dec 07, 2000
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 Sri Yamujala

Sorry, what I actually meant was for reading compressed files.
Thanks,
Sri
I'm trying to understand File I/O in Java. I understand how to open a file and read characters in it using a Character Stream, but can anyone point me in the right direction as to how do I read each line in a file at a time instead of each character.
Thanks,
Sri
Hi Shah,
Actually, It compiled fine. It didn't give any error.
If you remove the casting, it would give you an error for incompatible class types.
Sri

Originally posted by Shah Chunky:
Hi Friends...
Can someone tell me why the following code gives Runtime Error at Commented line 1.
I want to know the logic behind this error.
class Light { /* ... */ }
class LightBulb extends Light { /* ... */ }
public class WhoAmI
{
public static void main(String args[])
{
LightBulb lightbulb = new LightBulb();
Light lig = new Light();
lightbulb = (LightBulb) lig; // Line 1
System.out.println("It Works... ");
}
}
Thanks in advance


Originally posted by Cristi Tudose:
hi everybody,
for some reason,I could't enjoy this site
for almost 1 and half month
there are something new I should know regarding SCJP2?
plz notify me
thanks in advance
cristi


Cristi,
What's your question ???
The question of over-riding never comes in, in this case because both the methods in the subclass and superclass have different parameter types. This is actually a case of overloading, where the subclass is providing the same method as in its super class, but with a different type of parameter.
Hope this helps.
Sri

Originally posted by seema ram:
I dint completely understand the explanation.As the class is InheritanceTest I thought it will first look into InheritanceTest for the corresponding method.And since InheritanceTest(char c) was there would execute it.So the question of overriding never comes in.Can somebody correct me.


Hi Mindy,
If you take out private keyword for methodB() in Superclass, it's then printing 1 - so, it's got something to do with the access modifier in this case - can someone clarify, please?
Thanks,
Sri

Originally posted by Joseph Russell:
[B]What I attempted to do didn't work. To see how to format your code just click on the edit box above someone's post. You won't be able to edit their post but you will be able to find out how they were able to format their code.
Once you figure this out could you fix your code and maybe delete the repeated versions? Thanks.
I'm not sure but in response to your second code I believe the problem is that the methodA is calling methodB from within Superclass so it is going to call the Superclass method of methodB. I made both methods static and used the this keyword and still get your result so as far as the method is concerned it must not see subclass. Here's my code:

You've brought up an interesting question. I look forward to the replies.
Joe
[This message has been edited by Joseph Russell (edited February 15, 2001).][/B]


For the indexing, I just found out actually - use the UBB codes. Whenever you post some code, to save the indenting, put all your code between .
Like here:

Ans is
SuperClass Constructor Executed
SubClass Constructor Executed
methodA in Superclass
methodB in Superclass
9

Howcome methodB in SuperClass is called.Can some one explain in conjunction with the first program.
[/B]
Yep - thank you very much, Gaurav

Originally posted by Gaurav Mantro:
OK, I was partially wrong in my answer earlier. I overlooked some of the things, my bad.
Bala gave the right answer.
Since InhertitanceTest() in Process has int argument and InheritanceTest() in InheritanceTest has char argument, there would be no overriding. Since char can be converted to int the InheritanceTest method of Process is invoked.
So as long as InheritanceTest() in the process has argument to which char can be converted implicitly and that method is not overridden in InheritanceTest class, InheritanceTest of Process class will be invoked.
Sri did that help ?


Guys, any thoughts ???

Originally posted by Mindy Hudson:
class InheritanceTest extends Process {
int x=18;

public static void main(String [] args) {
Process p = new InheritanceTest();
System.out.println(p.InheritanceTest('R'));
System.out.println(p.x);
}

InheritanceTest() {
System.out.println(true ^ true);
}

InheritanceTest(char c) {
System.out.println(c);
}

char InheritanceTest(char c) {
c='V';
return (char)c;
}
}

class Process {
int x=9;

Process() {
System.out.println("Starting Process...");
}

char InheritanceTest(int i) {
i='S';
return (char)i;
}
}
What is the Output?
1.Prints Starting Process �, false, �S� and 18
2.Prints false, �V� and 9
3.Prints true, �V� and 9
4.Prints Starting Process � , true, �V� and 9
5.Prints Starting Process �, false, �V� and 9
6.Prints Starting Process �, false, �V� and 18
7.Prints Starting Process �, false, �S� and 9
8.Prints Starting Process �, true, �R�, and 18
9.Prints Starting Process �, true, �V� and 18
I think the answer should be 5.But the correct answer is 7.Since p has its class as InheritanceTest shouldnt the method in InheritanceTest be called.Can you explain it to me.


Hi Varsha,
I really don't think any weightage will be given to the percentage of marks in the market. I think all it matters is whether you are certified or not - although getting a high % makes you feel better.
But, you are certified now and enjoy your success. Congrats.
Sri

Originally posted by varsha mhaskar:
I really don't know whether to make announcement of such a low score on this forum or not.
It is indeed a very low score.
Well, I made mistakes on topics of thread and java.util
I wrongly interpreted the questions on java.util.
Anyway I just want to ask all of you
does this score has any value in the market?
How much weightage is given to the marks in the job opptunities? Or just certification is enough, marks are not that important?
I am a Comp Engineer with 6 years of experience in Oracle. and now I have given this exam.
Varsha


23 years ago
Hi Gaurav,
Alright, I reversed both the InheritanceTest methods in the subclass and super class i.e., the InheritanceTest method in the subclass now takes an int argument and the same method in the super class Process takes a char argument.
The code:

If I execute, from your explanation, the answer now should be
Starting Process...., false, V and 9, but the answer still is Starting Process...., false, S and 9 - why?
Thanks,
Sri

Ok it took me some amount of time to put the line numbers here, can JavaRanch provide similar functionlaity in as it would be pretty helpful.
At line 7 an object of InheritanceTest is created but refernced as object of Process P.
At line8 p.InheritanceTest('R') Should have invoked InheritanceTest() method of InheritanceTest object instead of Process object b'coz of overriding. But the trick is how Java handles it. Java internally converts it to int first and will look for a function matching argument list.
So at line 8 java prefers a function InheritanceTest(int x) over a function InheritanceTest (char c). It finds a InheritanceTest(int) in Process class which is not being overridden. You can test the same. Change the signature of InheritanceTest at line 22 and make it InheritanceTest(int c). Check the results.
Sri
p is not an object of Process type it is an object of InheritanceTest and is being type cast or referred as object of Process type.
Due to being casted as object of Process type p.x at line 9 means x of Process because overriding works on methods only.
Correct me if I am wrong.
regards
Gaurav Mantro[/b]
</BLOCKQUOTE>

[This message has been edited by Sri Yamujala (edited February 15, 2001).]
Hi Gaurav and Mindy,
Thank you for the clarification. My bad - you are right, over-riding works only for methods. That explains.
I've got a question on a totally different thing, though. If I want to reply to a post, how do I get the code from that post to be included in my reply, other than cut and paste method and how do I get indenting for this code.
Thanks Again,
Sri
I actually think that the answer should be
Starting Process....., false, V, 18
Althoug p is an object of Process type, since it has a reference to InheritanceTest type, unless you use 'super', nothing should be called from Process class. Isn't that right? I tried to compile it and I got Starting process..., false, S and 9 - why? Can someone explain please?
Thanks,
Sri
I see two problems with your loop.
Your boolean variable, b is not initialized and your while loop has an assignment operator (=) instead of a condition operator (==).
Try with those changes.
Sri
Hello,
I'm trying to read-up on packages and am having a problem implementing it.
My Classpath is set to
C:\Java
Under, C:\Java\Java2CR\Chapter9\p1,
I've these two files:
Protection.java:
package java2CR.Chapter9.p1 ;
public class Protection
{
int n = 1 ;
private int n_pri = 2 ;
protected int n_pro = 3 ;
public int n_pub = 4 ;
public Protection()
{
System.out.println( "Base Constructor" ) ;
System.out.println( "n = " + n ) ;
System.out.println( "n_pri = " + n_pri ) ;
System.out.println( "n_pro = " + n_pro ) ;
System.out.println( "n_pub = " + n_pub ) ;
}
}
Protection.java compiles fine.
I have another file in the same directory ( C:\Java\Java2CR\Chapter9\p1) as Derived.java
Derived.java:
package java2CR.Chapter9.p1 ;
class Derived extends Protection
{
Derived()
{
System.out.println( "Derived Constructor" ) ;
System.out.println( "n = " + n ) ;
System.out.println( "n_pri = " + n_pri ) ;
System.out.println( "n_pro = " + n_pro ) ;
System.out.println( "n_pub = " + n_pub ) ;
}
}
I'm unable to get Derived.java compiled. In Derived.java, I also changed the class declaration as
class Derived extends java2CR.Chapter9.p1.Protection -- it didn't work.
Any thoughts into this problem?
Appreciate very much your help.
Thanks,
Sri
23 years ago