• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

SCJP questions

 
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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,
 
author
Posts: 119
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 84
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
 
Ranch Hand
Posts: 817
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
 
amit taneja
Ranch Hand
Posts: 817
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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...
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic