File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Q from javacross simulation Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Q from javacross simulation" Watch "Q from javacross simulation" New topic
Author

Q from javacross simulation

madhu kumar
Greenhorn

Joined: Jan 05, 2001
Posts: 20
the following q I found at javacross simulation
Which of the following statements are true?
1]. Methods cannot be overriden to be more private
2]. Static methods cannot be overloaded
3]. Private methods cannot be overloaded
4]. An overloaded method cannot throw exceptions not checked in the base class
can anyone tell me the result.
Bharatesh H Kakamari
Ranch Hand

Joined: Nov 09, 2000
Posts: 198
1]. Methods cannot be overriden to be more private
True

2]. Static methods cannot be overloaded
False
3]. Private methods cannot be overloaded
False
4]. An overloaded method cannot throw exceptions not checked in the base class
False
Rishi Yagnik
Ranch Hand

Joined: Jan 04, 2001
Posts: 84
hi madhu
can u please get me the address for java cross exam
thanks in advance

bye
madhu kumar
Greenhorn

Joined: Jan 05, 2001
Posts: 20
Hi,
it is http://www.javacross.com I guess the site is under construction but you can find an entry level simulation.
Cherry Mathew
Ranch Hand

Joined: Dec 26, 2000
Posts: 159
i tried the site and found that many questions in the site is wrong and also itz very difficult to analyze the answers
madhu kumar
Greenhorn

Joined: Jan 05, 2001
Posts: 20
Hi,
can anyone please explain the solutions for me. I couldn't get them.
Also the site seems to be under construction may be the analysis of the exam should be somewhat clear as Cherry had pointed out.
sachin patel
Ranch Hand

Joined: Nov 28, 2000
Posts: 75
hi madhu,
here is the explanation according to me......
1]. Methods cannot be overriden to be more private
True
if it was possible to override the methods to be more private then there would have been no use of polymorphism....
eg..
class A
{
public aMethod()
{
}
}
class B
{
private aMethod()
{
}
public static void main(String [] arg)
{
A a = new B();
a.aMethod(); // problem here
}
}
for this during compile time compiler thinks its OK to call aMethod() because the refrence is to A class and aMethod() is public.. and it is not known untill runtime that actually there is object of B class that a is refrencing to... so during runtime there would be a problem for access....
2]. Static methods cannot be overloaded
False
eg. Math has methods that are static and overloaded... for eg. abs(int), abs(long), abs(float), abs(double)......
3]. Private methods cannot be overloaded
False
overloading has nothing to do with access modifiers... overloaded method has to have different argumentlist and same method name.,. that all is needed to overload a method
4]. An overloaded method cannot throw exceptions not checked in the base class
False
This is to confuse between overridding and overloaded methods..
the fact is overriding method cannot throw checked exception not checked in base class

hope this helps

------------------
Sachin,
****************************************************
Learn from others mistakes. Life is too short to make all yourself.
****************************************************
[This message has been edited by sachin patel (edited January 06, 2001).]


Sachin,<P>****************************************************<BR>Learn from others mistakes. Life is too short to make all yourself.<BR>****************************************************
madhu kumar
Greenhorn

Joined: Jan 05, 2001
Posts: 20
Hi sachin,
thankyou very much. Iam just beginning to feel comfortable with these concepts.
Hope I get all my queries in same fashion.
Regards
Madhu
madhu kumar
Greenhorn

Joined: Jan 05, 2001
Posts: 20
Hi,
can anybody explain me why private methods cannot be overloaded
Thanks in advance
Srinivasan Krishnan
Greenhorn

Joined: Nov 07, 2000
Posts: 8
Private Methods belongs only to that class, not even to the extending class, and so it cannot be overridden. Hope I am right
bill bozeman
Ranch Hand

Joined: Jun 30, 2000
Posts: 1070
Private methods can be overloaded, not overridden.
private void aMethod(int x){}
private void aMethod(float x){}
private int aMethod(long x){return 0;}
public void aMethod(){}
static void aMethod(double x){}
These are all valid in the same class, try it and see. The reason is that overloading has to do more with playing with names. The compiler can see these as all being separate methods because it takes different arguments. If you call aMethod and pass in an int, it knows that you want the first method. Therefore it is not confused, and it passes. It is not good programming practice to overload methods and give them different return types or differnt access modifiers, or making some static and some non-static becuase it is confusing to the user, even if it is not confusing to the compiler.
Bill
Pratap Reddy
Ranch Hand

Joined: Jan 05, 2001
Posts: 36
When you say Private methods can be overloaded, not overridden? I tried following example ,
class superclass
{
//methods
private int method(int cx)
{
return (cx+1);
}

}

public class subclass extends superclass
{

public static void main(String args[])
{
superclass SuperObj= new superclass();
subclass SubObj = new subclass();
int x = SubObj.method(10);
System.out.println("The value of x:" +x);
}

//methods
private int method(int dx )
{
return (dx+2);
}


}
I tried and compiled It works fine output: 12. I'm I not doing overridden here for method(int ). Can some one explain this.
Jane Griscti
Ranch Hand

