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 SCJP questions Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "SCJP questions" Watch "SCJP questions" New topic
Author

SCJP questions

Shweta R Dhaneshwar
Ranch Hand

Joined: May 27, 2005
Posts: 35
Hi,
I have following doubts...please help me solve them
Q.1
public class Test extends Super
{
public static void main(String args[])
{
Test t = new Test();
Super s = (Super) t;
s.method('a');
}

public void method(int i)
{
System.out.println("base int");
}

public void method(char c)
{
System.out.println("base char");
}
}

class Super
{
public void method(int i)
{
System.out.println("super int");
}

public void method(char c)
{
System.out.println("super char");
}
}

Doubt-Is public void method(char c)...overloaded?....what will be the o/p of this code?...please exaplain.


Q
public class Test
{
public static void main(String args[])
{
RuntimeException re = null;

try
{
System.out.println("throwing exception!");
throw re;
}
catch (Exception e)
{
System.out.println("catch exception!");
}
finally
{
System.out.println("finally!");
}
}
}
Doubt-throw re,throws a NullPointerException...please exp[lain how this code works


Q.
public class Test extends Super
{
public static void main(String args)
{
System.out.println("Test main!");
}
}

class Super
{
public static void main(String args[])
{
System.out.println("Super main!");
}
}

Doubt /p:Super main!......how???...explain

Thanks,


Shweta Dhaneshwar.<br />SCJP 1.4 90%
Philip Heller
author
Ranch Hand

Joined: Oct 24, 2000
Posts: 119
In your first question, the heart of the matter is the three lines:


Here's how I like to think about this situation: Objects have class, references have type. The class of an object is permanantly determined at construction time. To learn an object's class, just look at the constructor that was used to build it. In our case, the object's class is Test, because "new Test()" was called.

A reference to an object may have any type that is compatible with that object. A type is a class, and interface, an array, or (as of 5.0) an enum. In your example, s is a reference of type Super; it referes to an object of class Test.

How do you figure out which version of an overridden method is actually called? Ignore the type of the reference. Look at the class of the object to which the reference points. That's why the code prints "base char".

=================================================================

In your second question, you were essentially doing this:



This is just a guess, but I think the JVM's exception-throwing mechanism is trying to call some method on the exception, thus generating the null-pointer exception. Your catch block will catch any exception type that is "instanceof" Exception, and that includes NullPointerException. Note that if you change your caught type to RuntimeException, you get a different result.

There's a nice animated illustration of the whole try/catch mechanism in Chapter 11 of "Ground-Up Java".

=================================================================

In your third question, I need to know how you invoked your application. Did you type "java Test" or "java Super"?

Since you're surprised by the result, I'm guessing you typed "java Test". Notice the declaration of main() in class Test. The arg is String, not String[]. So what gets called is the inherited version from Super.


Consultant to SCJP team.<br />Co-designer of SCJD exam.<br />Co-author of "Complete Java 2 Certification Study Guide".<br />Author of "Ground-Up Java".
Jon Egan
Ranch Hand

Joined: Mar 24, 2004
Posts: 83
Originally posted by Philip Heller:
How do you figure out which version of an overridden method is actually called? Ignore the type of the reference. Look at the class of the object to which the reference points.


This is true for overridden methods, but there's a case that's very similar to overridden methods that behaves differently... when the methods are defined as static, a call to that method will use the implementation defined by the type of the reference, not the type of the underlying object. I do remember that is not technically overriding, but since it's so similar it "feels" a bit like an exception, and one worth noting in the SCJP forum, of all places.

-- Jon
amit taneja
Ranch Hand

Joined: Mar 14, 2003
Posts: 810
Originally posted by Philip Heller:
In your third question, I need to know how you invoked your application. Did you type "java Test" or "java Super"?

Since you're surprised by the result, I'm guessing you typed "java Test". Notice the declaration of main() in class Test. The arg is String, not String[]. So what gets called is the inherited version from Super.


how can a static method of super be inherited in subclass ?
and we have also not called the method Super.main(String[]); either ?

bit confused ? ..pls clarify


Thanks and Regards, Amit Taneja
Ravith Botejue
Greenhorn

Joined: May 25, 2005
Posts: 6
Originally posted by amit taneja:

"So what gets called is the inherited version from Super."
how can a static method of super be inherited in subclass ?
and we have also not called the method Super.main(String[]); either ?

bit confused ? ..pls clarify


I tried the following example code

public class test1{
public static void method1(){
System.out.println("test1 method1()");
}

public static void method1(int i){
System.out.println("test1 method1(int i)");
}

public static void main(String args){
test1 t = new test1();
System.out.println("test1");
//super s = t;
method1();
method1(1);
}
}

public class test2 extends test1{
public static void method1(){
System.out.println("test2 method1()");
}

public static void method1(int i){
System.out.println("test2 method1(int i)");
}

public static void main(String[] args){
test2 t = new test2();
System.out.println("test2");
//super s = t;
method1();
method1(1);
}
}
the output is
test2
test2 method1()
test2 method1(int i)
so it seems like the main method from the super class is overridden by the main method in the subclass.

when i removed teh main method in the sub class

it printed

test1
test1 method1()
test1 method1(int i)

some thin messy is happening inside....

any way ithink your doubt is cleared


RB
amit taneja
Ranch Hand

Joined: Mar 14, 2003
Posts: 810
Absolutely not....
my doubt is not clear...and infact can you tell me how do you compile the code...

first of all static class are not over-ridden but re-write..

if you have written the both the class in the same file ..then the program cant be compiled as in your case you have 2 public class in the same file

and if you have seprate files for each of them then i will also be don't have problem .and wouldn't post here

see your code carefully

anyway...can anybody tell the correct answer...
 
wood burning stoves
 
subject: SCJP questions
 
Similar Threads
constructor doubt
question from Sun Guoqiao's homepage
Question 58 jiris on casting
exception doubt
one doubt