final keyword is used to Break the inheritibility.
No longer any class can inherit from the class which is defned final.
final class One
{
void Ofun()
{
System.out.println("This is class One");
}
}
class Two extends One // Invalid
{
void Tfun()
{
System.out.println("This is class Two");
}
}
class Three extends Two
{
void Thfun()
{
System.out.println("This is class Three");
}
}
public class Test
{
public static void main(String args[])
{
Three t1 = new Three();
One o1 = new One();
t1.Ofun(); // Can not be accessed.
o1.Ofun(); // Can access base class method.
t1.Tfun();
t1.Thfun();
}
}
One class can not inherit to class Two.
Note:- Inorder to access the method inside the base class One we have to create the object of the base class.
Comments
Post a Comment