Experiment 10 24
Experiment 10 24
SAPID: 60003240302
RollNo: I169
Java Experiment 10
10. To implement dynamic polymorphism, final keyword and garbage collection (CO1)
a. Demonstrate using a suitable example that a base class reference variable can point to a child
class object or a base class object using the concept of dynamic method dispatch (dynamic
polymorphism).
Code:
class Animal
{ public void
makeSound()
{
System.out.println("Some generic sound");
}
}
{
System.out.println("Woof!");
}
}
{
System.out.println("Meow!");
}
}
Output:
b. Adwita , 8th Grade student wants to write a functions to calculate simple interest, compound
interest. She wants to keep same (final) rate of interest for every input of principal and time. She wants
to ensure that the declared functions are not overridden in any subclasses and the class is not inherited
by any other class. Help her to declare the variables methods and classes and write the code for the same
using final keyword. Code:
import java.util.*;
Output:
Object Oriented Programming using Java Laboratory (DJS23FLES201)
Academic Year 2024-25
c. WAP to create an object of a class, delete the same object by calling System. gc () and
display a message that the “object has been deleted”.
Code:
class MyClass {
private int id; public
MyClass(int id)
{ this.id
= id;
}
}
class Delete {
public static void main(String[] args) {
MyClass obj = new MyClass(123);
obj = null; System.gc();
Output: