0% found this document useful (0 votes)
68 views

CSE2200: Contemporary Programming Paradigms: OO Paradigm: Inheritance & Polymorphism

This document discusses inheritance and polymorphism in object-oriented programming. It defines inheritance as code reuse through subclassing, where a subclass inherits variables and methods from its parent class. Polymorphism allows one to use what is inherited to perform different tasks, so the same action can be achieved in many different ways. The document provides examples of inheritance through subclassing an Animal class, and polymorphism through method overriding in a Dog subclass.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
68 views

CSE2200: Contemporary Programming Paradigms: OO Paradigm: Inheritance & Polymorphism

This document discusses inheritance and polymorphism in object-oriented programming. It defines inheritance as code reuse through subclassing, where a subclass inherits variables and methods from its parent class. Polymorphism allows one to use what is inherited to perform different tasks, so the same action can be achieved in many different ways. The document provides examples of inheritance through subclassing an Animal class, and polymorphism through method overriding in a Dog subclass.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 24

Inheritance Polymorphism Summary

CSE2200: Contemporary Programming


Paradigms
OO Paradigm: Inheritance & Polymorphism

Max Baird
{max.baird@uog.edu.gy}

University of Guyana
Department of Computer Science

April, 2022 1/24


CSE2200: Contemporary Programming Paradigms Baird, Max
Inheritance Polymorphism Summary

Inheritance

I Code reuse is supported through inheritance


I In an OO language, classes are part of a class hierarchy
I A class may be subclass of another class which is called the
parent class or superclass
Each subclass can inherit variables and methods from its parent

2/24
CSE2200: Contemporary Programming Paradigms Baird, Max
Inheritance Polymorphism Summary

Inheritance

In OO design, the relationship between a subclass and a superclass


is established if an is-a relationship exists between them. For
example, a vowel is-a kind of letter, so is a consonant. And a letter
is-a kind of character, so is a digit.

Character

Digit Letter

Vowel Consonant

Figure: A Simple Class Hierarchy


3/24
CSE2200: Contemporary Programming Paradigms Baird, Max
Inheritance Polymorphism Summary

Inheritance

I Do not confuse an is-a relationship with a has-a relationship


has-a does not identify a subclass-superclass relationship
has-a identifies that a class is a client of another class
• This is usually called aggregation
I In Java, the extends keyword is used to indicate that a child
class inherits all the properties of the parent class

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

I Only protected and public instance members are inherited


I A parent class of one class may be a child class of another. In
this way a deep class hierarchy can be achieved
I A child class can introduce new instance members not
inherited from the parent class

7/24
CSE2200: Contemporary Programming Paradigms Baird, Max
Inheritance Polymorphism Summary

Inheritance—Method Overriding

I Sometimes it can be necessary to change inherited behaviour


from within the child class
I If the child class declares the same method as the parent
class, it is known as method overiding
I Overriding is used to provide the specific implementation of a
method already provided by the parent class
I Overriding Rules:
Method’s name, parameter list and return type must match
parent class’
There must be an is-a relationship (Inheritance)
I The @Override annotation is used to indicate that a method
in the child class is overriding a method in the parent class

8/24
CSE2200: Contemporary Programming Paradigms Baird, Max
Inheritance Polymorphism Summary

Inheritance—Method Overriding Example


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
7 @Override
8 public void move(){ //Override of parent class' move method
9 System.out.println("I am moving with 4 legs");
10 }
11 }

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 overriden method

Output:
My name is: Rex
I am moving with 4 legs

9/24
CSE2200: Contemporary Programming Paradigms Baird, Max
Inheritance Polymorphism Summary

Polymorphism

I The word “polymorphism” is derived from two Greek words:


“Poly” meaning many and
“Morphs” meaning forms
I Polymorphism therefore means many forms—a significant
feature of the OO Paradigm
I Inheritance lets one class acquire attributes and properties of
another
I Polymorphism allows one to use what is inherited to perform
different tasks
I Thus, the same action can be achieved in many different ways

10/24
CSE2200: Contemporary Programming Paradigms Baird, Max
Inheritance Polymorphism Summary

Polymorphism in reality

I An individual can have multiple simultaneous relationships


with different people:
A woman can be an aunt, mother, sister, grandmother, friend,
etc all at the same time
Similarly, a man can be a father, husband, employee, etc

11/24
CSE2200: Contemporary Programming Paradigms Baird, Max
Inheritance Polymorphism Summary

