Professional Documents
Culture Documents
CSEN401 - Lecture 3
CSEN401 - Lecture 3
Lecture 3 - Abstraction+Polymorphism
slim.abdennadher@guc.edu.eg
nourhan.ehab@guc.edu.eg
ahmed.abdelfattah@guc.edu.eg
18.2.2024 − 22.2.2024
© Slim Abdennadher L3- Abstraction+Polymorphism 1/36
Abstraction Interfaces Polymorphism Recap
Outline
1 Abstraction
2 Interfaces
3 Polymorphism
4 Recap
Abstraction
Abstract Classes
• Abstract classes are classes that represent an Abstract
Concept.
• Creating an Object from the class makes no sense.
Example: Greeting cards
Outline
1 Abstraction
2 Interfaces
3 Polymorphism
4 Recap
Interfaces
}
© Slim Abdennadher L3- Abstraction+Polymorphism 15/36
Abstraction Interfaces Polymorphism Recap
Outline
1 Abstraction
2 Interfaces
3 Polymorphism
4 Recap
Polymorphism
Polymporphism
Static Polymorphism
Method Signature
Example: Overloading
1 static void f() {/* ... */}
This version has no parameters, so its signature differs from
all the others which each have at least one parameter.
2 static void f(int x) {/* ... */}
This version differs from version 3, since its single parameter
is an int, not a double.
3 static void f(double x) {/* ... */}
This version differs from version 2, since its single parameter
is a double, not an int.
4 static void f(int x, double y) {/* ... */}
This version differs form version 5 because, even though
versions 4 and 5 have the same number of parameters with
the same types, the order of the types is different.
5 static void f(double x, int y) {/* ... */}
© Slim Abdennadher L3- Abstraction+Polymorphism 22/36
Abstraction Interfaces Polymorphism Recap
class Human {
public String getGender() {return "Neutral";}
}
class Man extends Human {
public String getGender() {return "Man";}
}
class Woman extends Human {
public String getGender() {return "Woman";}
}
Overriding
Dynamic Binding
The OO Way
The OO Way
//...
}
class Hippopotamus extends Animal {
public void talk() {
System.out.println("Roar!");
}
//...
}
class Example2 {
public static void main(String[] args) {
makeItTalk(new Cat());
makeItTalk(new Dog());
makeItTalk(new Hippopotamus());
The OO Way
}
public static void makeItTalk(Animal animal) {
animal.talk();
}
}
Outline
1 Abstraction
2 Interfaces
3 Polymorphism
4 Recap
• Abstraction.
• Interfaces.
• Polymorphism: Static vs Dynamic.