---
# System prepended metadata

title: Computer Programming II Pre-Lab 4
tags: [Computer Engineering, IUG, Computer Programming II, Pre-Lab]

---

---
title: Computer Programming II Pre-Lab 4
---
<h1 style='border: none'><center>Computer Programming II Pre-Lab 4</center></h1>
<h2 style='border: none'><center>More about Polymorphism</center></h2>
<h5><center>The Islamic University of Gaza<br>Engineering Faculty<br>Department of Computer Engineering</center></h5>
<h6>Authors: Usama R. Al Zayan<span style="float:right">2023/03/02</span></h6>
<h6>Parts of this Lab were adapted from work done by Mohammed Nafiz ALMadhoun and  Mohammed AlBanna</h6>
---

## Polymorphism

Polymorphism means "many forms", and it occurs when we have many classes that 
are related to each other by inheritance.

Polymorphism is one of the OOPs feature that allows us to perform a single action in 
different ways.




## Exercise
Have a look at the following classes and guess what is the result of running the following programs.

```java=
class Shape {
    public Shape() {
    }

    public String print() {
        return "General";
    }
}

class Shape2 {
    public Shape2() {
    }
}

class Circle extends Shape {
    public Circle() {
    }

    @Override
    public String print() {
        return "Circle";
    }
}

class Rectangle extends Shape {
    public Rectangle() {
    }

    @Override
    public String print() {
        return "Rectangle";
    }
}
```

### What is the output of the following code:

#### Ex: 01

```java=
public class Main {
    public static void main(String[] args) {
        Shape shape = new Shape();
        Circle cir = new Circle();
        Rectangle rect = new Rectangle();

        System.out.println(shape.print());     // General
        System.out.println(cir.print());       // Circle
        System.out.println(rect.print());      // Rectangle

    }
}
```
#### Ex: 02
```java=
public class Main {
    public static void main(String[] args) {
        Shape shape_cir = new Circle();
        Shape shape_rect = new Rectangle();

        System.out.println(shape_cir.print());  // Circle
        System.out.println(shape_rect.print()); // Rectangle
    }
}
```

#### Ex: 03

```java=
public class Main {
    public static void main(String[] args) {
        Object shape_cir = new Circle();
        Object shape_rect = new Rectangle();

        System.out.println(((Circle) shape_cir).print());     // Circle
        System.out.println(((Rectangle) shape_rect).print()); // Rectangle
    }
}
```

#### Ex: 04

```java=
public class Main {
    public static void main(String[] args) {
        Shape shape = new Shape();
        Circle cir = new Circle();
        Rectangle rect = new Rectangle();

        System.out.println(show(shape)); // General
        System.out.println(show(cir));   // Circle
        System.out.println(show(rect));  // Rectangle
    }

    public static String show(Shape shape) {
        return shape.print();
    }
}
```

#### Ex: 05

```java=
public class Main {
    public static void main(String[] args) {
        Shape2 shape2 = new Shape2();

        System.out.println(show(shape2)); // No value
    }

    public static String show(Object obj) {
        if (obj instanceof Shape) {
            return ((Shape) obj).print();
        }
        return "No value";
    }
}
```

###### tags: `Computer Programming II` `Pre-Lab` `IUG` `Computer Engineering`

<center>End Of Pre-Lab 4</center>