my dog learned polymorphism*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Queries Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Queries" Watch "Queries" New topic
Author

Queries

Smitha Ballikar
Ranch Hand

Joined: Aug 02, 2005
Posts: 99
Couple of queries:

1.

Given the following classes, defined in same file named SubClass.java

class BaseClass{
static void sayHello(){
System.out.println("Hi pal!!!, I am BaseClass");
}
}

public class SubClass extends BaseClass{
static void sayHello(){
System.out.println("Hi pal!!!, I am SubClass");
}

public static void main(String [] arg){
BaseClass bc = new SubClass();
bc.sayHello();
}
}

What happens when we compile and run SubClass.java?

A.Does't compile as you cannot override static methods.
B.Compiles but fails at runtime.
C.Compiles and runs successfully with output :
Hi Pal!!!, I am BaseClass
D.Compiles and runs successfully with output :
Hi Pal!!!, I am SubClass

Answer Given:C (Is it because its static??)
I feel D


2.
Given the code, what is the output

public class MyClass{
static int i = 10;

public static void main(String[] arg){
static int i = 20;

System.out.println("i is :"+i);
}
}

A.Code Does't Compile.
B.Code Compiles but error will be generated at runtime.
C.Code Compiles & runs, output is
i is : 10
D.Code compiles & runs, output is
i is : 20

Answer Given A.
Cant you declare an instance variable as a local variable(Is shadowing not possible)
It can be done in base class and derived class I feel

Let me know..

Cheers
Sergei Iakhnin
Ranch Hand

Joined: Nov 09, 2004
Posts: 53
1. Static methods are bound at compile time so the method that's associated with the reference type will be executed as opposed to the method that's associated with the runtime type of the object.

2. Local variables cannot be static.


SCJP 1.4, SCWCD 1.4
Arulkumar Gopalan
Ranch Hand

Joined: Oct 13, 2003
Posts: 104
For Q1:

Answer Given:C (Is it because its static??)

Yes, it is because of static.

I feel D

No, it can't be. Simple thumb rule is: Overriding can not be applied to static methods, since they are class methods and nothing to do with instance variables.

I think it did confuse you as the method is accessed by an object reference instead of using class name.

Just feel Java:-)
[ August 04, 2005: Message edited by: Arulkumar Gopalan ]

Anbudan & Mahalo,<br />Arul<br /> <br />-Not a sun certified Java professional :-)
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Queries
 
Similar Threads
please help me
overriding static method
Constructors.
Overriding static methods - pl explain!
Doubt in overrididng