0% found this document useful (0 votes)
13 views8 pages

Lab No 9 Huzaifa

The document outlines a lab assignment focused on Java programming concepts such as polymorphism and abstract classes. It includes tasks that require creating abstract classes, implementing methods in subclasses, and demonstrating polymorphic behavior through various examples. The lab aims to enhance understanding of object-oriented programming principles in Java.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views8 pages

Lab No 9 Huzaifa

The document outlines a lab assignment focused on Java programming concepts such as polymorphism and abstract classes. It includes tasks that require creating abstract classes, implementing methods in subclasses, and demonstrating polymorphic behavior through various examples. The lab aims to enhance understanding of object-oriented programming principles in Java.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 8

Lab#9: Java Environment SSUET/QR/144

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:

CE-225L: Object Oriented Programming Page 1


Lab#9: Java Environment SSUET/QR/144

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:

CE-225L: Object Oriented Programming Page 2


Lab#9: Java Environment SSUET/QR/144

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:

CE-225L: Object Oriented Programming Page 3


Lab#9: Java Environment SSUET/QR/144

Output:

CE-225L: Object Oriented Programming Page 4


Lab#9: Java Environment SSUET/QR/144

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:

CE-225L: Object Oriented Programming Page 5


Lab#9: Java Environment SSUET/QR/144

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.

CE-225L: Object Oriented Programming Page 6


Lab#9: Java Environment SSUET/QR/144

Code:

CE-225L: Object Oriented Programming Page 7


Lab#9: Java Environment SSUET/QR/144

Output:

CE-225L: Object Oriented Programming Page 8

You might also like