ALLInterview.com :: Home Page KalAajKal.com
 Advertise your Business Here     
Browse  |   Placement Papers  |   Company  |   Code Snippets  |   Certifications  |   Visa Questions
Post Question  |   Post Answer  |   My Panel  |   Search  |   Articles  |   Topics  |   ERRORS new
   Refer this Site  Refer This Site to Your Friends  Site Map  Bookmark this Site  Set it as your HomePage   interview questions urls   External Links  Contact Us     Login  |  Sign Up                      
tip       Ask Questions on ANYTHING, that arise in your Daily Life at     FORUM9.COM
Google
 
Categories  >>  Software  >>  Core Java  >>  Java J2EE  >>  Java Related
 
 


 

 
 Core Java interview questions  Core Java Interview Questions
 Advanced Java interview questions  Advanced Java Interview Questions
 Swing interview questions  Swing Interview Questions
 EJB interview questions  EJB Interview Questions
 Servlets interview questions  Servlets Interview Questions
 Struts interview questions  Struts Interview Questions
 JDBC interview questions  JDBC Interview Questions
 JMS interview questions  JMS Interview Questions
 SunOne interview questions  SunOne Interview Questions
 J2EE interview questions  J2EE Interview Questions
 Weblogic interview questions  Weblogic Interview Questions
 Websphere interview questions  Websphere Interview Questions
 Java Networking interview questions  Java Networking Interview Questions
 Java J2EE AllOther interview questions  Java J2EE AllOther Interview Questions
Question
Can we override static methods?
 Question Submitted By :: Guest
I also faced this Question!!     Rank Answer Posted By  
 
  Re: Can we override static methods?
Answer
# 1
no we cannot overriding the static methods .
if we override the static method it will gives error
 
Is This Answer Correct ?    2 Yes 3 No
P.sreekiran
 
  Re: Can we override static methods?
Answer
# 2
Yes, V can override static methods
But overridding and overridden methods must static
 
Is This Answer Correct ?    5 Yes 0 No
Venkateswara Reddy
 
 
 
  Re: Can we override static methods?
Answer
# 3
yes ,we can override the static methods to a nonstatic or 
static methods
 
Is This Answer Correct ?    0 Yes 1 No
Karun
 
  Re: Can we override static methods?
Answer
# 4
YES, WE CAN OVERRIDE STATIC METHODS BY STATIC METHODS
 
Is This Answer Correct ?    1 Yes 0 No
Guest
 
  Re: Can we override static methods?
