Download presentation
Presentation is loading. Please wait.
Published byLeonard Randall Modified over 8 years ago
1
Inheritance 2 Mehdi Einali Advanced Programming in Java 1
2
2 Abstract behavior
3
3 Abstract Behaviors Animal -name:String -age:int #talk():String #swim():void #getAge():int Dog +talk():String Cat +talk():String Fish +swim():void
4
4 Abstract Behaviors Does any animal swim? No. So “to swim” is not a behavior of animals. Any animal has a voice “to talk” is a behavior of animals But what is the voice of an animal? How does an animal “talk”? It depends to the specific type of animal Dog: Hop! Hop! Cat: Mewww!
5
5 Abstract Behaviors (2) “talk” is an abstract behavior of Animal All animals can “talk” But we can not specify how an animal talks It depends to the specific class of animal “talk” is a concrete behavior of Dog Hop! “swim” is not a behavior of Animal All animals can not swim “swim” is a concrete behavior of Fish
6
6 Remember Shape Classes
7
7
8
8
9
9
10
10 Shapes Example Shape is an abstract class Some methods are undefined in Shape Some methods should be defined in sub- classes getArea() getPerimeter() These methods are abstract methods Remember abstract behaviors
11
11 Abstract Methods Shape classes getArea() draw() Animals Talk() getName() is not abstract! How do you implement an abstract method? We can implement these methods by simple dummy operations Better way : abstract methods
12
12
13
13 Abstract Methods (2) abstract method : no implementation A class containing abstract methods: an abstract class You can not instantiate abstract classes Why? If a sub-class do not implement the abstract method It will be abstract too
14
14
15
15 More about inheritance
16
16 Sample 1
17
17 Sample 2
18
18 Sample 3
19
19 Sample 4
20
20 Sample 5
21
21 Sample 6
22
22 Sample 7
23
23 Sample 8
24
24 Sample 9
25
25 Sample 10
26
26 Sample 11
27
27 Sample 12
28
28 Final
29
29 Final Methods You can not override final methods final keyword Static method binding for final methods Private methods are implicitly final Static methods are implicitly final Static methods are statically bound Invoked reference is not important No polymorphism for static variables
30
30 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 You should immediately assign a value to final variables Final parameter Final local variable Final property Final static variable
31
31 Final variables
32
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 declared If 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
33 Final Classes You can not inherit from final classes No class can extend final classes
34
34 Review of final Keyword 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
35
35 Review of final Keyword (2) Final Methods No override Final Class No sub-class final keyword on data Different from final classes & methods
36
36 Finalism and performance Final 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 performance Java optimizer
37
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
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.