inheritance 2 mehdi einali advanced programming in java 1
TRANSCRIPT
1
Inheritance 2
Mehdi Einali
Advanced Programming in Java
2
Abstract behavior
3
Abstract BehaviorsAnimal
-name:String-age:int
#talk():String#swim():void#getAge():int
Dog
+talk():String
Cat
+talk():String
Fish
+swim():void
4
Abstract BehaviorsDoes any animal swim?
No. So “to swim” is not a behavior of animals.
Any animal has a voice“to talk” is a behavior of animalsBut what is the voice of an animal?How does an animal “talk”?
It depends to the specific type of animalDog: Hop! Hop!Cat: Mewww!
5
Abstract Behaviors (2)“talk” is an abstract behavior of Animal
All animals can “talk”But we can not specify how an animal talksIt depends to the specific class of animal
“talk” is a concrete behavior of DogHop! Hop!
“swim” is not a behavior of AnimalAll animals can not swim“swim” is a concrete behavior of Fish
6
Remember Shape Classes
7
8
9
10
Shapes ExampleShape is an abstract classSome methods are undefined in ShapeSome methods should be defined in sub-classes
getArea()getPerimeter()
These methods are abstract methodsRemember abstract behaviors
11
Abstract MethodsShape classes
getArea()draw()
Animals Talk()getName()
is not abstract!
How do you implement an abstract method?We can implement these methods by simple dummy operationsBetter way : abstract methods
12
13
Abstract Methods (2)abstract method : no implementationA class containing abstract methods: an abstract classYou can not instantiate abstract classes
Why?
If a sub-class do not implement the abstract method
It will be abstract too
14
15
More about inheritance
16
Sample 1
17
Sample 2
18
Sample 3
19
Sample 4
20
Sample 5
21
Sample 6
22
Sample 7
23
Sample 8
24
Sample 9
25
Sample 10
26
Sample 11
27
Sample 12
28
Final
29
Final MethodsYou can not override final methods final keywordStatic method binding for final methodsPrivate methods are implicitly finalStatic methods are implicitly final
Static methods are statically boundInvoked reference is not importantNo polymorphism for static variables
30
Final VariablesYou can define variables as final
The value of final variable will remain constantYou can not change the value of final variablesYou should immediately assign a value to final variables
Final parameterFinal local variableFinal propertyFinal static variable
31
Final variables
32
Key notes about Final variable
A not initialized final field of a class must be definitely assigned in every constructor of the class A not initialized final static variable must be definitely assigned in a static initializer of the class in which it is declaredIf the variable is a reference, this means that the variable cannot be re-bound to reference another object. But the object that it references is still mutable, if it was originally mutable
33
Final ClassesYou can not inherit from final classesNo class can extend final classes
34
Review of final KeywordFinal data
ConstLocal variablesMethod parametersMember variablesPrimitives constant valuesObjects constant references
A compile-time constant that won’t ever changeA value initialized at run time that you don’t want changed
35
Review of final Keyword (2)
Final MethodsNo override
Final ClassNo sub-class
final keyword on data Different from final classes & methods
36
Finalism and performanceFinal methods can be invoked inline Compiler can bind final methods statically
Static binding
So it may bring a better performance…It is now discouraged to use final to try to help the optimizer
Especially with Java 6+
Don’t worry about performanceJava optimizer
37
Robert Cecil Martin colloquially known as Uncle Bob
Famous quots
It is not enough for code to work.
Say what you mean. Mean what you say
The problem isn’t the simplicity of the code but the implicity of the code (to coin a phrase): the degree to which the context is not explicit in the code itself.
Is not the language that makes programs appear simple. It is the programmer that make the language appear simple