OOP Lab 8

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 13

Faculty of Computing and Information Technology (FCIT)

Department of Computing Indus University, Karachi

NAME OF STUDENT: _________________ID No: ______________

Lab No. 8: Polymorphism

Objective:
In these labs we will discuss and implement method overriding and polymorphism, which are one of the
key features of Object oriented programming (OOPs). Moreover the importance of these concepts would
be seen in problem solving in Java.

Scope:
In inheritance we have seen the concept of super classes and sub classes. If a class inherits a method from
its super class, then there is a chance to override the method provided that it is not marked final. The ben-
efit of overriding is: ability to define a behavior that's specific to the sub class type. Which means a sub-
class can implement a parent class method based on its requirement. In object oriented terms, overriding
means to override the functionality of any existing method.

Example 1: Let us look at an example.

Object Oriented Programming


Faculty of Computing and Information Technology (FCIT)
Department of Computing Indus University, Karachi

NAME OF STUDENT: _________________ID No: ______________

Example 2: Consider the following example

Object Oriented Programming


Faculty of Computing and Information Technology (FCIT)
Department of Computing Indus University, Karachi

NAME OF STUDENT: _________________ID No: ______________

Object Oriented Programming


Faculty of Computing and Information Technology (FCIT)
Department of Computing Indus University, Karachi

NAME OF STUDENT: _________________ID No: ______________

This program will throw a compile time error since b's reference type Animal doesn't have a method by
the name of bark.

Using the super keyword:

When invoking a superclass version of an overridden method the super keyword is used. Con-
sider the following example:

Object Oriented Programming


Faculty of Computing and Information Technology (FCIT)
Department of Computing Indus University, Karachi

NAME OF STUDENT: _________________ID No: ______________

Example 3: consider the following classes:

Object Oriented Programming


Faculty of Computing and Information Technology (FCIT)
Department of Computing Indus University, Karachi

NAME OF STUDENT: _________________ID No: ______________

Now because of inheritance, it is possible to write a single method that can accept an object of any of the
above classes as parameter and call the print method polymorphically like:

Object Oriented Programming


Faculty of Computing and Information Technology (FCIT)
Department of Computing Indus University, Karachi

NAME OF STUDENT: _________________ID No: ______________

Example 4: Consider the following inheritance diagram:

Object Oriented Programming


Faculty of Computing and Information Technology (FCIT)
Department of Computing Indus University, Karachi

NAME OF STUDENT: _________________ID No: ______________

 Suppose we have the following:

Shape s = new Circle();


s.draw();

 The question is which draw() method will be called?

 Because of polymorphism, the draw() of the actual object (not necessarily that of the reference
type) will be called. Thus, it is the draw() method of the Circle object that is called.

Here is the demonstration of this idea

Object Oriented Programming


Faculty of Computing and Information Technology (FCIT)
Department of Computing Indus University, Karachi

NAME OF STUDENT: _________________ID No: ______________

Object Oriented Programming using Lab Manual – Page 89|193


JAVA
Faculty of Computing and Information Technology (FCIT)
Department of Computing Indus University, Karachi

NAME OF STUDENT: _________________ID No: ______________

Example 5: Suppose we define a subclass, say ResearchAssistant for the class Student in Example 1 as
follows:

It is now possible to call the describe() with an object of type ResearchAssistant without any change to
the method as shown below. The describe() method is said to be extensible – meaning the functionality
can be added to a code simply by inheriting a new data type from the base class.

Object Oriented Programming using Lab Manual


JAVA
Faculty of Computing and Information Technology (FCIT)
Department of Computing Indus University, Karachi

NAME OF STUDENT: _________________ID No: ______________

Whereas the addition of ResearchAssistant class and its calling is shown below

Object Oriented Programming using Lab Manual


JAVA
Faculty of Computing and Information Technology (FCIT)
Department of Computing Indus University, Karachi

NAME OF STUDENT: _________________ID No: ______________

Exercises:

Exercise 1:
Consider the following class
Public class father
{
int age;
father(int x)
{
age=x;
}
public void Iam()
{
System.out.println(“I AM FATHER and My AGE = %d”, age);
}
}

Object Oriented Programming using Lab Manual


JAVA
Faculty of Computing and Information Technology (FCIT)
Department of Computing Indus University, Karachi

NAME OF STUDENT: _________________ID No: ______________

Derive two sub classes son and daughter from the above class and override the method Iam() in each of
the subclass and prints an appropriate message in each overridden method. You should also define a suit-
able constructor for each of these sub classes.
Now write a test class to check the functionality of above example. In main() method create three objects
one for each father, son and daughter respectively and call their Iam() method non polymorphically.
Then create an array of father references of size ten and initialize this array randomly with the objects of
daughter and son. Then traverse this array and call the Iam() method polymorphically and display the re-
sults.

Exercise 2:

ShapeHierarchy: Implement the Shape hierarchy shown in Fig. 1 givenbelow. Each TwoDimensional-
Shape should contain method getArea to calculate the area of the two-dimensional shape. Each ThreeDi-
mensionalShape should have methods getArea and getVolume to calculate the surface area and volume,
respectively, of the three-dimensional shape. Create a program that uses an array of Shape references to
objects of each concrete class in the hierarchy. The program should print a text description of the object to
which each array element refers. Also, in the loop that processes all the shapes in the array, determine
whether each shape is a TwoDimensionalShape or a ThreeDimensionalShape. If it’s a TwoDimensional-
Shape, display its area. If it’s a ThreeDimensionalShape, display its area and volume.

Figure 1: Shape hierarchy

Object Oriented Programming using Lab Manual


JAVA

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