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  Contact Us     Login  |  Sign Up                      
tip   To Refer this Site to Your Friends   Click Here
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 ?    13 Yes 12 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 ?    16 Yes 7 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 ?    1 Yes 10 No
Karun
 
  Re: Can we override static methods?
Answer
# 4
YES, WE CAN OVERRIDE STATIC METHODS BY STATIC METHODS
 
Is This Answer Correct ?    9 Yes 3 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 ?    8 Yes 4 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 ?    3 Yes 3 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 ?    12 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 ?    1 Yes 3 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 ?    7 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 ?    4 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 ?    4 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 ?    5 Yes 0 No
Neha Jain
 
  Re: Can we override static methods?
Answer
# 13
We can't override the static method we can only redefine 
them
 
Is This Answer Correct ?    0 Yes 1 No
Devender Negi
 
  Re: Can we override static methods?
Answer
# 14
Yes, We can! see Below---
public  class Final1 {
	public static void mone() {
	System.out.println("Iam in final method of          
super class");
	}
}
public  class Final extends Final1{
	
	public static void mone() {
		System.out.println("Iam in final method of 
sub class");
	}
public static void main(String a[]) {
Final f = new Final();
f.mone();
}
	
}
 
Is This Answer Correct ?    0 Yes 2 No
Rambabu
 
  Re: Can we override static methods?
Answer
# 15
Static methods can be hidden it cannot be overriden
 
Is This Answer Correct ?    0 Yes 0 No
Edward Sudhaharchennai
 

 
 
 
Other Core Java Interview Questions
 
  Question Asked @ Answers
 
How to call static method? Epoch3
Explain method overloading and overriding?  6
what are the oops concept in java explain with real time examples Tech-Mahindra4
what is dynamic method dispatch ? Fidelity11
I need to know about complete topic in java's collections i with an examples TCS1
what is Thread?  6
Explain the term serialization? Wipro6
What are the limitations for static method?  3
Explain difference between final, finally and finalize?  2
Name the types of 'priority'?  2
I want to know the host name and provider name for tcs web mail TCS3
Define Compiling?  2
What is the difference between java and .Net? Bosch5
Can a Byte object be cast to a double value?  2
What is the use of ?this??  4
Difference between Superclass and Subclass?  2
how cani read a command line argument?(usingfile object).  3
Can an exception be rethrown? Wipro4
How many statements can be created with one connection in jdbc? Polaris2
If there is no implementation of MARKER INTERFACE in java. Then how compiler come to know about specification.  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 interview questions urls   External Links 
   
Copyright © 2007  ALLInterview.com.  All Rights Reserved.

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