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

Dhruv Exp10

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)
11 views4 pages

Dhruv Exp10

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

Name: Dhruv Thaker Sap id:60004230210

Branch: Computer Engineering Div:C1

Course: Object Oriented Programming using Java Roll No: C046

Experiment no. _10___


Aim:

To implement dynamic polymorphism, final keyword and garbage collection

Problem Statement 1:

.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 JavaExp10{

public static void main(String[] args) {

superclass A=new superclass();

A.print();

superclass B=new subclass();

B.print();

class superclass{

public void print(){

System.out.println("this is superclass");

class subclass extends superclass{

public void print()


{

System.out.println("this is subclass");

Output

Problem Statement 2:

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

public final class InterestCalculator {

final double rateOfInterest = 5.0;

public final double calculateSimpleInterest(double principal, double time) {

return (principal * rateOfInterest * time) / 100;

public final double calculateCompoundInterest(double principal, double time) {

return principal * Math.pow((1 + rateOfInterest / 100), time) - principal;


}

public static void main(String[] args) {

InterestCalculator calculator = new InterestCalculator();

Scanner scanner = new Scanner(System.in);

System.out.println("Enter the principal amount:");

double principal = scanner.nextDouble();

System.out.println("Enter the time in years:");

double time = scanner.nextDouble();

double simpleInterest = calculator.calculateSimpleInterest(principal, time);

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

double compoundInterest = calculator.calculateCompoundInterest(principal, time);

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

Output

Problem Statement 3:

Code:

public class MyClass {

@Override

protected void finalize() throws Throwable {

super.finalize();

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


}

public static void main(String[] args) {

MyClass obj = new MyClass();

obj = null;

System.gc();

Output

You might also like