Joined: Aug 30, 2000
Posts: 3141
Hi Pratap ...
'Private' methods cannot be overridden because they are not inherited... the compiler does not check the superclass for a corresponding method signature.
In your example, try changing the method in the subclass to something which would normally cause a compiler error for an overriding class ie void method(int dx) ... it will compile without an error.
If you remove the 'private' from the superclass method and compile; overriding applies and you get an error.
Hope that helps.
------------------
Jane
The cure for boredom is curiosity.
There is no cure for curiosity.
-- Dorothy Parker


Jane Griscti
SCJP, Co-author Mike Meyers' Java 2 Certification Passport
Pratap Reddy
Ranch Hand

Joined: Jan 05, 2001
Posts: 36
As per my knowledge overriding means , a method having same signature & name exists both in superclass and subclass. Then it is called override. That's what I am doing in my example.
If I try this statement Superclass x = new Subclass.
x.method(10).
Then it gives me error. Correct me. Thanks in advance.
bill bozeman
Ranch Hand

Joined: Jun 30, 2000
Posts: 1070
Pratap, you are correct when you say "As per my knowledge overriding means , a method having same signature & name exists both in superclass and subclass."
But also remember that a private method is not inherited by the subclass, so if it is not even seen in the subclass, how can you override it? What you are basically doing is creating a new method in the subclass that happens to have the same name as the one in the superclass, but it is not overriding.
As Jane said, try making the method in the subclass not follow one of the overriding rules. If the super class returns an int as an argument, make the subclass return a long. Or make the subclass method throw IOException. Neither of these are allowed with overriding methods because they have to have the same signature and they throw checked exceptions that were not thrown in the overridden method. You will see that both still compile becuase the overridden rules do not apply, because you can't override private methods.
This can further be seen when you try to apply polymorphism. If you created a subclass and then upcast it to the superclass, if you call an overridden method you will get the method of the subclass. Try it here and see what you get.
Bill
[This message has been edited by bill bozeman (edited January 09, 2001).]
Jane Griscti
Ranch Hand

Joined: Aug 30, 2000
Posts: 3141
Hi Pratap,
Yes. If a method exists in a superclass and want the subclass to have a different implementation; you can 'override' the superclass method. A legal override must pass the following compiler checks:

  • the method signatures (name and parameter list) must be the same in the superclass and subclass
  • the method return type must be the same for both methods
  • the subclass method cannot have an access modifier which is weaker than that provided by the superclass method ie if the superclass method is 'public' the subclass method must be 'public'; if the superclass method is 'protected' the subclass method can be 'protected' or 'public'
  • the subclass method cannot throw any checked exceptions which were not originally originally handled by the superclass method

  • Here's another example:
    <code>
    class Super {

    // a 'private' method which cannot be 'overridden'

    private void privMethod() {}


    // a non-private method which can be 'overridden'

    void nonPrivMethod() {}
    }
    class TestOverride extends Super {

    // method with same name as 'private' method in superclass
    // with a different return type; does NOT produce a compile

    int privMethod() { return 0; }


    // method with same name as 'non-private' method in
    // superclass with a different return type; DOES cause a
    // compile-error as rules state overridden method must have
    // same name, return type and parameters

    int nonPrivMethod() { return 0;}
    }
    </code>
    When the above is compiled, there is one error:
    <code>
    TestOverride.java:20: nonPrivMethod() in TestOverride cannot
    override nonPrivMethod() in Super; attempting to use
    incompatible return type
    found : int
    required: void
    int nonPrivMethod() { return 0;}
    ^
    1 error

    </code>
    The error is the result of an 'overriding' rule being broken. No such error occurs with the 'privMethod()' declaration because the compiler does not expect a private method to be overridden; it doesn't apply the rules.
    JLS§8.4.6.3

    Note that a private method cannot be hidden or overridden in the technical sense of those terms. This means that a subclass
    can declare a method with the same signature as a private method
    in one of its superclasses, and there is no requirement that
    the return type or throws clause of such a method bear any
    relationship to those of the private method in the superclass.

    Hope that helps.

    ------------------
    Jane
    The cure for boredom is curiosity.
    There is no cure for curiosity.
    -- Dorothy Parker
madhu kumar
Greenhorn

Joined: Jan 05, 2001
Posts: 20
Thanx all I have come to know some good concepts.
Pratap Reddy
Ranch Hand

Joined: Jan 05, 2001
Posts: 36
Thanks..Jane Griscti , bill bozeman. Now I am with you.
smitha
Greenhorn

Joined: Jan 11, 2001
Posts: 3
hi, can someone tell me the url for the cross simulation and other simulations??
Jane Griscti
Ranch Hand

Joined: Aug 30, 2000
Posts: 3141
Hi smitha,
You'll find a link to all the Mock Exams on Maha Anna's page at http://www.javaranch.com/maha/_Mock_Exams/_mock_exams.html
Note that JavaRanch has a Name Policy. Please reveiw the policy and re-register with a name that complies with the rules.
Thanks for you co-operation.
Jane
Noel Hinders
Ranch Hand

Joined: Jan 13, 2001
Posts: 44
Hi smita,
Its http://www.javacross.com you will find it at the start of this post itself I wonder u saw it, anyhow go thru the post before u post one.
Noel.
smitha
Greenhorn

Joined: Jan 11, 2001
Posts: 3
Thanks noel,
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Q from javacross simulation