Monday, February 23, 2009

final method issues in overriding


Demo 1:
// super class final method sub class non-final method
package oops;
class Superclass
{
final void m1()
{
System.out.println("Super class method");
}
}
public class Subclass extends Superclass {

void m1() // compile time error
//can not overide the final method from superclass

{
System.out.println("Sub class method");
}
public static void main(String[] args) {
Subclass s=new Subclass();
s.m1();
}

}

Demo 2:
// super class non-final method, sub class final method
package
oops;
class Superclass
{
void m1()
{
System.out.println("Super class method");
}
}
public class Subclass extends Superclass {

final void m1() //no error
{
System.out.println("Sub class method");
}
public static void main(String[] args) {
Subclass s=new Subclass();
s.m1(); // sub class
}

}


0 comments:

Post a Comment