Advertisement
Rahmadnet

final-keywords

Feb 13th, 2019
136
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.09 KB | None | 0 0
  1. package keyword;
  2.  
  3. public class keywordFinal
  4. {
  5.     public static void main(String[] args)
  6.     {
  7.         ClassA clA = new ClassA(20, 90);
  8.         clA.dispFinal();
  9.         clA.dispMyInt();
  10.        
  11.         CLassB clB = new CLassB();
  12.         clB.setMyInt(50);
  13.         clB.dispMyInt();
  14.     }
  15. }
  16.  
  17. // OUTPUT
  18.  
  19. //The value of final variable : 20
  20. //The value MyInt : 90
  21. //The value of MyInt for Class B : 50
  22.  
  23. package keyword;
  24.  
  25. class ClassA
  26. {
  27.     final int finInteger;
  28.     protected int myInt;
  29.     public ClassA(int x, int y)
  30.     {
  31.         finInteger = x; // initialize for only one time
  32.         myInt = y;
  33.     }
  34.    
  35.     public ClassA()
  36.     {
  37.         this(0, 0);
  38.     }
  39.    
  40.     final public void dispFinal() // final methods can not be overriden
  41.     {
  42.         System.out.println("The value of final variable : "+ finInteger);
  43.     }
  44.    
  45.     public void dispMyInt()
  46.     {
  47.         System.out.println("The value MyInt : "+ myInt);
  48.     }
  49. }
  50.  
  51. final class CLassB extends ClassA // final class will not be extended
  52. {
  53.     protected int myInt;
  54.     public void setMyInt(int x)
  55.     {
  56.         myInt = x;
  57.     }
  58.    
  59.     public void dispMyInt() // overriden method
  60.     {
  61.         System.out.println("The value of MyInt for Class B : "+ myInt);
  62.     }
  63. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement