[go: up one dir, main page]

0% found this document useful (0 votes)
15 views4 pages

Experiment 10 24

The document outlines Java experiments for Object Oriented Programming, focusing on dynamic polymorphism, the final keyword, and garbage collection. It includes code examples demonstrating dynamic method dispatch with animal sounds, a final class for interest calculations, and object deletion using garbage collection. Each section provides code snippets and expected outputs related to the concepts being taught.

Uploaded by

Mohammad Shaikh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views4 pages

Experiment 10 24

The document outlines Java experiments for Object Oriented Programming, focusing on dynamic polymorphism, the final keyword, and garbage collection. It includes code examples demonstrating dynamic method dispatch with animal sounds, a final class for interest calculations, and object deletion using garbage collection. Each section provides code snippets and expected outputs related to the concepts being taught.

Uploaded by

Mohammad Shaikh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

Object Oriented Programming using Java Laboratory (DJS23FLES201)

Academic Year 2024-25

Name: Mohammad Shaikh

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");
}
}

class Dog extends Animal


{ public void
makeSound()

{
System.out.println("Woof!");
}
}

class Cat extends Animal


{ public void
makeSound()
Object Oriented Programming using Java Laboratory (DJS23FLES201)
Academic Year 2024-25

{
System.out.println("Meow!");
}
}

public class Species


{
public static void main(String[] args) {
Animal myAnimal; myAnimal = new
Dog(); myAnimal.makeSound();

myAnimal = new Cat();


myAnimal.makeSound();

myAnimal = new Animal();


myAnimal.makeSound();
}}

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.*;

final class IntCalc


Object Oriented Programming using Java Laboratory (DJS23FLES201)
Academic Year 2024-25

{ private final double rate;


public IntCalc(double rate)
{ this.rate = rate;
}

public final double calcSI(double principal, double time)


{ return (principal * rate * time) /
100.0;
}

public final double calcCI(double principal, double time)


{
return principal * Math.pow((1 + rate / 100.0), time)-principal;
}
}

public class Dude


{
public static void main(String[] args) {
IntCalc calc = new IntCalc(5.0); double
principal = 1000.0; double time = 2.0;
double SI = calc.calcSI(principal,time);
double CI = calc.calcCI(principal,time);

System.out.println("Simple Interest: " + SI);


System.out.println("Compound Interest: " + CI);
}}

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();

System.out.println("Object has been deleted.");


} }

Output:

You might also like