CSE2200: Contemporary Programming Paradigms: OO Paradigm: Inheritance & Polymorphism
CSE2200: Contemporary Programming Paradigms: OO Paradigm: Inheritance & Polymorphism
Max Baird
{max.baird@uog.edu.gy}
University of Guyana
Department of Computer Science
Inheritance
2/24
CSE2200: Contemporary Programming Paradigms Baird, Max
Inheritance Polymorphism Summary
Inheritance
Character
Digit Letter
Vowel Consonant
Inheritance
4/24
CSE2200: Contemporary Programming Paradigms Baird, Max
Inheritance Polymorphism Summary
Inheritance Example
1 public class Animal{
2 public String name; //Instance variable of parent class
3
4 public void move(){ //Instance method of parent class
5 System.out.println("I am moving");
6 }
7 }
1 public class Dog extends Animal{ //Inherits properties from Animal class
2
3 public void show(){ //New method in child class
4 System.out.println("My name is: " + name); // Parent class' member can be accessed
5 }
6 }
In this example, the class Dog extends the Animal class and can
access all of its public and protected members.
5/24
CSE2200: Contemporary Programming Paradigms Baird, Max
Inheritance Polymorphism Summary
Inheritance Example
1 Dog dog = new Dog(); //Create a new object of child class
2
3 dog.name = "Rex"; //Access member of parent class
4 dog.show();
5
6 dog.move(); //Call method of parent class using object of child class
Output:
My name is: Rex
I am moving
Here, dog is an object of type Dog , but name and move are
members of the Animal class. Because the Dog class inherits fields
and methods from the Animal class, we are able to access them.
6/24
CSE2200: Contemporary Programming Paradigms Baird, Max
Inheritance Polymorphism Summary
Inheritance—Important Points
7/24
CSE2200: Contemporary Programming Paradigms Baird, Max
Inheritance Polymorphism Summary
Inheritance—Method Overriding
8/24
CSE2200: Contemporary Programming Paradigms Baird, Max
Inheritance Polymorphism Summary
Output:
My name is: Rex
I am moving with 4 legs
9/24
CSE2200: Contemporary Programming Paradigms Baird, Max
Inheritance Polymorphism Summary
Polymorphism
10/24
CSE2200: Contemporary Programming Paradigms Baird, Max
Inheritance Polymorphism Summary
Polymorphism in reality
11/24
CSE2200: Contemporary Programming Paradigms Baird, Max
Inheritance Polymorphism Summary
Polymorphism
12/24
CSE2200: Contemporary Programming Paradigms Baird, Max
Inheritance Polymorphism Summary
Polymorphism in Java
13/24
CSE2200: Contemporary Programming Paradigms Baird, Max
Inheritance Polymorphism Summary
Compile-time Polymorphism
1 class AverageCalculator{
2 double average(int a, int b){
3 return (a + b) / 2.0;
4 }
5
6 double average(int a, int b, int c){
7 return (a + b + c) / 3.0;
8 }
9 }
Compile-time Polymorphism
Output:
Average 1: 2.5
Average 2: 3.0
15/24
CSE2200: Contemporary Programming Paradigms Baird, Max
Inheritance Polymorphism Summary
Compile-time Polymorphism
1 class Adder{
2 public void add(int a, int b){
3 System.out.println(a + b);
4 }
5
6 public void add(String s1, String s2){
7 System.out.println(s1 + s2);
8 }
9 }
Output:
1 Adder adder = new Adder();
2 adder.add(2, 3); //Outputs "5"
3 adder.add("Hello, ", "world"); //Outputs "Hello, World"
16/24
CSE2200: Contemporary Programming Paradigms Baird, Max
Inheritance Polymorphism Summary
Runtime Polymorphism
1 class X{}
2 class Y extends X{}
3 X x = new Y(); // upcasting
17/24
CSE2200: Contemporary Programming Paradigms Baird, Max
Inheritance Polymorphism Summary
Runtime Polymorphism
Consider the following class hierarchy:
Animal
18/24
CSE2200: Contemporary Programming Paradigms Baird, Max
Inheritance Polymorphism Summary
Runtime Polymorphism
Polymorphism
Polymorphism Example
1 class Animal{
2 void eat(){
3 System.out.println("All animals eat");
4 }
5 }
6
7 class Herbivore extends Animal{
8 void eat(){
9 System.out.println("Herbivores eat plants");
10 }
11 }
12
13 class Carnivore extends Animal{
14 void eat(){
15 System.out.println("Carnivores eat meat");
16 }
17 }
18
19 class Omnivore extends Animal{
20 void eat(){
21 System.out.println("Omnivores eat plants and meat");
22 }
23 }
21/24
CSE2200: Contemporary Programming Paradigms Baird, Max
Inheritance Polymorphism Summary
Polymorphism Example
1 Animal animal1 = new Animal();
2 Animal animal2 = new Carnivore(); //upcasting
3 Animal animal3 = new Herbivore(); //upcasting
4 Animal animal4 = new Omnivore(); //upcasting
5
6 animal1.eat();
7 animal2.eat();
8 animal3.eat();
9 animal4.eat();
Output:
1 All animals eat
2 Carnivores eat meat
3 Herbivores eat plants
4 Omnivores eat plants and meat
Summary
23/24
CSE2200: Contemporary Programming Paradigms Baird, Max
Inheritance Polymorphism Summary
Sources
24/24
CSE2200: Contemporary Programming Paradigms Baird, Max