1 / 28

Advanced Programming in Java

Advanced Programming in Java. Sadegh Aliakbary Sharif University of Technology Fall 2010. Agenda. Initialization Final methods and classes Abstract method and classes. Review of Polymorphism. Review of Polymorphism. Hint on Array Initialization. Initialization.

elda
Télécharger la présentation

Advanced Programming in 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. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Advanced Programming in Java SadeghAliakbary Sharif University of Technology Fall 2010

  2. Agenda • Initialization • Final methods and classes • Abstract method and classes Sharif University of Technology

  3. Review of Polymorphism Sharif University of Technology

  4. Sharif University of Technology

  5. Review of Polymorphism Sharif University of Technology

  6. Hint on Array Initialization Sharif University of Technology

  7. Initialization • There are different ways for initialization of variables • Variable declaration initialization • Constructor • Initialization block • Guarantees that certain operations occur regardless of which explicit constructor is called Sharif University of Technology

  8. Sharif University of Technology

  9. Static Initialization • Static variables are initiated per class • When class is loaded • Variable declaration initialization • Static block Sharif University of Technology

  10. Order of initialization • Once per class • Static variable declaration • Static block • Once per object • variable declaration • Initialization block • Constrictor Sharif University of Technology

  11. Final Methods • You can not override final methods • Static method binding for final methods • Private methods are implicitly final • Static methods are implicitly final Sharif University of Technology

  12. Final Variables • You can define variables as final • The value of final variable will remain constant • You can not change the value of final variables Sharif University of Technology

  13. Final Variables Sharif University of Technology

  14. Final Classes • You can not inherit from final classes • No class can extend final classes Sharif University of Technology

  15. Final Review • Final data • Const • Local variables • Method parameters • Member variables • Primitives  constant values • Objects  constant references • A compile-time constant that won’t ever change • A value initialized at run time that you don’t want changed Sharif University of Technology

  16. Final Review (2) • Final Methods • No override • Final Class • No sub-class • final keyword on data • Different from final classes & methods Sharif University of Technology

  17. Final Review (3) Sharif University of Technology

  18. Finalism and Performance • Final methods can be invoked inline • Compiler can bind final methods statically • Static binding • It is now discouraged to use final to try to help the optimizer • Especially with Java 6 • Don’t worry about performance • Java optimizer Sharif University of Technology

  19. Abstract Methods • Remember Shape classes • getArea() • draw() • Remember Animals • Talk() • getName() : not abstract • We can implement these methods by simple dummy operations • Better way : abstract methods Sharif University of Technology

  20. Abstract Methods (2) • abstract method : no implementation • A class containing abstract methods: an abstract class • You can not instantiate abstract classes • If a sub-class do not implement the abstract method • It will be abstract too Sharif University of Technology

  21. Animal Example Sharif University of Technology

  22. Animal Example (2) Sharif University of Technology

  23. Quiz! Sharif University of Technology

  24. Sharif University of Technology

  25. Sharif University of Technology

  26. Sharif University of Technology

  27. Result: • Where am I? • Animal(String name) • I am a cat • Cat(String name) • Where am I? • Animal(String name) • I am a dog • Dog(String name) • Maloos: Meowww! • Fido: Hop! Hop! Sharif University of Technology

  28. Sharif University of Technology

More Related