Final and Super keyword
Final: ---
Final is a keyword can be used before the name of
variable, method and class.
Final variable: --- Final variable is just like
constant variable this is initialized only once and the value of final variable
remains constant in whole script.
Final method: --- Final method cannot be overridden in
any derived class.
Final class: --- Final class never gets inherited
(extends) means you can’t make sub classes of final class.
final class xyz
{
final int a=20;
final void fun()
{
System.out.println("this
is final method");
}
}
Super key:--
It is used to access the
member variable, method and constructor of base class in derived class.
Note: ---
super keyword is only used as first
line in constructor of derived class.
import java.util.*;
class xyz
{
xyz()
{
System.out.println("this
is constructor of class xyz");
}
int a;
void fun()
{
System.out.println("a===="+a);
}
}
class pqr extends xyz
{
pqr()
{
super(); //calls
constructor of xyz
super.a=20; //access
variable of xyz
super.fun(); //calls
function of xyz
}
}
public class abc
{
public static void
main(String as[])
{
pqr p= new
pqr();
}
}
No comments:
Post a Comment