Top Banner
Inheritance 2 Mehdi Einali Advanced Programming in Java 1
37

Inheritance 2 Mehdi Einali Advanced Programming in Java 1.

Jan 19, 2016

Download

Documents

Leonard Randall
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Inheritance 2 Mehdi Einali Advanced Programming in Java 1.

1

Inheritance 2

Mehdi Einali

Advanced Programming in Java

Page 2: Inheritance 2 Mehdi Einali Advanced Programming in Java 1.

2

Abstract behavior

Page 3: Inheritance 2 Mehdi Einali Advanced Programming in Java 1.

3

Abstract BehaviorsAnimal

-name:String-age:int

#talk():String#swim():void#getAge():int

Dog

+talk():String

Cat

+talk():String

Fish

+swim():void

Page 4: Inheritance 2 Mehdi Einali Advanced Programming in Java 1.

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!

Page 5: Inheritance 2 Mehdi Einali Advanced Programming in Java 1.

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

Page 6: Inheritance 2 Mehdi Einali Advanced Programming in Java 1.

6

Remember Shape Classes

Page 7: Inheritance 2 Mehdi Einali Advanced Programming in Java 1.

7

Page 8: Inheritance 2 Mehdi Einali Advanced Programming in Java 1.

8

Page 9: Inheritance 2 Mehdi Einali Advanced Programming in Java 1.

9

Page 10: Inheritance 2 Mehdi Einali Advanced Programming in Java 1.

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

Page 11: Inheritance 2 Mehdi Einali Advanced Programming in Java 1.

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

Page 12: Inheritance 2 Mehdi Einali Advanced Programming in Java 1.

12

Page 13: Inheritance 2 Mehdi Einali Advanced Programming in Java 1.

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

Page 14: Inheritance 2 Mehdi Einali Advanced Programming in Java 1.

14

Page 15: Inheritance 2 Mehdi Einali Advanced Programming in Java 1.

15

More about inheritance

Page 16: Inheritance 2 Mehdi Einali Advanced Programming in Java 1.

16

Sample 1

Page 17: Inheritance 2 Mehdi Einali Advanced Programming in Java 1.

17

Sample 2

Page 18: Inheritance 2 Mehdi Einali Advanced Programming in Java 1.

18

Sample 3

Page 19: Inheritance 2 Mehdi Einali Advanced Programming in Java 1.

19

Sample 4

Page 20: Inheritance 2 Mehdi Einali Advanced Programming in Java 1.

20

Sample 5

Page 21: Inheritance 2 Mehdi Einali Advanced Programming in Java 1.

21

Sample 6

Page 22: Inheritance 2 Mehdi Einali Advanced Programming in Java 1.

22

Sample 7

Page 23: Inheritance 2 Mehdi Einali Advanced Programming in Java 1.

23

Sample 8

Page 24: Inheritance 2 Mehdi Einali Advanced Programming in Java 1.

24

Sample 9

Page 25: Inheritance 2 Mehdi Einali Advanced Programming in Java 1.

25

Sample 10

Page 26: Inheritance 2 Mehdi Einali Advanced Programming in Java 1.

26

Sample 11

Page 27: Inheritance 2 Mehdi Einali Advanced Programming in Java 1.

27

Sample 12

Page 28: Inheritance 2 Mehdi Einali Advanced Programming in Java 1.

28

Final

Page 29: Inheritance 2 Mehdi Einali Advanced Programming in Java 1.

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

Page 30: Inheritance 2 Mehdi Einali Advanced Programming in Java 1.

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

Page 31: Inheritance 2 Mehdi Einali Advanced Programming in Java 1.

31

Final variables

Page 32: Inheritance 2 Mehdi Einali Advanced Programming in Java 1.

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

Page 33: Inheritance 2 Mehdi Einali Advanced Programming in Java 1.

33

Final ClassesYou can not inherit from final classesNo class can extend final classes

Page 34: Inheritance 2 Mehdi Einali Advanced Programming in Java 1.

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

Page 35: Inheritance 2 Mehdi Einali Advanced Programming in Java 1.

35

Review of final Keyword (2)

Final MethodsNo override

Final ClassNo sub-class

final keyword on data Different from final classes & methods

Page 36: Inheritance 2 Mehdi Einali Advanced Programming in Java 1.

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

Page 37: Inheritance 2 Mehdi Einali Advanced Programming in Java 1.

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