slide1 n.
Download
Skip this Video
Loading SlideShow in 5 Seconds..
Java PowerPoint Presentation
Download Presentation
Java

Loading in 2 Seconds...

play fullscreen
1 / 26
harlan-briggs

Java - PowerPoint PPT Presentation

72 Views
Download Presentation
Java
An Image/Link below is provided (as is) to download presentation

Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.

- - - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript

  1. Java Powered By: Arvind Department of Computer science and engineering Radha govind group of institutions,meerut

  2. Final Variable and Methods • All the methods and variables can be overridden by default in subclass. if we wish to prevent the subclasses from overriding the members of super class, we can declare them as final using the keyword final as a modifier. final int size=100; final void show() { ……………. } Powerd By: Arvind

  3. Making a method final ensures that the functionality defined in this method will never be altered in any way. similarly, the value of a final variable can never be changed. classPersonalLoan{  public final String getName(){      return "personal loan";  } } class CheapPersonalLoan extends PersonalLoan{       public final String getName(){         return "cheap personal loan"; //compilation error: overridden method is final     } } Powerd By: Arvind

  4. classBike{finalintspeedlimit=90;//final variable  void run() {speedlimit=400;  }publicstaticvoid main(String args[]){classBike{finalintspeedlimit=90;//final variable  void run() {speedlimit=400;  }publicstaticvoid main(String args[]){ Bikeobj=new  Bike();  obj.run();   }  } Powerd By: Arvind

  5. classBike{finalvoid run() { System.out.println("running"); } }   class Honda extendsBike{void run() { System.out.println("running safely with 100kmph"); }publicstaticvoid main(String args[]) {   Honda honda= new Honda();  honda.run();     }   } Output:Compiletime error Powerd By: Arvind

  6. Final classes • Sometimes we may like to prevent a class being further sub classes for security reasons. A class that can not be sub classed is called a final class. • This is achieved in java by using the keyword final as follows. final class Aclass{ …………… } Powerd By: Arvind

  7. final class Bclass extends Someclass{ …………. } Any attempt to inherit these classes will causes an error and the compiler will not allow it. final class PersonalLoan{ } classCheapPersonalLoan extends PersonalLoan{  ………………… //compilation error: cannot inherit from final class } Powerd By: Arvind

  8. Q1. Is final method inherited?Ans: Yes, final method is inherited but you cannot override it. classBike{ finalvoid run() { System.out.println("running..."); }}class Honda extendsBike{publicstaticvoid main(String args[]) {new Honda().run();   }} Powerd By: Arvind

  9. Q2.What is blank or uninitialized final variable?Ans: A final variable that is not initialized at the time of declaration is known as blank final variable. • It can be initialized only in constructor. Example of blank final variable classStudent{int id;  String name;  final String PAN_CARD_NUMBER;  } Powerd By: Arvind

  10. Q3.Can we initialize blank final variable?Ans: Yes, but only in constructor. For example: classBike{finalintspeedlimit; //blank final variable   Bike() {speedlimit=70;    System.out.println(speedlimit);    }  publicstaticvoid main(String args[]) {new Bike();   }  } Output:70 Powerd By: Arvind

  11. static final blank variable • A static final variable that is not initialized at the time of declaration is known as static blank final variable. It can be initialized only in static block. class A{  staticfinalint data; //static blank final variable  static{ data=50; }publicstaticvoid main(String args[]){ System.out.println(A.data);   }   } Powerd By: Arvind

  12.  What is final parameter?If you declare any parameter as final, you cannot change the value of it. class Bike{  int cube(finalint n){     n=n+2;//can't be changed as n is final     n*n*n;    }  publicstaticvoid main(String args[]){      Bike b=new Bike();  b.cube(5);   }  } Output: Compile time error Powerd By: Arvind

  13. Static • The static keyword is used in java mainly for memory management. We may apply static keyword with variables, methods, blocks and nested class. The static keyword belongs to the class than instance of the class. • The static can be: • variable (also known as class variable) • method (also known as class method) • block • nested class Powerd By: Arvind

  14. Static Variable: if you declare any variable as static, it is known as static variable. • The static variable can be used to refer the common property of all objects (that is not unique for each object) e.g. company name of employees, college name of students etc. • The static variable gets memory only once in class area at the time of class loading. Advantage of static variable: it makes your program memory efficient. Powerd By: Arvind

  15. classStudent{introllno;  String name;   String college="ITS"; }classStudent{introllno;  String name;   String college="ITS"; } • Suppose there are 500 students in my college, now all instance data members will get memory each time when object is created.All student have its unique rollno and name so instance data member is good.Here, college refers to the common property of all objects.If we make it static,this field will get memory only once. • Static property is shared by all the objects. Powerd By: Arvind

  16. classStudent{introllno;     String name;  static String college =“RGGI";  Student(intr,String n) {rollno = r;   name = n;     }  void display (){ System.out.println(rollno+" "+name+" "+college);}publicstaticvoid main(String args[]) { Student s1 = new Student (111,"Karan");   Student s2 = new Student (222,"Aryan");   s1.display();   s2.display();   }} Powerd By: Arvind

  17. Output: 111 Karan RGGI 222 Aryan RGGI Powerd By: Arvind

  18. static method • If you apply static keyword with any method, it is known as static method • A static method belongs to the class rather than object of a class. • A static method can be invoked without the need for creating an instance of a class. • static method can access static data member and can change the value of it. Powerd By: Arvind

  19. classStudent{introllno;   String name;  static String college = "ITS";  staticvoid change() {   college = “RGGI"; }     Student(int r, String n) {rollno = r;       name = n;       }   Powerd By: Arvind

  20. void display () { System.out.println(rollno+" "+name+" "+college); }void display () { System.out.println(rollno+" "+name+" "+college); } publicstaticvoid main(String args[]) {Student.change();      Student s1 = new Student (111,"Karan");      Student s2 = new Student (222,"Aryan");      Student s3 = new Student (333,"Sonoo");      s1.display();      s2.display();      s3.display();      }  }   Powerd By: Arvind

  21. OUTPUT: • 111 karan RGGI 222 Aryan RGGI 333 Sonoo RGGI Powerd By: Arvind

  22. classCalculate{staticint cube(int x) {return x*x*x;   }  publicstaticvoid main(String args[]) {int result=Calculate.cube(5);    System.out.println(result);    }  }classCalculate{staticint cube(int x) {return x*x*x;   }  publicstaticvoid main(String args[]) {int result=Calculate.cube(5);    System.out.println(result);    }  } Powerd By: Arvind

  23. Restrictions for static method There are two main restrictions for static method. • The static method can not use non static data member or call non-static method directly. • this and super cannot be used in static context. Powerd By: Arvind

  24. classA{int a=40; //non static  publicstaticvoid main(String args[]){  System.out.println(a);   }  }classA{int a=40; //non static  publicstaticvoid main(String args[]){  System.out.println(a);   }  } Output: compile time error Powerd By: Arvind

  25. static block • Is used to initialize the static data member. • It is executed before main method at the time of class loading. classA{static{ System.out.println("static block is invoked"); }publicstaticvoid main(String args[]){   System.out.println("Hello main");    }  } Output: static block is invoked hello main Powerd By: Arvind

  26. Can we execute a program without main() method? • Yes, one of the way is static block but in previous version of JDK not in jdk 1.7 . classA{static{    System.out.println("static block is invoked");  System.exit(0);    }  } Powerd By: Arvind