Advanced Programming in Java Inheritance 2 Mehdi Einali
- Slides: 37
Advanced Programming in Java Inheritance 2 Mehdi Einali 1
Abstract behavior 2
Abstract Behaviors Animal -name: String -age: int #talk(): String #swim(): void #get. Age(): int Dog +talk(): String Cat +talk(): String 3 Fish +swim(): void
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! Cat: Mewww! 4
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 5
Remember Shape Classes 6
7
8
9
Shapes Example Shape is an abstract class Some methods are undefined in Shape Some methods should be defined in subclasses get. Area() get. Perimeter() These methods are abstract methods Remember abstract behaviors 10
Abstract Methods Shape classes get. Area() draw() Animals Talk() get. Name() is not abstract! How do you implement an abstract method? We can implement these methods by simple dummy operations Better way : abstract methods 11
12
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 13
14
More about inheritance 15
Sample 1 16
Sample 2 17
Sample 3 18
Sample 4 19
Sample 5 20
Sample 6 21
Sample 7 22
Sample 8 23
Sample 9 24
Sample 10 25
Sample 11 26
Sample 12 27
Final 28
Final Methods You can not override final methods final keyword Static method binding for final methods Private methods are implicitly final Static methods are statically bound Invoked reference is not important No polymorphism for static variables 29
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 local variable property static variable 30
Final variables 31
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 32
Final Classes You can not inherit from final classes No class can extend final classes 33
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 34
Review of final Keyword (2) Final Methods No override Final Class No sub-class final keyword on data Different from final classes & methods 35
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 36
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 37
- Advanced programming in java
- Java inheritance
- Advantages and disadvantages of inheritance in java
- Bank account java program using inheritance
- Different types of inheritance
- Contoh program inheritance java
- Java inheritance lab exercise
- Java definition
- Ang namumuno sa bansang iran upang makamit ang kalayaan
- Mehdi nt
- Mehdi sebbane
- Mehdi bouguerra
- Base deficit formula
- Mehdi bouguerra
- Sodium correction formula
- Mehdi salek md
- Dr. mehdi pain management
- Mehdi
- Mehdi khalighi
- Mehdi hamadani
- Mehdi namazi
- Mehdi kargar
- Mehdi salek md
- Advanced dynamic programming
- Advanced internet programming
- Design specification of an assembler
- Imperative statement in system programming
- Advanced data structures in java
- Object oriented programming exercises java
- Perbedaan linear programming dan integer programming
- Greedy programming vs dynamic programming
- System programming vs application programming
- Integer programming vs linear programming
- Perbedaan linear programming dan integer programming
- Application
- Java parallel programming
- Problem solving
- Event-driven programming in java