Answer
# 5
yes ,we can override the static methods to static methods 
only.
 sorry for the previous my wrong answer.:((((
 
Is This Answer Correct ?    1 Yes 0 No
Karun
 
  Re: Can we override static methods?
Answer
# 6
Yes, Static Methods can be overridden but it won't give 
overridden behaviour. Overiding execution happens based on 
the runtime object.
 
Is This Answer Correct ?    1 Yes 1 No
N.suresh Babu
 
  Re: Can we override static methods?
Answer
# 7
It may seems to be overriding the static methods, but the
real fact is HIDING. 

             class Foo {
    public static void classMethod() {
        System.out.println("classMethod() in Foo");
		
    }

    public void instanceMethod() {
        System.out.println("instanceMethod() in Foo");
    }
}

class Bar extends Foo {
    public static void classMethod() {
        System.out.println("classMethod() in Bar");
		
    }

    public void instanceMethod() {
        System.out.println("instanceMethod() in Bar");
    }
}
 
class StaticHiding {
    public static void main(String[] args) {
        Foo f = new Bar();
		
        f.instanceMethod();
        f.classMethod();


when u run this program output will be:
   
          instanceMethod() in Bar
          classMethod() in Foo.

              Here if u say it to be overriding then the
subclass ie., Bar class having static classMethod() should
be executed. But the fact here is Foo class static
classMethod() is executed. 
 
                So its method HIDING and not method
overriding.. 
 
                 I hope i have given answer to my best if
anyone feels wrong plz do post ur suggestions..
 
Is This Answer Correct ?    1 Yes 0 No
Sadheez
 
  Re: Can we override static methods?
Answer
# 8
yes we can override static method but can not overload that 
is called method hiding nt overloading.
 
Is This Answer Correct ?    0 Yes 1 No
Neha Jain
 
  Re: Can we override static methods?
Answer
# 9
Neha,

I didn't get your answer.
You can overload as well as override static methods, but 
only with static methods.

I have tried it and it is getting compiled.

Example is here.
*********************************************************
public class abc
{
	public static void main(String args[])
	{

	}
	public static void trss()
	{

	}
	public static void trss(int i)
	{
	}

}
*********************************************************
 
Is This Answer Correct ?    2 Yes 0 No
Yogesh Gandhi
 
  Re: Can we override static methods?
Answer
# 10
we cannot override a static method but we can overload a 
static method.
Ex: override is not possible
class Foo {
    public static void classMethod() {
        System.out.println("classMethod() in Foo");
		
    }

    public void instanceMethod() {
        System.out.println("instanceMethod() in Foo");
    }
}

class Bar extends Foo {
    public static void classMethod() {
        System.out.println("classMethod() in Bar");
		
    }

    public void instanceMethod() {
        System.out.println("instanceMethod() in Bar");
    }
}
 
class StaticHiding {
    public static void main(String[] args) {
        Foo f = new Bar();
		
        f.instanceMethod();
        f.classMethod();


when u run this program output will be:
          instanceMethod() in Bar
          classMethod() in Foo.

Ex: overload is possible
public class abc
{
	public static void main(String args[])
	{

	}
	public static void trss()
	{

	}
	public static void trss(int i)
	{
	}

}
 
Is This Answer Correct ?    1 Yes 1 No
Madan Mohanp
 
  Re: Can we override static methods?
Answer
# 11
Thanks for addition.
You are correct.

Static method cannot be overridden.
Overloading of static methods is possible.
 
Is This Answer Correct ?    1 Yes 0 No
Yogesh Gandhi
 
  Re: Can we override static methods?
Answer
# 12
dear yogesh 
sory for wrong answer we can not override static method 
with static method that known as method hiding.plz view 
following example
Briefly, when you override a method, you still get the 
benefits of run-time polymorphism, and when you hide, you 
don't. So what does that mean? Take a look at this code:

class Foo {
    public static void classMethod() {
        System.out.println("classMethod() in Foo");
    }

    public void instanceMethod() {
        System.out.println("instanceMethod() in Foo");
    }
}

class Bar extends Foo {
    public static void classMethod() {
        System.out.println("classMethod() in Bar");
    }

    public void instanceMethod() {
        System.out.println("instanceMethod() in Bar");
    }
}
 
class Test {
    public static void main(String[] args) {
        Foo f = new Bar();
        f.instanceMethod();
        f.classMethod();
    }
}
If you run this, the output is
instanceMethod() in Bar
classMethod() in Foo
 
Is This Answer Correct ?    2 Yes 0 No
Neha Jain
 

 
 
 
Other Core Java Interview Questions
 
  Question Asked @ Answers
 
Which One is optimal to choose ? Syncronized hash map or Hash table with single thread model? How can a hash map syncronized with out using syncrozed blocks in programm? Four-soft2
How applets will communicate with each other?  1
C and C++ has constructors and distructors, why does Java does not have distructors? T3-Softwares1
What is encapsulation? Elaborate with example? BMC1
What is a reflection package?  2
write a simple program inheritance? Polaris1
how session will be expired? Infosys3
what is the use of finalize()Method please explain with an example  1
What is synchronization? How it can be achieved?  2
whats the life cycle of jsp Satyam2
What are different types of access modifiers?  1
Hi Friends.. can any one provide the real time example for methodoverloading and methodoverriding ......... Honeywell1
when to use abstract class and when to use interface? IonIdea3
What is a compilation unit?  1
what is the difference between cd & dvd ? Satyam4
Why const and goto are reserved keyword in java?  1
How to transfer data from an Applet to Servlet ?  1
What is the difference between a Window and a Frame?  1
which swing component is similar to rich text box in .net/vb  1
What is the need of "creating and throwing an UserdefinedException" when the "Exception" class is already available?  2
 
For more Core Java Interview Questions Click Here 
 
 
 
 
 
   
Copyright Policy  |  Terms of Service  |  Help  |  Site Map 1  |  Articles  |  Site Map  |   Site Map  |  Contact Us
   
Copyright © 2007  ALLInterview.com.  All Rights Reserved.

ALLInterview.com   ::  Forum9.com   ::  KalAajKal.com