Win a copy of Five Lines of Code this week in the OO, Patterns, UML and Refactoring forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Liutauras Vilda
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • fred rosenberger
  • salvin francis
Bartenders:
  • Piet Souris
  • Frits Walraven
  • Carey Brown

Queries

 
Ranch Hand
Posts: 99
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 104
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
She still doesn't approve of my superhero lifestyle. Or this shameless plug:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
    Bookmark Topic Watch Topic
  • New Topic