You are on page 1of 4

Lab session 5: Java Polymorphism

Objective

The objective of lab session 5 is


 To understand the use of polymorphism
 To differentiate abstract class and interface
 To implement multiple inheritance using interface
 To create and use polymorphic reference

Pre-lab Exercise

1. What concepts come under Polymorphism in java?


A. Method overloading C. Constructor overloading
B. Method overriding D. All the above
2. Which polymorphism behavior do you see in below class?

class Paint {
// all methods have same name
public void Color(int x) {
}

public void Color(int x, int y) {


}

public void Color(int x, int y, int z) {


}
}

A. Method overloading C. Constructor overloading


B. Method overriding D. Run time polymorphism
3. If the subclass does not override the abstract methods of the abstract class, then it is
mandatory for the subclasses to tag itself as abstract.
A. True
B. False

1|Page
4. Which of the following is true about abstract method?
A. Must be implemented in a subclass
B. Must be declared as abstract
C. The method has only the heading with no body
D. None
5. What is wrong with the following block of code?

interface Shape {
void print();
}

Class Test{
public static void main() {
Shape s1=new Shape();
}
}

6. In below java code, whose “Car” will be called?

class Father {

public void car() {


System.out.println("Father's Car");
}
}

class Son extends Father {

public void car() {


System.out.println("Son's Car");
}
}

public class Sample {

2|Page
public static void main(String[] args) {

Son john = new Son();


john.car();
}

A. Father’s Car C. Son’s Car


B. There is an ambiguity, so no one Car D. Compiler Error

7. What will be the output of the following Java code?

class A
{ int i;
public A(){ i=10;}
}
class B extends A
{ int i;
public B(){ i=20;}
}

public class MainClass


{ public static void main(String[] args)
{ A a = new B();
System.out.println(a.i);
}
}

3|Page
In-lab Exercise

8. Design an inheritance hierarchy with Employee base class for HourlyEmployee and
SalaryEmployee subclasses. The common fields and methods to these classes are name
and job title of the employee along with accessors and mutators. The salary employees
need an attribute for weekly salary, and the corresponding methods for accessing and
changing its sate. The HourlyEmployess class possesses pay rate and hours worked fields.
The superclass contains an abstract method called calculateWeeklyPay(), which must
be implemented in classes derived from Employee class. The weekly salary worker’s pay
is just the weekly salary, whereas pay for an hourly employee is simply hours worked times
pay rate.

9. Define an abstract class Shape with abstract method CalculateSurface() and fields
width and height. Define two additional classes for a triangle and a rectangle, which
implement CalculateSurface(). This method has to return the areas of the rectangle
(height*width) and the triangle (height*width/2). Define a class for a circle with an
appropriate constructor, which initializes the two fields (height and width) with the same
value (the radius) and implement the abstract method for calculating the area. Create
three different shapes (rectangle, triangle, circle) and calculate the area of each shape.

Post-lab Exercise: Discuss with Example

10. Discuss the concepts of final and abstract modifiers in Java?


11. Why we use interface in Java?
12. What is the difference between abstract class and interface?

4|Page

You might also like