Polymorphism

I Polymorphism enables us to program in a generic way


As opposed to “programming in the specific”

Polymorphism enables us to write programs that process objects


that share the same superclass (parent class) in a class hierarchy as
if they are all objects of the super class—this can simplify
programming.

12/24
CSE2200: Contemporary Programming Paradigms Baird, Max
Inheritance Polymorphism Summary

Polymorphism in Java

I Can occur in either of the following ways:


Compile-time polymorphism (static polymorphism)
Runtime Polymorphism (dynamic polymorphism)

13/24
CSE2200: Contemporary Programming Paradigms Baird, Max
Inheritance Polymorphism Summary

Compile-time Polymorphism

Method overloading facilitates compile time polymorphism in Java.


Multiple methods with the same name and return type, but with
different parameter lists.

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 }

I Two definitions of the method average


I Which method is called is determined by the parameter list at
compile time
Hence compile time polymorphism
14/24
CSE2200: Contemporary Programming Paradigms Baird, Max
Inheritance Polymorphism Summary

Compile-time Polymorphism

1 AverageCalculator ac = new AverageCalculator();


2 double avg1 = ac.average(2, 3);
3 double avg2 = ac.average(2, 3, 4);
4
5 System.out.println("Average 1: " + avg1);
6 System.out.println("Average 2: " + avg2);

Output:
Average 1: 2.5
Average 2: 3.0

Note that the correct average method is called based on the


number of parameters provided.

15/24
CSE2200: Contemporary Programming Paradigms Baird, Max
Inheritance Polymorphism Summary

Compile-time Polymorphism

It is also possible to overload by having different parameter types.


In the example below, the method add either adds integers or
strings together.

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

With runtime polymorphism, a call to an overriden method is


resolved dynamically at runtime instead of at compile time.
Method overriding is the way runtime polymorphism is achieved.

I Overriding is done via a reference variable of a superclass


I The method to be called is determined by the object this
reference variable is assigned to
I This is called upcasting

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

Fish Frog Bird

I Classes Fish , Frog and Bird all extend Animal

Imagine the Animal class contains a method move which


maintains an animal’s current location as a pair of x-y coordinates
and each subclass implements the method move .

18/24
CSE2200: Contemporary Programming Paradigms Baird, Max
Inheritance Polymorphism Summary

Runtime Polymorphism

Let’s assume there is an array of Animal objects and their


movements are simulated by sending the message move —once per
second—to each object in the array.

I Each type of animal may respond to the message in a unique


way:
Fish might swim 3 feet
Frog might jump 5 feet
Bird might fly 10 feet
I The same message ( move ) is sent to each object generically
I But each object knows how to modify its x-y coordinates
appropriately for its specific movement
19/24
CSE2200: Contemporary Programming Paradigms Baird, Max
Inheritance Polymorphism Summary

Polymorphism

Relying on each object to know how to “do the right thing” in


response to the same method call is the key concept of
polymorphism. The same message (in this case move ) sent to a
variety of objects has “many forms” of results—hence the term
polymorphism.

I Polymorphism allows one to design and implement systems


that are easily extensible
New classes can be added with little to no modification of the
existing program
• As long as they are part of the inheritance hierarchy the
program processes generically
I For example: The Animal class may be extended by a Turtle
class which moves by one inch
20/24
CSE2200: Contemporary Programming Paradigms Baird, Max
Inheritance Polymorphism Summary

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

In this example, all subclasses extend and override their superclass’


eat method. The eat method is called via the parent class
reference. However, since the parent class refers to the child class
object (e.g line 2), and the child class overrides the parent class eat
method, the child class method is invoked at runtime.
22/24
CSE2200: Contemporary Programming Paradigms Baird, Max
Inheritance Polymorphism Summary

Summary

I Inheritance supports code reuse


This allows the creation of a class hierarchy
Child classes can override and customize inherited methods
I Polymorphism allows one to program in a generic way
Objects sharing the same parent class can be treated as
objects of that class
I Polymorphism in Java can be done at:
Compile-time and/or
Runtime

23/24
CSE2200: Contemporary Programming Paradigms Baird, Max
Inheritance Polymorphism Summary

Sources

I Programming Languages, Principles and Paradigms Second


Edition. Chapter 13
I Java, how to program Seventh Edition. Chapter 10

24/24
CSE2200: Contemporary Programming Paradigms Baird, Max

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy