Download - Advanced Programming in Java
Advanced Programming in Java
Sadegh AliakbarySharif University of Technology
Fall 2010
Sharif University of Technology 2
AgendaInitializationFinal methods and classesAbstract method and classes
Fall 2010
Sharif University of Technology 3
Review of Polymorphism
Fall 2010
Sharif University of Technology 4Fall 2010
Sharif University of Technology 5
Review of Polymorphism
Fall 2010
Sharif University of Technology 6
Hint on Array Initialization
Fall 2010
Sharif University of Technology 7
InitializationThere are different ways for initialization of
variablesVariable declaration initializationConstructorInitialization block
Guarantees that certain operations occur regardless of which explicit constructor is called
Fall 2010
Sharif University of Technology 8Fall 2010
Sharif University of Technology 9
Static InitializationStatic variables are initiated per class
When class is loadedVariable declaration initializationStatic block
Fall 2010
Sharif University of Technology 10
Order of initializationOnce per class
Static variable declarationStatic block
Once per objectvariable declarationInitialization blockConstrictor
Fall 2010
Sharif University of Technology 11
Final MethodsYou can not override final methods Static method binding for final methodsPrivate methods are implicitly finalStatic methods are implicitly final
Fall 2010
Sharif University of Technology 12
Final VariablesYou can define variables as final
The value of final variable will remain constant
You can not change the value of final variables
Fall 2010
Sharif University of Technology 13
Final Variables
Fall 2010
Sharif University of Technology 14
Final ClassesYou can not inherit from final classesNo class can extend final classes
Fall 2010
Sharif University of Technology 15
Final ReviewFinal data
ConstLocal variablesMethod parametersMember variablesPrimitives constant valuesObjects constant references
A compile-time constant that won’t ever change
A value initialized at run time that you don’t want changed
Fall 2010
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
Fall 2010
Sharif University of Technology 17
Final Review (3)
Fall 2010
Sharif University of Technology 18
Finalism and PerformanceFinal methods can be invoked inline Compiler can bind final methods statically
Static bindingIt is now discouraged to use final to try
to help the optimizerEspecially with Java 6
Don’t worry about performanceJava optimizer
Fall 2010
Sharif University of Technology 19
Abstract MethodsRemember Shape classes
getArea()draw()
Remember Animals Talk()getName() : not abstract
We can implement these methods by simple dummy operations
Better way : abstract methods
Fall 2010
Sharif University of Technology 20
Abstract Methods (2)abstract method : no implementationA class containing abstract methods: an
abstract classYou can not instantiate abstract classesIf a sub-class do not implement the abstract
methodIt will be abstract too
Fall 2010
Sharif University of Technology 21
Animal Example
Fall 2010
Sharif University of Technology 22
Animal Example (2)
Fall 2010
Sharif University of Technology 23
Quiz!
Fall 2010
Sharif University of Technology 24Fall 2010
Sharif University of Technology 25Fall 2010
Sharif University of Technology 26Fall 2010
Sharif University of Technology 27
Result:Where am I?Animal(String name)I am a catCat(String name)Where am I?Animal(String name)I am a dogDog(String name)Maloos: Meowww!Fido: Hop! Hop!
Fall 2010
Sharif University of Technology 28Fall 2010