polymorphism completed
This commit is contained in:
parent
0d55cf1273
commit
a0bdd99b3f
@ -17,7 +17,7 @@ Lessons:
|
||||
- [encapsulation](./lessons/Encapsulation.md)
|
||||
- [inheritance](./lessons/Inheritance.md)
|
||||
- [properties](./lessons/Properties.md) -
|
||||
- [polymorphism](./lessons/Polymorphism.md) -
|
||||
- [polymorphism](./lessons/Polymorphism.md)
|
||||
|
||||
Work in progress:
|
||||
|
||||
|
5
java/Polymorphism.md
Normal file
5
java/Polymorphism.md
Normal file
@ -0,0 +1,5 @@
|
||||
# Polymorphism
|
||||
|
||||
Relevant files
|
||||
|
||||
- [Program.java](./inheritance/Program.java)
|
@ -1,9 +1,14 @@
|
||||
public class Program {
|
||||
public static void main(String[] args) {
|
||||
Dog d = new Dog("buster");
|
||||
Cat c = new Cat("mittens");
|
||||
// because cat and dog are subclasses of animal, they can be stored in an array of animals
|
||||
Animal[] animals = {
|
||||
new Dog("buster"),
|
||||
new Cat("mittens")
|
||||
};
|
||||
|
||||
d.make_sound();
|
||||
c.make_sound();
|
||||
for (Animal a : animals) {
|
||||
// when calling the make_sound function, the implementation of the actual class gets called (in this case cat or dog)
|
||||
a.make_sound();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -2,7 +2,6 @@
|
||||
|
||||
Relevant files:
|
||||
|
||||
- [Program.java](./Program.java)
|
||||
- [Animal.java](./Animal.java)
|
||||
- [Cat.java](./Cat.java)
|
||||
- [Dog.java](./Dog.java)
|
||||
|
@ -0,0 +1,19 @@
|
||||
# Polymorphism
|
||||
|
||||
Polymorphism is the ability to treat subclasses like their base classes, for
|
||||
example passing cat, which extends animal to a function that expects an animal
|
||||
as parameter. or storing objects of different types that all extend a common
|
||||
class in an array of that common type.
|
||||
|
||||
You lose acess to the properties of the subclass, but you can still access the
|
||||
base class like usual. When the subclass overrides behaviour of the base class,
|
||||
the overridden functionality in the subclass is still called.
|
||||
|
||||
You can regain access to properties of the subclass with casting, but you have
|
||||
to make sure that the object is actually an instance of that class or you might
|
||||
run into runtime errors.
|
||||
|
||||
- [Java](../java/Polymorphism.md)
|
||||
- [Rust](../rust/Inheritance.md)
|
||||
- [C++](../cpp/Polymorphism.md)
|
||||
- [JavaScript (using TypeScript)](../typescript/Polymorphism.md)
|
5
typescript/Polymorphism.md
Normal file
5
typescript/Polymorphism.md
Normal file
@ -0,0 +1,5 @@
|
||||
# Polymorphism
|
||||
|
||||
Relevant files
|
||||
|
||||
- [index.ts](./inheritance/index.ts)
|
@ -2,7 +2,6 @@
|
||||
|
||||
Relevant files:
|
||||
|
||||
- [index.ts](./index.ts)
|
||||
- [animal.ts](./animal.ts)
|
||||
- [cat.ts](./cat.ts)
|
||||
- [dog.ts](./dog.ts)
|
||||
|
@ -1,8 +1,14 @@
|
||||
import { Dog } from "./dog";
|
||||
import { Cat } from "./cat";
|
||||
import { Animal } from "./animal";
|
||||
|
||||
const d = new Dog('buster');
|
||||
const c = new Cat('mittens');
|
||||
// because cat and dog are subclasses of animal, they can be stored in an array of animals
|
||||
const animals: Animal[] = [
|
||||
new Dog('buster'),
|
||||
new Cat('mittens')
|
||||
]
|
||||
|
||||
d.make_sound();
|
||||
c.make_sound();
|
||||
for (const a of animals) {
|
||||
// when calling the make_sound function, the implementation of the actual class gets called (in this case cat or dog)
|
||||
a.make_sound();
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user