Wednesday, 10 February 2016

Final Keyword

By
Questions for which we are about to get answers
How to prevent classes from being inherited?
How to make instance variable constant?

Final keyword can be used with
  • Class
  • Variable
  • Methods
If final is used with Class then we can not inherit that class or it can not be further be extended.final class Example{ //Class definition   }
If final is used with Methods then that method can not be overridden by sub classes
final public void example(){//Method definition}
If final is used with instance variable then we can not change the value of that variable throughout the classfinal int CUSTOMERID = 100;Note: It the convention to use all upper case letter to declare final variable
Will final instance variable be initialized to default value if not initialized ?No,It will result in compilation error .Final variable must be initialized when declared or inside the constructor of class.Note:If final variable is static it can be initialized at the time of declaration or inside the static block
Self study section:Can you answer these question?
1. final class ParentClass{


   protected int parentId;
  public ParentClass(){
   parentId=1000;
   }
 }
 class ChildClass extends ParentClass{
   private int childId;
   public ChildClass(){
   childId=2000;
   }
   public void display(){
   System.out.println("Parent Id:"+parentId+" Child Id:"+childId);
   }
 }
 class Demo{
   public static void main(String args[]){
   ChildClass childOne = new ChildClass();
   childOne.display();
   }
 }

2. class ClassExample{
   protected int classId;
   private final int MAX=10;
   private static int counter;
  public ClassExample(){
   classId=counter++;
   if(counter>MAX){
  MAX=20;
  counter=0;
  }
  System.out.println("MAX:"+MAX+"counter :"+counter);
   }
 }
 class Demo{
   public static void main(String args[]){
   ClassExample exampleOne = new ClassExample();
   }
 }

3 .class ClassExample{
   protected int classId;
   private final int MAX=10;
   private static int counter;
  public ClassExample(){
   classId=counter++;
   if(counter>MAX){
  counter=0;
  }
  System.out.println("MAX:"+MAX+" counter :"+counter);
   }
 }
 class Demo{
   public static void main(String args[]){
   ClassExample exampleOne = new ClassExample();
   }
 }

4.class ClassExample{
   protected int classId;
   private final int MAX=10;
   private static int counter;
  public ClassExample(){
   classId=counter++;
   if(counter>MAX){
  counter=0;
  }
   }
   public final void display(){
  System.out.println("MAX:"+MAX+" counter :"+counter+ " classId:"+classId);
  }
 }
 class DerivedExample extends ClassExample{
   private int derivedId;
   public DerivedExample(){
   derivedId=10;
   }
   public void display(){
   super.display();
   System.out.println("derivedId:"+derivedId);
   }
 }
 class Demo{
   public static void main(String args[]){
   DerivedExample exampleOne = new DerivedExample();
   exampleOne.display();
   }
 }

Answer by comment and i will share correct answer later
Thank you  for reading my blogs 


0 comments :

Post a Comment