Swaraj Pal

Greenhorn
+ Follow
since May 24, 2014
Merit badge: grant badges
For More
Cows and Likes
Cows
Total received
In last 30 days
0
Forums and Threads

Recent posts by Swaraj Pal

Hello Rajat,
It could be used in many situations , just one of them may be when we need to throw our own custom Exceptions as Java code by default does not throw the exceptions made by the programmer
Its just one case,

e.g.



and in method


10 years ago
Hey Ryan,
Yes you can do it for sure, infact that is what overriding means, for sake of polymorphism(many forms, that is what you saying).

If I understand your question correct , you are asking for this,
Overriding is used when you have to use same method name for different purpose in different classes(in inheritence ofcourse).

okay so you can do follwing



and its subclass as




So you can execute following instances



So i guess it is helpful to you.
10 years ago
Hey all,
You can try this approach for better work,

Instead of working with integers its better to work with Strings for this type of iterative work,





Reply if it helps
10 years ago
Hello Roberts,

You may use the DateFormat.parse() method to make two Date Objects and further use Date.getTime() to subtract the time (in milliseconds) to calculate the hours between the two objects.



Hope it helps, Reply if it helped.
10 years ago
Thank You guys, i appeared for OCPJP 6 and cleared it. Thank You.

Dharmendra Ramanujan wrote:Given the default classpath:
/foo
And this directory structure:
foo
|
test
|
xcom
|--A.class
|--B.java

And these two files:


package xcom;
public class A { }

package xcom;
public class B extends A { }

Which allows B.java to compile? (Choose all that apply.)

A. Set the current directory to xcom then invoke
javac B.java

B. Set the current directory to xcom then invoke
javac -classpath . B.java

C. Set the current directory to test then invoke
javac -classpath . xcom/B.java

D. Set the current directory to test then invoke
javac -classpath xcom B.java

E. Set the current directory to test then invoke
javac -classpath xcom:. B.java

why option b is not correct




Hello Dharmendra,
The following may help you,
The root of xcom package is the "test" directory. So the javac must be executed from that directory only .
In simpler words, the classes A and B are in package xcom (they are put in a box xcom) so we can see only xcom.A and xcom.B classes from test directory.

So your case, option b is incorrect because:-





The compiler sees it as

foo
|
test
|-xcom.A
|-xcom.B


When you are in directory xcom, you need a "A.class" to compile B , but that class exists in xcom package so we need a xcom.A class but that is not in current directory (i.e. -> "." )(xcom/xcom/A.class is not valid) , but it is present in previous directory (test)(as test/xcom/A.class is valid) so we can modify this command to take xcom.A (from test directory) as follwing:-







The correct options are only C and E because in C current directory has xcom.A (or xcom/A) and same is in E too.
In option D current directory is not included and xcom does not contain xcom.A (as it searches for xcom/xcom/A)


Reply if it helped.
Thanks alot guys.
10 years ago
Hi Ryan,
In your code I suppose there is nothing wrong with the inheritance , you are doing everything perfect, so is the JVM,
Here is the problem :-

1. You think that line 31 code runs before line 27.

2. But if you check line 25 it will show you that it runs the overridden method of ShippedOrder Class and on checking the code of ShippedOrder's totalPrice() you have a message display(showMessageDialog) there. check here



In above method it displays the total price before everyone else.

As it is not displayTotalPrice(), you should define this method as:-


Hope it helps, reply if your problem is solved.
10 years ago
Hello all,
I just appeared for OCPJP today and cleared with 98%. I want to Thanks all the members who gave awesome and quick answers to my queries. Thanks to Kathy And Bert for the awesome book and Devaka Cooray for examlab, both of them were awesome learning resources, and yeah Paul Wheaton for providing this awesome forum. :-) . I am loving this forum.

For other exam takers I will suggest to use the K&B book,

I followed following approach:-

1. Read out whole of K&B book. 20-25 days
Read out whole of book and practice the tests at end of each exam.
2. Read the important points again from all chapters and made the notes of things that were tough to understand 4 days
These notes were made in view of final revision, that can be helpful as a reference and also helps in revising the chapters again for better understanding of missed out points
3. Give Tests at Examlab.com and From the CD master Version of K&B , dont worry these are tough ones and have a learning attitude , and keep making points of what you don't understand and questions that were incorrect.

If you score 50-60% in mock exams , You shall be able to clear exam.
10 years ago

Henry Wong wrote: Inheriting a method from the superclass just means that the subclass will use the superclass version of the method. It doesn't mean that the subclass get a copy of the method, which is then recompiled with the scope of the subclass.

Henry



Thanks Henry. That was the problem where I was wrong. I was mistaken of the subclass having a copy of that method.
Hello all,
Please can anyone help me out in this:-




The output is "Animal Animal"

I want to ask if the name field in Animal is private , it cannot be inherited by Dog class, but the getName() method will be inherited. And on call to getName() method , why does it use a "non-inherited variable" instead of the local object instance variable ?
Thanks Jeanne for your reply , i thought the same for it being false but on Free Exam on whizlabs i got it incorrect .
Hello,
In following code


The question I want to ask is if this is true? :-
A "HAS-A" C is true?

though C is not directly member of A though?

and another question is if anyone can tell that regarding multiple-choice questions in OCPJP 6 , the choice is given like (Choose 3(or2)) or (Choose all that apply)?
Thanks Pawel , just used extended code to explain the concept of equals... your code is prefectly more efficient too..