Tuesday, February 24, 2009

instance methods ,static methods overriding issues



DEMO 1:
// Super class instance method,sub class static method
package oops;

import java.io.FileNotFoundException;
import java.io.IOException;
class Parentclass
{
public void sm()
{
System.out.println("super class");
}
}
public class OREx extends Parentclass{

public static void sm() // compile time error
//static method cannot hide the instance method
{
System.out.println("sub class");
}
public static void main(String a[])throws Exception
{
OREx s=
new OREx();
s.sm();
}
}

Demo 2:
// super class static method sub class instance method
package oops;

import java.io.FileNotFoundException;
import java.io.IOException;
class Parentclass
{
public static void sm()
{
System.out.println("super class");
}
}
public class OREx extends Parentclass{

public void sm() // compile time error
//This instancemethod cannot override the static method
{
System.out.println("sub class");
}
public static void main(String a[])
{
OREx s=
new OREx();
s.sm();
}
}


0 comments:

Post a Comment