OOP Lab 8
OOP Lab 8
OOP Lab 8
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.
This program will throw a compile time error since b's reference type Animal doesn't have a method by
the name of bark.
When invoking a superclass version of an overridden method the super keyword is used. Con-
sider the following example:
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:
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.
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.
Whereas the addition of ResearchAssistant class and its calling is shown below
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);
}
}
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.
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: