Lab No 9 Huzaifa
Lab No 9 Huzaifa
LAB # 09
Polymorphism and abstract class
Objective:
Construct java programs using the concepts polymorphism and abstract classes
Task 1:
Create an abstract class 'Parent' with a method 'message'. It has two subclasses each having
a method with the same name 'message' that prints "This is first subclass" and "This is
second subclass" respectively. Call the methods 'message' by creating an object for each
subclass to show the polymorphic behavior.
Code:
Output:
Task 2:
Create an abstract class 'Animals' with two abstract methods 'cats' and 'dogs'. Now create a
class 'Cats' with a method 'cats' which prints "Cats meow" and a class 'Dogs' with a method
'dogs' which prints "Dogs bark", both inheriting the class 'Animals'. Now create an object for
each of the subclasses and call their respective methods.
Code:
Output:
Task 3:
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 results.
Code:
Output:
Task 4:
Create an Abstract Class named Restaurant
Create a method that will print the name of the restaurant when called.
Create an abstract method named total price
Create an abstract method named menu items
Create an abstract method name location
Create a Class called McDonalds that extends Restaurant
Implement all abstract methods
Add logic so that the total price method/function will give the total price of the
meal including a 6% tax
Add a method that returns a Boolean named hasPlayPlace, which returns true
when this location has a playplace
Create a Constructor that will set the name of the Mcdonalds, location, and hasPlayPlace
Code:
Output:
Task 5:
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
ThreeDimensionalShape 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.
Code:
Output: