290 likes | 395 Views
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.
E N D
Advanced Programming in Java SadeghAliakbary Sharif University of Technology Fall 2010
Agenda • Initialization • Final methods and classes • Abstract method and classes Sharif University of Technology
Review of Polymorphism Sharif University of Technology
Review of Polymorphism Sharif University of Technology
Hint on Array Initialization Sharif University of Technology
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
Static Initialization • Static variables are initiated per class • When class is loaded • Variable declaration initialization • Static block Sharif University of Technology
Order of initialization • Once per class • Static variable declaration • Static block • Once per object • variable declaration • Initialization block • Constrictor Sharif University of Technology
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
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
Final Variables Sharif University of Technology
Final Classes • You can not inherit from final classes • No class can extend final classes Sharif University of Technology
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
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
Final Review (3) Sharif University of Technology
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
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
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
Animal Example Sharif University of Technology
Animal Example (2) Sharif University of Technology
Quiz! Sharif University of Technology
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